-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.go
42 lines (32 loc) · 818 Bytes
/
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
package adapter
import (
"fmt"
"time"
"github.com/duc-cnzj/mars/v4/internal/contracts"
"github.com/patrickmn/go-cache"
)
type goCacheAdapter struct {
c *cache.Cache
}
// NewGoCacheAdapter return Store instance.
func NewGoCacheAdapter(c *cache.Cache) contracts.Store {
return &goCacheAdapter{c: c}
}
// Get return val from cache.
func (g *goCacheAdapter) Get(key string) (value []byte, err error) {
v, b := g.c.Get(key)
if !b {
return nil, fmt.Errorf("key %s not found", key)
}
return v.([]byte), nil
}
// Set val for key.
func (g *goCacheAdapter) Set(key string, value []byte, expireSeconds int) (err error) {
g.c.Set(key, value, time.Second*time.Duration(expireSeconds))
return nil
}
// Delete key from cache.
func (g *goCacheAdapter) Delete(key string) error {
g.c.Delete(key)
return nil
}