Repository navigation
Expand file tree
/
Copy pathcache.go
More file actions
64 lines (50 loc) · 1.45 KB
/
Copy pathcache.go
File metadata and controls
64 lines (50 loc) · 1.45 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
package pmtilr
import (
"context"
"strconv"
"github.com/maypok86/otter/v2"
)
type Cacher interface {
Get(ctx context.Context, key string) (Directory, bool)
Set(ctx context.Context, key string, value Directory) bool
Close()
Clear()
}
// buildCacheKey efficiently builds a singleflight key using a shared buffer pool
func buildCacheKey(etag string, offset, length uint64) string {
bufPtr, _ := keyBufPool.Get().(*[]byte) //nolint:errcheck
buf := (*bufPtr)[:0] // Reset length but keep capacity
defer keyBufPool.Put(bufPtr)
buf = append(buf, etag...)
buf = append(buf, ':')
buf = strconv.AppendUint(buf, offset, 10)
buf = append(buf, ':')
buf = strconv.AppendUint(buf, length, 10)
return string(buf)
}
const (
DefaultOtterMaximumSize = 10_000
DefaultOtterInitialCapacity = 1_000
)
func NewOtterCache() (Cacher, error) {
cache, err := otter.New(&otter.Options[string, Directory]{
MaximumSize: DefaultOtterMaximumSize,
InitialCapacity: DefaultOtterInitialCapacity,
})
if err != nil {
return nil, err
}
return &OtterCache{cache: cache}, nil
}
type OtterCache struct {
cache *otter.Cache[string, Directory]
}
func (oc *OtterCache) Get(_ context.Context, key string) (Directory, bool) {
return oc.cache.GetIfPresent(key)
}
func (oc *OtterCache) Set(_ context.Context, key string, value Directory) bool {
_, ok := oc.cache.Set(key, value)
return ok
}
func (oc *OtterCache) Close() {}
func (oc *OtterCache) Clear() {}