-
Notifications
You must be signed in to change notification settings - Fork 2
/
inmemory.go
133 lines (107 loc) · 2.8 KB
/
inmemory.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package cache
import (
"encoding/json"
"time"
"sync"
"github.com/patrickmn/go-cache"
)
type InMemoryCache struct {
cache cache.Cache // Only expose the methods we want to make available
mu sync.RWMutex // For increment / decrement prevent reads and writes
defaultExpiration time.Duration // DefaultExpiration.
}
func NewInMemoryCache(defaultExpiration time.Duration) InMemoryCache {
return InMemoryCache{
cache: *cache.New(defaultExpiration, time.Minute),
mu: sync.RWMutex{},
defaultExpiration: defaultExpiration,
}
}
func (c InMemoryCache) Get(key string, ptrValue interface{}) error {
c.mu.RLock()
defer c.mu.RUnlock()
value, found := c.cache.Get(key)
if !found {
return ErrCacheMiss
}
bytes, err := json.Marshal(value)
if err != nil {
return err
}
return json.Unmarshal(bytes, ptrValue)
}
func (c InMemoryCache) GetMulti(keys ...string) (Getter, error) {
return c, nil
}
func (c InMemoryCache) SetFields(key string, value map[string]interface{}, expires time.Duration) error {
c.mu.RLock()
defer c.mu.RUnlock()
existing := map[string]interface{}{}
v, found := c.cache.Get(key)
if !found {
return ErrNotStored
}
bytes, err := json.Marshal(v)
if err != nil {
return err
}
if err := json.Unmarshal(bytes, &existing); err != nil {
return err
}
for k, v := range value {
existing[k] = v
}
c.cache.Set(key, existing, expires)
return nil
}
func (c InMemoryCache) Set(key string, value interface{}, expires time.Duration) error {
c.mu.Lock()
defer c.mu.Unlock()
// NOTE: go-cache understands the values of DefaultExpiryTime and ForEverNeverExpiry
c.cache.Set(key, value, expires)
return nil
}
func (c InMemoryCache) Add(key string, value interface{}, expires time.Duration) error {
c.mu.Lock()
defer c.mu.Unlock()
err := c.cache.Add(key, value, expires)
if err != nil {
return ErrNotStored
}
return err
}
func (c InMemoryCache) Replace(key string, value interface{}, expires time.Duration) error {
c.mu.Lock()
defer c.mu.Unlock()
if err := c.cache.Replace(key, value, expires); err != nil {
return ErrNotStored
}
return nil
}
func (c InMemoryCache) Keys() ([]string, error) {
items := func() map[string]cache.Item {
c.mu.Lock()
defer c.mu.Unlock()
return c.cache.Items()
}()
keys := make([]string, 0, len(items))
for k := range items {
keys = append(keys, k)
}
return keys, nil
}
func (c InMemoryCache) Delete(key string) error {
c.mu.RLock()
defer c.mu.RUnlock()
c.cache.Delete(key)
return nil
}
func (c InMemoryCache) Flush() error {
c.mu.Lock()
defer c.mu.Unlock()
c.cache.Flush()
return nil
}