-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory.go
51 lines (42 loc) · 1010 Bytes
/
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
package cache
import (
"sync"
"time"
)
const inMemoryStoreName = "In-Memory Store"
// NewInMemoryStore creates a new in-memory store
func NewInMemoryStore() Store {
return &inmemorystore{
cache: make(map[string]cacheddata),
config: StoreConfig{StoreName: inMemoryStoreName},
}
}
type inmemorystore struct {
sync.RWMutex
cache map[string]cacheddata
config StoreConfig
}
func (ims *inmemorystore) Store(cacheKey string, data []byte) error {
ims.Lock()
ims.cache[cacheKey] = cacheddata{data, time.Now()}
ims.Unlock()
return nil
}
func (ims *inmemorystore) Retrieve(cacheKey string) (*cacheddata, bool, error) {
ims.RLock()
cachedData, ok := ims.cache[cacheKey]
ims.RUnlock()
return &cachedData, ok, nil
}
func (ims *inmemorystore) Delete(cacheKey string) error {
ims.Lock()
delete(ims.cache, cacheKey)
ims.Unlock()
return nil
}
func (ims *inmemorystore) Config() StoreConfig {
return ims.config
}
func (ims *inmemorystore) SetConfig(config StoreConfig) {
ims.config = config
}