-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheviction.go
More file actions
103 lines (86 loc) · 3.42 KB
/
eviction.go
File metadata and controls
103 lines (86 loc) · 3.42 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Package eviction implements various cache eviction algorithms.
package eviction
import (
"maps"
"github.com/hyp3rd/ewrap"
"github.com/hyp3rd/hypercache/internal/sentinel"
)
// IAlgorithm is the interface that must be implemented by eviction algorithms.
type IAlgorithm interface {
// Evict returns the next item to be evicted from the cache.
Evict() (string, bool)
// Set adds a new item to the cache with the given key.
Set(key string, value any)
// Get retrieves the item with the given key from the cache.
Get(key string) (any, bool)
// Delete removes the item with the given key from the cache.
Delete(key string)
}
// AlgorithmRegistry manages eviction algorithm constructors.
type AlgorithmRegistry struct {
algorithms map[string]func(capacity int) (IAlgorithm, error)
}
// getDefaultAlgorithms returns the default set of eviction algorithms.
func getDefaultAlgorithms() map[string]func(capacity int) (IAlgorithm, error) {
return map[string]func(capacity int) (IAlgorithm, error){
"lru": func(capacity int) (IAlgorithm, error) {
return NewLRUAlgorithm(capacity)
},
"clock": func(capacity int) (IAlgorithm, error) {
return NewClockAlgorithm(capacity)
},
"lfu": func(capacity int) (IAlgorithm, error) {
return NewLFUAlgorithm(capacity)
},
"cawolfu": func(capacity int) (IAlgorithm, error) {
return NewCAWOLFU(capacity)
},
}
}
// NewAlgorithmRegistry creates a new algorithm registry.
func NewAlgorithmRegistry() *AlgorithmRegistry {
registry := &AlgorithmRegistry{
algorithms: make(map[string]func(capacity int) (IAlgorithm, error)),
}
// Register the default algorithms
registry.RegisterMultiple(getDefaultAlgorithms())
return registry
}
// NewEmptyAlgorithmRegistry creates a new algorithm registry without default algorithms.
// This is useful for testing or when you want to register only specific algorithms.
func NewEmptyAlgorithmRegistry() *AlgorithmRegistry {
return &AlgorithmRegistry{
algorithms: make(map[string]func(capacity int) (IAlgorithm, error)),
}
}
// Register registers a new eviction algorithm with the given name.
func (r *AlgorithmRegistry) Register(name string, createFunc func(capacity int) (IAlgorithm, error)) {
r.algorithms[name] = createFunc
}
// RegisterMultiple registers a set of eviction algorithms.
func (r *AlgorithmRegistry) RegisterMultiple(algorithms map[string]func(capacity int) (IAlgorithm, error)) {
maps.Copy(r.algorithms, algorithms)
}
// NewAlgorithm creates a new eviction algorithm with the given capacity.
func (r *AlgorithmRegistry) NewAlgorithm(algorithmName string, capacity int) (IAlgorithm, error) {
// Check the parameters.
if algorithmName == "" {
return nil, ewrap.Wrap(sentinel.ErrParamCannotBeEmpty, "algorithmName")
}
if capacity < 0 {
return nil, ewrap.Wrapf(sentinel.ErrInvalidCapacity, "capacity")
}
createFunc, ok := r.algorithms[algorithmName]
if !ok {
return nil, ewrap.Wrap(sentinel.ErrAlgorithmNotFound, algorithmName)
}
return createFunc(capacity)
}
// NewEvictionAlgorithm creates a new eviction algorithm with the given capacity.
// It uses a new registry instance with default algorithms for each call.
// If the capacity is negative, it returns an error.
// The algorithmName parameter is used to select the eviction algorithm from the default algorithms.
func NewEvictionAlgorithm(algorithmName string, capacity int) (IAlgorithm, error) {
registry := NewAlgorithmRegistry()
return registry.NewAlgorithm(algorithmName, capacity)
}