forked from qax-os/excelize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
117 lines (98 loc) · 2.19 KB
/
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
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
package excelize
import (
"container/list"
"sync"
)
type formulaResult struct {
value formulaArg
err error
}
type Cache struct {
cache map[string]*list.Element
list *list.List
mutex sync.RWMutex
limit int
disabled bool
hits int
misses int
}
func NewCache() *Cache {
return &Cache{
cache: make(map[string]*list.Element),
list: list.New(),
limit: 0, // Unlimited entries by default
}
}
func (c *Cache) SetLimit(limit int) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.limit = limit
// If the limit is reduced, evict excess entries
if limit > 0 {
for c.list.Len() > limit {
oldest := c.list.Back()
if oldest != nil {
cellRef := oldest.Value.(string)
delete(c.cache, cellRef)
c.list.Remove(oldest)
}
}
}
}
func (c *Cache) Add(key string, value formulaResult) {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.disabled {
return
}
// If the cache has a positive limit and is full, evict the least recently used entry
if c.limit > 0 && c.list.Len() >= c.limit {
oldest := c.list.Back()
if oldest != nil {
// Get the key of the oldest entry
for k, v := range c.cache {
if v == oldest {
delete(c.cache, k) // Remove the oldest entry from the cache
break
}
}
c.list.Remove(oldest) // Remove the oldest entry from the list
}
}
// Add the new entry to the cache and the front of the list
elem := c.list.PushFront(value)
c.cache[key] = elem
}
func (c *Cache) Get(key string) (formulaResult, bool) {
c.mutex.RLock()
defer c.mutex.RUnlock()
if c.disabled {
return formulaResult{}, false
}
if elem, ok := c.cache[key]; ok {
// Move the accessed element to the front of the list (LRU)
c.list.MoveToFront(elem)
c.hits++
return elem.Value.(formulaResult), true
}
c.misses++
return formulaResult{}, false
}
func (c *Cache) DisableCache() {
c.mutex.Lock()
defer c.mutex.Unlock()
c.disabled = true
c.cache = make(map[string]*list.Element)
c.list = list.New()
}
func (c *Cache) Invalidate() {
c.mutex.Lock()
defer c.mutex.Unlock()
// If the cache is disabled, do nothing
if c.disabled {
return
}
// Clear the cache map and reset the LRU list
c.cache = make(map[string]*list.Element)
c.list = list.New()
}