1
- package redis
1
+ package rediscache
2
2
3
3
import (
4
4
"context"
@@ -11,22 +11,22 @@ import (
11
11
"go.uber.org/zap"
12
12
)
13
13
14
- type cache struct {
14
+ type provider struct {
15
15
client * redis.Client
16
16
opts * _cache.Redis
17
17
}
18
18
19
- func New (opts * _cache.Redis ) * cache {
20
- return & cache {opts : opts }
19
+ func New (opts * _cache.Redis ) * provider {
20
+ return & provider {opts : opts }
21
21
}
22
22
23
23
// WithClient creates a new cache with the given client
24
- func WithClient (client * redis.Client ) * cache {
25
- return & cache {client : client }
24
+ func WithClient (client * redis.Client ) * provider {
25
+ return & provider {client : client }
26
26
}
27
27
28
28
// Connect connects to the redis server
29
- func (c * cache ) Connect (_ context.Context ) error {
29
+ func (c * provider ) Connect (_ context.Context ) error {
30
30
c .client = redis .NewClient (& redis.Options {
31
31
Addr : fmt .Sprintf ("%s:%d" , c .opts .Host , c .opts .Port ),
32
32
Password : c .opts .Password ,
@@ -36,12 +36,12 @@ func (c *cache) Connect(_ context.Context) error {
36
36
}
37
37
38
38
// Store stores the data in the cache
39
- func (c * cache ) Store (ctx context.Context , cacheKey string , data _cache.CacheableEntity , ttl time.Duration ) error {
39
+ func (c * provider ) Store (ctx context.Context , cacheKey string , data _cache.CacheableEntity , ttl time.Duration ) error {
40
40
return c .client .Set (ctx , cacheKey , data , ttl ).Err ()
41
41
}
42
42
43
43
// Retrieve retrieves the data from the cache
44
- func (c * cache ) Retrieve (ctx context.Context , cacheKey string , dest _cache.CacheableEntity , allowExpired bool ) (_cache.RetrieveStatus , error ) {
44
+ func (c * provider ) Retrieve (ctx context.Context , cacheKey string , dest _cache.CacheableEntity , allowExpired bool ) (_cache.RetrieveStatus , error ) {
45
45
err := c .client .Get (ctx , cacheKey ).Scan (dest )
46
46
if err != nil {
47
47
if errors .Is (err , redis .Nil ) {
@@ -53,47 +53,47 @@ func (c *cache) Retrieve(ctx context.Context, cacheKey string, dest _cache.Cache
53
53
}
54
54
55
55
// SetTTL sets the TTL for the cache entry
56
- func (c * cache ) SetTTL (ctx context.Context , cacheKey string , ttl time.Duration ) {
56
+ func (c * provider ) SetTTL (ctx context.Context , cacheKey string , ttl time.Duration ) {
57
57
err := c .client .Expire (ctx , cacheKey , ttl ).Err ()
58
58
if err != nil {
59
59
zap .L ().Error ("error setting TTL for cache key" , zap .String ("cacheKey" , cacheKey ), zap .Duration ("ttl" , ttl ), zap .Error (err ))
60
60
}
61
61
}
62
62
63
63
// Remove removes the cache entry
64
- func (c * cache ) Remove (ctx context.Context , cacheKey string ) {
64
+ func (c * provider ) Remove (ctx context.Context , cacheKey string ) {
65
65
c .BulkRemove (ctx , []string {cacheKey })
66
66
}
67
67
68
68
// BulkRemove removes the cache entries
69
- func (c * cache ) BulkRemove (ctx context.Context , cacheKeys []string ) {
69
+ func (c * provider ) BulkRemove (ctx context.Context , cacheKeys []string ) {
70
70
if err := c .client .Del (ctx , cacheKeys ... ).Err (); err != nil {
71
71
zap .L ().Error ("error deleting cache keys" , zap .Strings ("cacheKeys" , cacheKeys ), zap .Error (err ))
72
72
}
73
73
}
74
74
75
75
// Close closes the connection to the redis server
76
- func (c * cache ) Close (_ context.Context ) error {
76
+ func (c * provider ) Close (_ context.Context ) error {
77
77
return c .client .Close ()
78
78
}
79
79
80
80
// Ping pings the redis server
81
- func (c * cache ) Ping (ctx context.Context ) error {
81
+ func (c * provider ) Ping (ctx context.Context ) error {
82
82
return c .client .Ping (ctx ).Err ()
83
83
}
84
84
85
85
// GetClient returns the redis client
86
- func (c * cache ) GetClient () * redis.Client {
86
+ func (c * provider ) GetClient () * redis.Client {
87
87
return c .client
88
88
}
89
89
90
90
// GetOptions returns the options
91
- func (c * cache ) GetOptions () * _cache.Redis {
91
+ func (c * provider ) GetOptions () * _cache.Redis {
92
92
return c .opts
93
93
}
94
94
95
95
// GetTTL returns the TTL for the cache entry
96
- func (c * cache ) GetTTL (ctx context.Context , cacheKey string ) time.Duration {
96
+ func (c * provider ) GetTTL (ctx context.Context , cacheKey string ) time.Duration {
97
97
ttl , err := c .client .TTL (ctx , cacheKey ).Result ()
98
98
if err != nil {
99
99
zap .L ().Error ("error getting TTL for cache key" , zap .String ("cacheKey" , cacheKey ), zap .Error (err ))
@@ -102,12 +102,12 @@ func (c *cache) GetTTL(ctx context.Context, cacheKey string) time.Duration {
102
102
}
103
103
104
104
// GetKeys returns the keys matching the pattern
105
- func (c * cache ) GetKeys (ctx context.Context , pattern string ) ([]string , error ) {
105
+ func (c * provider ) GetKeys (ctx context.Context , pattern string ) ([]string , error ) {
106
106
return c .client .Keys (ctx , pattern ).Result ()
107
107
}
108
108
109
109
// GetKeysWithTTL returns the keys matching the pattern with their TTL
110
- func (c * cache ) GetKeysWithTTL (ctx context.Context , pattern string ) (map [string ]time.Duration , error ) {
110
+ func (c * provider ) GetKeysWithTTL (ctx context.Context , pattern string ) (map [string ]time.Duration , error ) {
111
111
keys , err := c .GetKeys (ctx , pattern )
112
112
if err != nil {
113
113
return nil , err
0 commit comments