-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper.go
More file actions
55 lines (46 loc) · 906 Bytes
/
wrapper.go
File metadata and controls
55 lines (46 loc) · 906 Bytes
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
package ascache
import (
"strings"
)
func NewCache[K comparable, V any](
cache Cacher[K, V],
policy PolicyType,
size int,
) *CacheWrapper[K, V] {
return &CacheWrapper[K, V]{
Cacher: cache,
policy: policy,
size: size,
stats: PolicyStats{},
}
}
type CacheWrapper[K comparable, V any] struct {
Cacher[K, V]
size int
policy PolicyType
stats PolicyStats
}
func (c *CacheWrapper[K, V]) Get(key K) (value V, ok bool) {
value, ok = c.Cacher.Get(key)
if ok {
c.stats.Hits++
} else {
c.stats.Misses++
}
return
}
func (c *CacheWrapper[K, V]) Cap() int {
return c.size
}
func (c *CacheWrapper[K, V]) Name() string {
return strings.ToLower(c.policy.String())
}
func (c *CacheWrapper[K, V]) GetType() PolicyType {
return c.policy
}
func (c *CacheWrapper[K, V]) GetStats() PolicyStats {
return c.stats
}
func (c *CacheWrapper[K, V]) ResetStats() {
c.stats = PolicyStats{}
}