-
Notifications
You must be signed in to change notification settings - Fork 2
/
bucketcache.go
57 lines (45 loc) · 1.1 KB
/
bucketcache.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
package bucketcache
import (
"github.com/elastic/go-freelru"
)
type BucketCache[K comparable, V any] struct {
cache freelru.Cache[K, []V]
maxBucketSize int
}
func New[K comparable, V any](cacheSize uint32, maxBucketSize uint32, hash freelru.HashKeyCallback[K]) (*BucketCache[K, V], error) {
cache, err := freelru.NewSynced[K, []V](cacheSize, hash)
if err != nil {
return nil, err
}
return &BucketCache[K, V]{
cache: cache,
maxBucketSize: int(maxBucketSize),
}, nil
}
func (b *BucketCache[K, V]) AddToBucket(k K, val V) bool {
return b.addToCache(k, val, false)
}
func (b *BucketCache[K, V]) ForceAddToBucket(k K, val V) {
b.addToCache(k, val, true)
}
func (b *BucketCache[K, V]) addToCache(k K, val V, force bool) bool {
bucket, found := b.cache.Get(k)
if !found {
b.cache.Add(k, []V{val})
return true
}
if len(bucket) >= b.maxBucketSize {
if force {
bucket[0] = val
b.cache.Add(k, bucket)
return true
}
return false
}
b.cache.Add(k, append(bucket, val))
return true
}
func (b *BucketCache[K, V]) GetBucket(k K) []V {
res, _ := b.cache.Get(k)
return res
}