-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
46 lines (38 loc) · 1.04 KB
/
redis.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
package echo_http_cache
import (
"context"
"time"
redisCache "github.com/go-redis/cache/v8"
"github.com/go-redis/redis/v8"
)
type (
// CacheRedisStore is the redis standalone store implementation for Cache
CacheRedisStore struct {
store *redisCache.Cache
}
)
func NewCacheRedisStoreWithConfig(opt redis.Options) CacheStore {
return &CacheRedisStore{
redisCache.New(&redisCache.Options{
Redis: redis.NewClient(&opt),
}),
}
}
// Get implements the cache CacheRedisClusterStore interface Get method.
func (store *CacheRedisStore) Get(key uint64) ([]byte, bool) {
var data []byte
if err := store.store.Get(context.Background(), keyAsString(key), &data); err == nil {
return data, true
}
return nil, false
}
func (store *CacheRedisStore) Set(key uint64, response []byte, expiration time.Time) {
store.store.Set(&redisCache.Item{
Key: keyAsString(key),
Value: response,
TTL: expiration.Sub(time.Now()),
})
}
func (store *CacheRedisStore) Release(key uint64) {
store.store.Delete(context.Background(), keyAsString(key))
}