forked from dropbox/godropbox
/
local_map_storage.go
69 lines (55 loc) · 1.53 KB
/
local_map_storage.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
package caching
type LocalMapStorage struct {
}
type localMapStorage struct {
keyStringFunc ToStringFunc
itemToKeyStringFunc ToStringFunc
keyVal map[string]interface{}
}
func (s *localMapStorage) get(key interface{}) (interface{}, error) {
if item, inMap := s.keyVal[s.keyStringFunc(key)]; inMap {
return item, nil
}
return nil, nil
}
func (s *localMapStorage) set(item interface{}) error {
s.keyVal[s.itemToKeyStringFunc(item)] = item
return nil
}
func (s *localMapStorage) del(key interface{}) error {
delete(s.keyVal, s.keyStringFunc(key))
return nil
}
func (s *localMapStorage) flush() error {
s.keyVal = make(map[string]interface{})
return nil
}
func (s *localMapStorage) size() int {
return len(s.keyVal)
}
func newLocalMapStorage(
name string,
KeyStringFunc ToStringFunc,
ItemToKeyStringFunc ToStringFunc) (*localMapStorage, Storage) {
storage := &localMapStorage{
keyStringFunc: KeyStringFunc,
itemToKeyStringFunc: ItemToKeyStringFunc,
keyVal: make(map[string]interface{}),
}
options := GenericStorageOptions{
GetFunc: storage.get,
SetFunc: storage.set,
DelFunc: storage.del,
FlushFunc: storage.flush,
}
return storage, NewGenericStorage(name, options)
}
// This returns a local non-persistent storage which uses map[string]interface{}
// as its underlying storage.
func NewLocalMapStorage(
name string,
KeyStringFunc ToStringFunc,
ItemToKeyStringFunc ToStringFunc) Storage {
_, storage := newLocalMapStorage(name, KeyStringFunc, ItemToKeyStringFunc)
return storage
}