File tree 6 files changed +34
-17
lines changed
6 files changed +34
-17
lines changed Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ func main() {
29
29
30
30
cache , _ := cache.NewGorm2Cache (&config.CacheConfig {
31
31
CacheLevel: config.CacheLevelAll ,
32
- CacheStorage: storage.NewRedisWithClient ( redisClient),
32
+ CacheStorage: storage.NewRedis (&storage. RedisStoreConfig {Client: redisClient} ),
33
33
InvalidateWhenUpdate: true , // when you create/update/delete objects, invalidate cache
34
34
CacheTTL: 5000 , // 5000 ms
35
35
CacheMaxItemCnt: 50 , // if length of objects retrieved one single time
Original file line number Diff line number Diff line change @@ -76,8 +76,6 @@ func (c *Gorm2Cache) AttachToDB(db *gorm.DB) {
76
76
func (c * Gorm2Cache ) Init () error {
77
77
c .InstanceId = util .GenInstanceId ()
78
78
79
- prefix := util .GormCachePrefix + ":" + c .InstanceId
80
-
81
79
if c .cache != nil {
82
80
c .cache = c .Config .CacheStorage
83
81
} else {
@@ -94,7 +92,7 @@ func (c *Gorm2Cache) Init() error {
94
92
TTL : c .Config .CacheTTL ,
95
93
Debug : c .Config .DebugMode ,
96
94
Logger : c .Logger ,
97
- }, prefix )
95
+ })
98
96
if err != nil {
99
97
c .Logger .CtxError (context .Background (), "[Init] cache init error: %v" , err )
100
98
return err
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ type Gcache struct {
26
26
once sync.Once
27
27
}
28
28
29
- func (g * Gcache ) Init (config * Config , prefix string ) error {
29
+ func (g * Gcache ) Init (config * Config ) error {
30
30
g .once .Do (func () {
31
31
if config .TTL != 0 {
32
32
g .builder .Expiration (time .Duration (config .TTL ) * time .Microsecond )
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ type Config struct {
17
17
}
18
18
19
19
type DataStorage interface {
20
- Init (config * Config , prefix string ) error
20
+ Init (config * Config ) error
21
21
CleanCache (ctx context.Context ) error
22
22
23
23
// read
Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ import (
10
10
"github.com/asjdf/gorm-cache/util"
11
11
)
12
12
13
+ var _ DataStorage = & Memory {}
14
+
13
15
type MemStoreConfig struct {
14
16
MaxSize int64 // maximal items in primary cache
15
17
}
@@ -18,8 +20,11 @@ var DefaultMemStoreConfig = &MemStoreConfig{
18
20
MaxSize : 1000 ,
19
21
}
20
22
21
- func NewMem (config * MemStoreConfig ) * Memory {
22
- return & Memory {config : config }
23
+ func NewMem (config ... * MemStoreConfig ) * Memory {
24
+ if len (config ) == 0 {
25
+ config = append (config , DefaultMemStoreConfig )
26
+ }
27
+ return & Memory {config : config [0 ]}
23
28
}
24
29
25
30
type Memory struct {
@@ -31,7 +36,7 @@ type Memory struct {
31
36
once sync.Once
32
37
}
33
38
34
- func (m * Memory ) Init (conf * Config , prefix string ) error {
39
+ func (m * Memory ) Init (conf * Config ) error {
35
40
m .once .Do (func () {
36
41
c := ccache .New (ccache .Configure [string ]().MaxSize (m .config .MaxSize ))
37
42
m .cache = c
Original file line number Diff line number Diff line change @@ -11,14 +11,29 @@ import (
11
11
12
12
var _ DataStorage = & Redis {}
13
13
14
- func NewRedisWithClient (client * redis.Client ) * Redis {
15
- return & Redis {
16
- client : client ,
17
- }
14
+ type RedisStoreConfig struct {
15
+ KeyPrefix string // key prefix will be random if not set
16
+
17
+ Client * redis.Client // if Client is not nil, Options will be ignored
18
+ Options * redis.Options
18
19
}
19
20
20
- func NewRedisWithOptions (options * redis.Options ) * Redis {
21
- return NewRedisWithClient (redis .NewClient (options ))
21
+ func NewRedis (config ... * RedisStoreConfig ) * Redis {
22
+ if len (config ) == 0 {
23
+ panic ("redis config is required" )
24
+ }
25
+ if config [0 ].KeyPrefix == "" {
26
+ config [0 ].KeyPrefix = util .GormCachePrefix + ":" + util .GenInstanceId ()
27
+ }
28
+ r := & Redis {
29
+ keyPrefix : config [0 ].KeyPrefix ,
30
+ }
31
+ if config [0 ].Client != nil {
32
+ r .client = config [0 ].Client
33
+ return r
34
+ }
35
+ r .client = redis .NewClient (config [0 ].Options )
36
+ return r
22
37
}
23
38
24
39
type Redis struct {
@@ -33,13 +48,12 @@ type Redis struct {
33
48
once sync.Once
34
49
}
35
50
36
- func (r * Redis ) Init (conf * Config , prefix string ) error {
51
+ func (r * Redis ) Init (conf * Config ) error {
37
52
var err error
38
53
r .once .Do (func () {
39
54
r .ttl = conf .TTL
40
55
r .logger = conf .Logger
41
56
r .logger .SetIsDebug (conf .Debug )
42
- r .keyPrefix = prefix
43
57
err = r .initScripts ()
44
58
})
45
59
return err
You can’t perform that action at this time.
0 commit comments