-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
100 lines (83 loc) · 2.38 KB
/
cache.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package cache
import (
"github.com/thiagozs/go-cache/buntdb"
"github.com/thiagozs/go-cache/gocache"
"github.com/thiagozs/go-cache/kind"
"github.com/thiagozs/go-cache/redis"
)
type Cache struct {
db CacheRepo
}
func New(opts ...Options) (*Cache, error) {
var db CacheRepo
var err error
params, err := newCacheParams(opts...)
if err != nil {
return nil, err
}
if params.GetCache() != nil {
return &Cache{db: params.GetCache()}, nil
}
switch params.GetDriver() {
case kind.BUNTDB:
opts := []buntdb.Options{
buntdb.OptFile(params.GetFile()),
buntdb.OptFolder(params.GetFolder()),
buntdb.OptTTL(params.GetTTL()),
buntdb.OptLogDebug(params.GetLogDebug()),
buntdb.OptLogDisable(params.GetLogDisable()),
buntdb.OptDriver(params.GetDriver()),
}
db, err = buntdb.NewBuntDB(opts...)
case kind.REDIS:
opts := []redis.Options{
redis.OptHost(params.GetHost()),
redis.OptPort(params.GetPort()),
redis.OptTTL(params.GetTTL()),
redis.OptLogDebug(params.GetLogDebug()),
redis.OptLogDisable(params.GetLogDisable()),
redis.OptDriver(params.GetDriver()),
}
db, err = redis.NewRedis(opts...)
case kind.GOCACHE:
opts := []gocache.Options{
gocache.OptTExpiration(params.GetTExpiration()),
gocache.OptTCleanUpInt(params.GetTCleanUpInt()),
gocache.OptLogDebug(params.GetLogDebug()),
gocache.OptLogDisable(params.GetLogDisable()),
gocache.OptDriver(params.GetDriver()),
}
db, err = gocache.NewMemory(opts...)
}
if err != nil {
return nil, err
}
return &Cache{db: db}, nil
}
func (d *Cache) WriteKeyVal(key string, val string) error {
return d.db.WriteKeyVal(key, val)
}
func (d *Cache) WriteKeyValTTL(key string, val string, ttlSeconds int) error {
return d.db.WriteKeyValTTL(key, val, ttlSeconds)
}
func (d *Cache) DeleteKey(key string) (string, error) {
return d.db.DeleteKey(key)
}
func (d *Cache) WriteKeyValAsJSON(key string, val interface{}) error {
return d.db.WriteKeyValAsJSON(key, val)
}
func (d *Cache) WriteKeyValAsJSONTTL(key string, val interface{}, ttlSeconds int) error {
return d.db.WriteKeyValAsJSONTTL(key, val, ttlSeconds)
}
func (d *Cache) GetVal(key string) (string, error) {
return d.db.GetVal(key)
}
func (d *Cache) GetDriver() kind.Driver {
return d.db.GetDriver()
}
func (d *Cache) Incr(key string) (int64, error) {
return d.db.Incr(key)
}
func (d *Cache) Decr(key string) (int64, error) {
return d.db.Decr(key)
}