-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathauto_cache_fake.go
More file actions
50 lines (41 loc) · 1.18 KB
/
auto_cache_fake.go
File metadata and controls
50 lines (41 loc) · 1.18 KB
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
package cache
import (
"sync"
"time"
"go-cache/errors"
)
// StorageAutoCacheFake implements cache that uses auto cache as storage
// This type of autocache have no TTL
type StorageAutoCacheFake struct {
updaters map[string]func() (interface{}, error)
mutex sync.RWMutex
}
// NewStorageAutoCache create new instance of StorageAutoCache
func NewStorageAutoCacheFake() *StorageAutoCacheFake {
return &StorageAutoCacheFake{
updaters: make(map[string]func() (interface{}, error)),
}
}
// Get returns data by given key
func (storage *StorageAutoCacheFake) Get(key string) (interface{}, error) {
storage.mutex.RLock()
updater, find := storage.updaters[key]
storage.mutex.RUnlock()
if !find {
return nil, errors.Errorf("Auto cache key %s nof found", key)
}
return updater()
}
// Remove removes value by key
func (storage *StorageAutoCacheFake) Remove(key string) {
storage.mutex.Lock()
delete(storage.updaters, key)
storage.mutex.Unlock()
}
// Put puts data into storage
func (storage *StorageAutoCacheFake) Put(updater func() (interface{}, error), key string, ttl time.Duration) error {
storage.mutex.Lock()
storage.updaters[key] = updater
storage.mutex.Unlock()
return nil
}