-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
118 lines (101 loc) · 2.52 KB
/
cache.go
File metadata and controls
118 lines (101 loc) · 2.52 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
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
package ddgo
import (
"container/list"
"sync"
)
// ResultCache is a pluggable cache used by Detector.Parse.
//
// Implementations must be safe for concurrent use.
type ResultCache interface {
Get(key string) (Result, bool)
Set(key string, result Result)
}
// LRUResultCache is a bounded in-memory LRU cache implementation.
type LRUResultCache struct {
mu sync.Mutex
capacity int
order *list.List
entries map[string]*list.Element
}
type cacheEntry struct {
key string
result Result
}
// NewLRUResultCache creates a bounded in-memory LRU parse result cache.
//
// Returns nil when capacity is <= 0.
func NewLRUResultCache(capacity int) *LRUResultCache {
if capacity <= 0 {
return nil
}
return newLRUResultCache(capacity)
}
func newLRUResultCache(capacity int) *LRUResultCache {
if capacity <= 0 {
return nil
}
return &LRUResultCache{
capacity: capacity,
order: list.New(),
entries: make(map[string]*list.Element, capacity),
}
}
// Get returns a cached result for key and updates recency on hit.
func (c *LRUResultCache) Get(key string) (Result, bool) {
c.mu.Lock()
defer c.mu.Unlock()
elem, ok := c.entries[key]
if !ok {
return Result{}, false
}
c.order.MoveToFront(elem)
return elem.Value.(cacheEntry).result, true
}
// Set stores a result for key and evicts the least-recently-used entry when full.
func (c *LRUResultCache) Set(key string, result Result) {
c.mu.Lock()
defer c.mu.Unlock()
if elem, ok := c.entries[key]; ok {
entry := elem.Value.(cacheEntry)
entry.result = result
elem.Value = entry
c.order.MoveToFront(elem)
return
}
elem := c.order.PushFront(cacheEntry{key: key, result: result})
c.entries[key] = elem
if c.order.Len() <= c.capacity {
return
}
tail := c.order.Back()
if tail == nil {
return
}
entry := tail.Value.(cacheEntry)
delete(c.entries, entry.key)
c.order.Remove(tail)
}
// MemoryResultCache is a simple unbounded in-memory cache implementation.
type MemoryResultCache struct {
mu sync.RWMutex
entries map[string]Result
}
// NewMemoryResultCache creates an unbounded in-memory cache.
func NewMemoryResultCache() *MemoryResultCache {
return &MemoryResultCache{
entries: make(map[string]Result),
}
}
// Get returns a cached result for key.
func (c *MemoryResultCache) Get(key string) (Result, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
result, ok := c.entries[key]
return result, ok
}
// Set stores a cached result for key.
func (c *MemoryResultCache) Set(key string, result Result) {
c.mu.Lock()
defer c.mu.Unlock()
c.entries[key] = result
}