Skip to content

Commit 694911d

Browse files
committed
add redis as cache
1 parent 0bb5c19 commit 694911d

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

cache/cache.go

+8
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package cache
22

33
import (
44
"time"
5+
6+
"github.com/redis/go-redis/v9"
57
)
68

79
// Cache ...
810
type Cache interface {
11+
getClient() *redis.Client
912
set(key string, value V)
1013
setWithExpiry(key string, value V, expiry time.Duration)
1114
get(key string) V
@@ -27,6 +30,11 @@ func Init(driver CacheDriver, url ...string) {
2730
go conn.cleanUp()
2831
}
2932

33+
// Client ...
34+
func Client() *redis.Client {
35+
return conn.getClient()
36+
}
37+
3038
// Set ...
3139
func Set(key string, value V) {
3240
conn.set(key, value)

cache/memory.go

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/ochom/gutils/env"
8+
"github.com/redis/go-redis/v9"
89
)
910

1011
// memoryCache implements Cache
@@ -22,6 +23,11 @@ func newMemoryCache() Cache {
2223
}
2324
}
2425

26+
// getClient ...
27+
func (m *memoryCache) getClient() *redis.Client {
28+
return nil
29+
}
30+
2531
// set ...
2632
func (m *memoryCache) set(key string, value V) {
2733
expiry := time.Hour * time.Duration(env.Int("MAX_CACHE_HOUR", 24))

cache/redis.go

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ func newRedisCache(url ...string) Cache {
3131
}
3232
}
3333

34+
// getClient ...
35+
func (r *redisCache) getClient() *redis.Client {
36+
return r.client
37+
}
38+
3439
// set ...
3540
func (r *redisCache) set(key string, value V) {
3641
r.setWithExpiry(key, value, 0)

0 commit comments

Comments
 (0)