aboutsummaryrefslogtreecommitdiffstats
path: root/evict.go
diff options
context:
space:
mode:
authorMarc Pervaz Boocha <marcpervaz@qburst.com>2025-02-20 17:58:55 +0530
committerMarc Pervaz Boocha <marcpervaz@qburst.com>2025-02-20 17:58:55 +0530
commit2f95efc236520485c7cc1439acae24140657d76c (patch)
tree10eac60e3eec8e6b3bb9c27e131cd531363c39d6 /evict.go
parentAdded Eviction and Options setup (diff)
downloadcache-2f95efc236520485c7cc1439acae24140657d76c.tar
cache-2f95efc236520485c7cc1439acae24140657d76c.tar.gz
cache-2f95efc236520485c7cc1439acae24140657d76c.tar.bz2
cache-2f95efc236520485c7cc1439acae24140657d76c.tar.lz
cache-2f95efc236520485c7cc1439acae24140657d76c.tar.xz
cache-2f95efc236520485c7cc1439acae24140657d76c.tar.zst
cache-2f95efc236520485c7cc1439acae24140657d76c.zip
Add Tests and benchmarks
Diffstat (limited to 'evict.go')
-rw-r--r--evict.go22
1 files changed, 21 insertions, 1 deletions
diff --git a/evict.go b/evict.go
index dabbe6a..27b6d54 100644
--- a/evict.go
+++ b/evict.go
@@ -2,6 +2,7 @@ package cache
import "errors"
+// EvictionPolicyType defines the type of eviction policy.
type EvictionPolicyType int
const (
@@ -12,18 +13,21 @@ const (
PolicyLTR
)
+// evictionStrategies interface defines the methods for eviction strategies.
type evictionStrategies interface {
OnInsert(n *node)
OnAccess(n *node)
Evict() *node
}
+// evictionPolicy struct holds the eviction strategy and its type.
type evictionPolicy struct {
evictionStrategies
Type EvictionPolicyType
evict *node
}
+// SetPolicy sets the eviction policy based on the given type.
func (e *evictionPolicy) SetPolicy(y EvictionPolicyType) error {
store := map[EvictionPolicyType]func() evictionStrategies{
PolicyNone: func() evictionStrategies {
@@ -41,16 +45,18 @@ func (e *evictionPolicy) SetPolicy(y EvictionPolicyType) error {
}
factory, ok := store[y]
if !ok {
- return errors.New("invalid olicy")
+ return errors.New("invalid policy")
}
e.evictionStrategies = factory()
return nil
}
+// nonePolicy struct represents the no eviction policy.
type nonePolicy struct {
evict *node
}
+// OnInsert adds a node to the eviction list.
func (s nonePolicy) OnInsert(node *node) {
node.EvictPrev = s.evict
node.EvictNext = node.EvictPrev.EvictNext
@@ -58,17 +64,21 @@ func (s nonePolicy) OnInsert(node *node) {
node.EvictPrev.EvictNext = node
}
+// OnAccess is a no-op for nonePolicy.
func (nonePolicy) OnAccess(n *node) {
}
+// Evict returns nil for nonePolicy.
func (nonePolicy) Evict() *node {
return nil
}
+// fifoPolicy struct represents the First-In-First-Out eviction policy.
type fifoPolicy struct {
evict *node
}
+// OnInsert adds a node to the eviction list.
func (s fifoPolicy) OnInsert(node *node) {
node.EvictPrev = s.evict
node.EvictNext = node.EvictPrev.EvictNext
@@ -76,9 +86,11 @@ func (s fifoPolicy) OnInsert(node *node) {
node.EvictPrev.EvictNext = node
}
+// OnAccess is a no-op for fifoPolicy.
func (fifoPolicy) OnAccess(n *node) {
}
+// Evict returns the oldest node for fifoPolicy.
func (s fifoPolicy) Evict() *node {
if s.evict.EvictPrev != s.evict {
return s.evict.EvictPrev
@@ -87,10 +99,12 @@ func (s fifoPolicy) Evict() *node {
}
}
+// lruPolicy struct represents the Least Recently Used eviction policy.
type lruPolicy struct {
evict *node
}
+// OnInsert adds a node to the eviction list.
func (s lruPolicy) OnInsert(node *node) {
node.EvictPrev = s.evict
node.EvictNext = node.EvictPrev.EvictNext
@@ -98,6 +112,7 @@ func (s lruPolicy) OnInsert(node *node) {
node.EvictPrev.EvictNext = node
}
+// OnAccess moves the accessed node to the front of the eviction list.
func (s lruPolicy) OnAccess(node *node) {
node.EvictNext.EvictPrev = node.EvictPrev
node.EvictPrev.EvictNext = node.EvictNext
@@ -108,6 +123,7 @@ func (s lruPolicy) OnAccess(node *node) {
node.EvictPrev.EvictNext = node
}
+// Evict returns the least recently used node for lruPolicy.
func (s lruPolicy) Evict() *node {
if s.evict.EvictPrev != s.evict {
return s.evict.EvictPrev
@@ -116,10 +132,12 @@ func (s lruPolicy) Evict() *node {
}
}
+// lfuPolicy struct represents the Least Frequently Used eviction policy.
type lfuPolicy struct {
evict *node
}
+// OnInsert adds a node to the eviction list and initializes its access count.
func (s lfuPolicy) OnInsert(node *node) {
node.EvictPrev = s.evict
node.EvictNext = node.EvictPrev.EvictNext
@@ -128,6 +146,7 @@ func (s lfuPolicy) OnInsert(node *node) {
node.Access = 0
}
+// OnAccess increments the access count of the node and reorders the list.
func (s lfuPolicy) OnAccess(node *node) {
node.Access++
@@ -152,6 +171,7 @@ func (s lfuPolicy) OnAccess(node *node) {
node.EvictPrev.EvictNext = node
}
+// Evict returns the least frequently used node for LFU.
func (s lfuPolicy) Evict() *node {
if s.evict.EvictPrev != s.evict {
return s.evict.EvictPrev