forked from eaigner/jet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlru.go
97 lines (81 loc) · 1.62 KB
/
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
package jet
import (
"database/sql"
"sort"
"sync"
"time"
)
// The LRUCache can speed up queries by caching prepared statements.
type LRUCache struct {
m map[string]*lruItem
max int
mtx sync.Mutex
}
type lruItem struct {
key string
stmt *sql.Stmt
lastAccess time.Time
}
type lruList []*lruItem
func (l lruList) Len() int {
return len(l)
}
func (l lruList) Less(i, j int) bool {
return l[i].lastAccess.Before(l[j].lastAccess)
}
func (l lruList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
// NewLRUCache creates a new LRU cache with the specified size.
// You can set this cache on a *Db instance.
func NewLRUCache(max int) *LRUCache {
c := &LRUCache{max: max}
c.reset()
return c
}
func (c *LRUCache) set(key string, stmt *sql.Stmt) {
c.mtx.Lock()
defer c.mtx.Unlock()
c.m[key] = &lruItem{
key: key,
stmt: stmt,
lastAccess: time.Now(),
}
c.cleanIfNeeded()
}
func (c *LRUCache) get(key string) *sql.Stmt {
c.mtx.Lock()
defer c.mtx.Unlock()
if v, ok := c.m[key]; ok {
return v.stmt
}
return nil
}
func (c *LRUCache) cleanIfNeeded() {
if c.max == 0 {
c.max = 20
}
if len(c.m) > c.max {
c.clean()
}
}
func (c *LRUCache) clean() {
a := make(lruList, 0, len(c.m))
for _, v := range c.m {
a = append(a, v)
}
sort.Sort(sort.Reverse(a))
for _, v := range a[c.max:] {
// BUG(erik): investigate why a statement close from LRU triggers a nil pointer exception in database/sql/sql.go:384
//
// if v.stmt != nil {
// v.stmt.Close()
// }
delete(c.m, v.key)
}
}
func (c *LRUCache) reset() {
c.mtx.Lock()
defer c.mtx.Unlock()
c.m = make(map[string]*lruItem, c.max)
}