-
Notifications
You must be signed in to change notification settings - Fork 9
/
policy.go
88 lines (76 loc) · 2.19 KB
/
policy.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
package libcache
import (
"strconv"
"sync"
)
const (
// IDLE cache replacement policy.
IDLE ReplacementPolicy = iota + 1
// FIFO cache replacement policy.
FIFO
// LIFO cache replacement policy.
LIFO
// LRU cache replacement policy.
LRU
// LFU cache replacement policy.
LFU
// MRU cache replacement policy.
MRU
// ARC cache replacement policy.
ARC
max
)
var policies = make([]func(cap int) Cache, max)
// ReplacementPolicy identifies a cache replacement policy function that implemented in another package.
type ReplacementPolicy uint
// Register registers a function that returns a new cache instance,
// of the given cache replacement policy function.
// This is intended to be called from the init function,
// in packages that implement cache replacement policy function.
func (c ReplacementPolicy) Register(function func(cap int) Cache) {
if c <= 0 && c >= max { //nolint:staticcheck
panic("libcache: Register of unknown cache replacement policy function")
}
policies[c] = function
}
// Available reports whether the given cache replacement policy is linked into the binary.
func (c ReplacementPolicy) Available() bool {
return c > 0 && c < max && policies[c] != nil
}
// New returns a new thread safe cache.
// New panics if the cache replacement policy function is not linked into the binary.
func (c ReplacementPolicy) New(cap int) Cache {
cache := new(cache)
cache.mu = sync.Mutex{}
cache.unsafe = c.NewUnsafe(cap)
return cache
}
// NewUnsafe returns a new non-thread safe cache.
// NewUnsafe panics if the cache replacement policy function is not linked into the binary.
func (c ReplacementPolicy) NewUnsafe(cap int) Cache {
if !c.Available() {
panic("libcache: Requested cache replacement policy function #" + strconv.Itoa(int(c)) + " is unavailable")
}
return policies[c](cap)
}
// String returns string describes the cache replacement policy function.
func (c ReplacementPolicy) String() string {
switch c {
case IDLE:
return "IDLE"
case FIFO:
return "FIFO"
case LIFO:
return "LIFO"
case LRU:
return "LRU"
case LFU:
return "LFU"
case MRU:
return "MRU"
case ARC:
return "ARC"
default:
return "unknown cache replacement policy value " + strconv.Itoa(int(c))
}
}