-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmemory.go
60 lines (51 loc) · 1.63 KB
/
memory.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
52
53
54
55
56
57
58
59
60
package kvs
import (
"time"
gocache "github.com/pmylund/go-cache"
)
// MemoryKeyValueStore is an in-memory cache implementation of KeyValueStore.
type MemoryKeyValueStore struct {
Cache *gocache.Cache
cleanupInterval time.Duration
}
// NewDefaultMemoryStore creates an instance of MemoryKeyValueStore
// with default settings.
func NewDefaultMemoryStore() KeyValueStore {
return NewMemoryKeyValueStore(30 * time.Second)
}
// NewMemoryKeyValueStore creates an instance of MemoryKeyValueStore
// with given backing.
func NewMemoryKeyValueStore(cleanupInterval time.Duration) *MemoryKeyValueStore {
cache := gocache.New(gocache.NoExpiration, cleanupInterval)
store := &MemoryKeyValueStore{
Cache: cache,
cleanupInterval: cleanupInterval,
}
return store
}
// Set sets a key with time-to-live.
func (store *MemoryKeyValueStore) Set(key, value string, ttl time.Duration) error {
if ttl < store.cleanupInterval {
logger.Warn("The cleanupInterval setting for in-memory key-value store is longer than the TTL of this operation, which means its effective TTL is based on the cleanupInterval")
}
store.Cache.Set(key, value, ttl)
return nil
}
// Get retrieves a value given key.
func (store *MemoryKeyValueStore) Get(key string) (string, error) {
val, found := store.Cache.Get(key)
if !found {
return "", nil
}
return val.(string), nil
}
// Del deletes value given key.
func (store *MemoryKeyValueStore) Del(key string) error {
store.Cache.Delete(key)
return nil
}
// FlushDB clears all keys
func (store *MemoryKeyValueStore) FlushDB() error {
store.Cache = gocache.New(gocache.NoExpiration, store.cleanupInterval)
return nil
}