-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
bigcache.go
51 lines (42 loc) · 1.11 KB
/
bigcache.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
47
48
49
50
51
package store
import (
"errors"
"time"
)
// BigcacheClientInterface represents a allegro/bigcache client
type BigcacheClientInterface interface {
Get(key string) ([]byte, error)
Set(key string, entry []byte) error
}
const (
BigcacheType = "bigcache"
)
// BigcacheStore is a store for Redis
type BigcacheStore struct {
client BigcacheClientInterface
}
// NewBigcache creates a new store to Bigcache instance(s)
func NewBigcache(client BigcacheClientInterface) *BigcacheStore {
return &BigcacheStore{
client: client,
}
}
// Get returns data stored from a given key
func (s *BigcacheStore) Get(key interface{}) (interface{}, error) {
item, err := s.client.Get(key.(string))
if err != nil {
return nil, err
}
if item == nil {
return nil, errors.New("Unable to retrieve data from bigcache")
}
return item, err
}
// Set defines data in Redis for given key idntifier
func (s *BigcacheStore) Set(key interface{}, value interface{}, expiration time.Duration) error {
return s.client.Set(key.(string), value.([]byte))
}
// GetType returns the store type
func (s *BigcacheStore) GetType() string {
return BigcacheType
}