-
Notifications
You must be signed in to change notification settings - Fork 2
/
bolt_lru.go
145 lines (134 loc) · 2.96 KB
/
bolt_lru.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
134
135
136
137
138
139
140
141
142
143
144
145
// This package provides a simple LRU cache backed by boltdb It is based on the
// LRU implementation in groupcache:
// https://github.com/golang/groupcache/tree/master/lru
package boltlru
import (
"container/list"
"errors"
"github.com/boltdb/bolt"
"sync"
)
var bucketName = []byte("cache")
type Cache struct {
size int
evictList *list.List
items map[string]*list.Element
lock sync.RWMutex
db *bolt.DB
}
func New(size int, dbPath string) (*Cache, error) {
if size <= 0 {
return nil, errors.New("Must provide a positive size")
}
db, err := bolt.Open(dbPath, 0640, nil)
if err != nil {
return nil, err
}
err = db.Update(func(tx *bolt.Tx) (err error) {
_, err = tx.CreateBucketIfNotExists(bucketName)
return err
})
if err != nil {
return nil, err
}
c := &Cache{
size: size,
evictList: list.New(),
items: make(map[string]*list.Element, size),
db: db,
}
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketName)
cur := b.Cursor()
var key string
for k, _ := cur.First(); k != nil; k, _ = cur.Next() {
key = string(k)
el := c.evictList.PushFront(key)
c.items[key] = el
}
return nil
})
return c, nil
}
func (c *Cache) Add(key string, value []byte) error {
return c.AddMulti(map[string][]byte{key: value})
}
func (c *Cache) AddMulti(data map[string][]byte) error {
keysToRemove := make([]string, 0)
c.lock.Lock()
for key, _ := range data {
if el, ok := c.items[key]; ok {
c.evictList.MoveToFront(el)
} else {
el := c.evictList.PushFront(key)
c.items[key] = el
evict := c.evictList.Len() > c.size
if evict {
el := c.evictList.Back()
if el != nil {
key := el.Value.(string)
c.evictList.Remove(el)
delete(c.items, key)
keysToRemove = append(keysToRemove, key)
}
}
}
}
c.lock.Unlock()
return c.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketName)
var err error
for _, k := range keysToRemove {
err = b.Delete([]byte(k))
if err != nil {
return err
}
}
for key, value := range data {
err = b.Put([]byte(key), value)
if err != nil {
return err
}
}
return nil
})
}
func (c *Cache) Get(key string) (value []byte, err error) {
data, err := c.MultiGet([]string{key})
if err != nil {
return
}
value, ok := data[key]
if !ok {
err = errors.New("not found")
}
return
}
func (c *Cache) MultiGet(keys []string) (values map[string][]byte, err error) {
c.lock.Lock()
existsKeys := make([]string, 0, len(keys))
for _, k := range keys {
if el, ok := c.items[k]; ok {
existsKeys = append(existsKeys, k)
c.evictList.MoveToFront(el)
}
}
c.lock.Unlock()
values = make(map[string][]byte, len(existsKeys))
err = c.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketName)
for _, k := range existsKeys {
values[k] = b.Get([]byte(k))
}
return nil
})
return
}
func (c *Cache) Len() int {
c.lock.RLock()
defer c.lock.RUnlock()
return c.evictList.Len()
}
func (c *Cache) Close() {
c.db.Close()
}