-
Notifications
You must be signed in to change notification settings - Fork 3
/
dao.go
82 lines (68 loc) · 1.7 KB
/
dao.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package fridge
import (
"fmt"
"github.com/shomali11/util/xconversions"
"time"
)
const (
configKeyFormat = "%s.config"
)
// Dao controls access to redis
type Dao struct {
cache Cache
}
// Get retrieves an item
func (d *Dao) Get(key string) (string, bool, error) {
return d.cache.Get(key)
}
// Set stores a value
func (d *Dao) Set(key string, value string, timeout time.Duration) error {
return d.cache.Set(key, value, timeout)
}
// SetStorageDetails stores a key's defaults
func (d *Dao) SetStorageDetails(key string, storageDetails *StorageDetails) error {
storageDetails.Timestamp = time.Now().UTC()
timestampString, err := xconversions.Stringify(storageDetails)
if err != nil {
return err
}
configKey := fmt.Sprintf(configKeyFormat, key)
return d.cache.Set(configKey, timestampString, 0)
}
// GetStorageDetails retrieves a key's storage details
func (d *Dao) GetStorageDetails(key string) (*StorageDetails, bool, error) {
configKey := fmt.Sprintf(configKeyFormat, key)
configString, found, err := d.cache.Get(configKey)
if err != nil {
return nil, false, err
}
if !found {
return nil, false, nil
}
var storageDetails *StorageDetails
err = xconversions.Structify(configString, &storageDetails)
if err != nil {
return nil, false, err
}
return storageDetails, true, nil
}
// Remove an item
func (d *Dao) Remove(key string) error {
timestampKey := fmt.Sprintf(configKeyFormat, key)
err := d.cache.Remove(key)
if err != nil {
return err
}
return d.cache.Remove(timestampKey)
}
// Ping pings redis
func (d *Dao) Ping() error {
return d.cache.Ping()
}
// Close closes resources
func (d *Dao) Close() error {
return d.cache.Close()
}
func newDao(cache Cache) *Dao {
return &Dao{cache: cache}
}