forked from scylladb/gocql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepared_cache.go
89 lines (71 loc) · 1.6 KB
/
prepared_cache.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
package gocql
import (
"bytes"
"github.com/gocql/gocql/internal/lru"
"sync"
)
const defaultMaxPreparedStmts = 1000
// preparedLRU is the prepared statement cache
type preparedLRU struct {
mu sync.Mutex
lru *lru.Cache
}
// Max adjusts the maximum size of the cache and cleans up the oldest records if
// the new max is lower than the previous value. Not concurrency safe.
func (p *preparedLRU) max(max int) {
p.mu.Lock()
defer p.mu.Unlock()
for p.lru.Len() > max {
p.lru.RemoveOldest()
}
p.lru.MaxEntries = max
}
func (p *preparedLRU) clear() {
p.mu.Lock()
defer p.mu.Unlock()
for p.lru.Len() > 0 {
p.lru.RemoveOldest()
}
}
func (p *preparedLRU) add(key string, val *inflightPrepare) {
p.mu.Lock()
defer p.mu.Unlock()
p.lru.Add(key, val)
}
func (p *preparedLRU) remove(key string) bool {
p.mu.Lock()
defer p.mu.Unlock()
return p.lru.Remove(key)
}
func (p *preparedLRU) execIfMissing(key string, fn func(lru *lru.Cache) *inflightPrepare) (*inflightPrepare, bool) {
p.mu.Lock()
defer p.mu.Unlock()
val, ok := p.lru.Get(key)
if ok {
return val.(*inflightPrepare), true
}
return fn(p.lru), false
}
func (p *preparedLRU) keyFor(addr, keyspace, statement string) string {
// TODO: we should just use a struct for the key in the map
return addr + keyspace + statement
}
func (p *preparedLRU) evictPreparedID(key string, id []byte) {
p.mu.Lock()
defer p.mu.Unlock()
val, ok := p.lru.Get(key)
if !ok {
return
}
ifp, ok := val.(*inflightPrepare)
if !ok {
return
}
select {
case <-ifp.done:
if bytes.Equal(id, ifp.preparedStatment.id) {
p.lru.Remove(key)
}
default:
}
}