-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcaches.go
More file actions
62 lines (51 loc) · 1.31 KB
/
Copy pathcaches.go
File metadata and controls
62 lines (51 loc) · 1.31 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
package aklapi
import (
"github.com/phuslu/lru"
)
const defCacheSz = 100 // seems reasonable.
var (
addrCache = newLRUCache[string, *AddrResponse](defCacheSz)
rubbishCache = rubbishResultCache{lc: newLRUCache[string, *CollectionDayDetailResult](defCacheSz)}
)
type lruCache[K comparable, V any] struct {
cache *lru.LRUCache[K, V]
}
func newLRUCache[K comparable, V any](size int) *lruCache[K, V] {
return &lruCache[K, V]{
cache: lru.NewLRUCache[K, V](size),
}
}
func (c *lruCache[K, V]) Lookup(key K) (resp V, ok bool) {
var nothing V
if NoCache {
return nothing, false
}
return c.cache.Get(key)
}
func (c *lruCache[K, V]) Add(key K, value V) {
c.cache.Set(key, value)
}
func (c *lruCache[K, V]) Delete(key K) {
c.cache.Delete(key)
}
type rubbishResultCache struct {
lc *lruCache[string, *CollectionDayDetailResult]
}
func (c *rubbishResultCache) Lookup(searchText string) (result *CollectionDayDetailResult, ok bool) {
result, ok = c.lc.Lookup(searchText)
if !ok {
return nil, false
}
today := now()
for _, res := range result.Collections {
if today.After(res.Date) || res.Date.IsZero() {
// invalidate from cache.
c.lc.Delete(searchText)
return nil, false
}
}
return
}
func (c *rubbishResultCache) Add(searchText string, result *CollectionDayDetailResult) {
c.lc.Add(searchText, result)
}