diff options
author | Marc Pervaz Boocha <marcpervaz@qburst.com> | 2025-02-24 10:37:50 +0530 |
---|---|---|
committer | Marc Pervaz Boocha <marcpervaz@qburst.com> | 2025-02-27 13:37:32 +0530 |
commit | a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2 (patch) | |
tree | 5af6a2854644ac3dc44acdc9d604fa0eb576f120 /evict.go | |
parent | Added LTR Eviction (diff) | |
download | cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar.gz cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar.bz2 cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar.lz cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar.xz cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.tar.zst cache-a35f42bc2e4c29a1583fbc8900c14ec0ce5533d2.zip |
Added examples and documentation
Diffstat (limited to 'evict.go')
-rw-r--r-- | evict.go | 27 |
1 files changed, 26 insertions, 1 deletions
@@ -7,7 +7,8 @@ import ( // EvictionPolicyType defines the type of eviction policy. type EvictionPolicyType int -const ( +const ( + // PolicyNone indicates no eviction policy. PolicyNone EvictionPolicyType = iota PolicyFIFO PolicyLRU @@ -16,6 +17,7 @@ const ( ) // evictionStrategies interface defines the methods for eviction strategies. + // evictionStrategies interface defines the methods for eviction strategies. type evictionStrategies interface { OnInsert(n *node) OnUpdate(n *node) @@ -24,12 +26,14 @@ type evictionStrategies interface { } // evictionPolicy struct holds the eviction strategy and its type. + // evictionPolicy struct holds the eviction strategy and its type. type evictionPolicy struct { evictionStrategies Type EvictionPolicyType evict *node } + // pushEvict adds a node to the eviction list. func pushEvict(node *node, sentinnel *node) { node.EvictPrev = sentinnel node.EvictNext = node.EvictPrev.EvictNext @@ -38,6 +42,7 @@ func pushEvict(node *node, sentinnel *node) { } // SetPolicy sets the eviction policy based on the given type. + // 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 { @@ -65,25 +70,30 @@ func (e *evictionPolicy) SetPolicy(y EvictionPolicyType) error { } // fifoPolicy struct represents the First-In-First-Out eviction policy. + // fifoPolicy struct represents the First-In-First-Out eviction policy. type fifoPolicy struct { evict *node shouldEvict bool } // OnInsert adds a node to the eviction list. + // OnInsert adds a node to the eviction list. func (s fifoPolicy) OnInsert(node *node) { pushEvict(node, s.evict) } // OnAccess is a no-op for fifoPolicy. + // OnAccess is a no-op for fifoPolicy. func (fifoPolicy) OnAccess(n *node) { } // OnUpdate is a no-op for fifoPolicy. + // OnUpdate is a no-op for fifoPolicy. func (fifoPolicy) OnUpdate(n *node) { } // Evict returns the oldest node for fifoPolicy. + // Evict returns the oldest node for fifoPolicy. func (s fifoPolicy) Evict() *node { if s.shouldEvict && s.evict.EvictPrev != s.evict { return s.evict.EvictPrev @@ -93,20 +103,24 @@ func (s fifoPolicy) Evict() *node { } // lruPolicy struct represents the Least Recently Used eviction policy. + // lruPolicy struct represents the Least Recently Used eviction policy. type lruPolicy struct { evict *node } // OnInsert adds a node to the eviction list. + // OnInsert adds a node to the eviction list. func (s lruPolicy) OnInsert(node *node) { pushEvict(node, s.evict) } + // OnUpdate moves the accessed node to the front of the eviction list. func (s lruPolicy) OnUpdate(node *node) { s.OnAccess(node) } // OnAccess moves the accessed node to the front of the eviction list. + // 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 @@ -114,6 +128,7 @@ func (s lruPolicy) OnAccess(node *node) { } // Evict returns the least recently used node for lruPolicy. + // Evict returns the least recently used node for lruPolicy. func (s lruPolicy) Evict() *node { if s.evict.EvictPrev != s.evict { return s.evict.EvictPrev @@ -123,21 +138,25 @@ func (s lruPolicy) Evict() *node { } // lfuPolicy struct represents the Least Frequently Used eviction policy. + // 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. + // OnInsert adds a node to the eviction list and initializes its access count. func (s lfuPolicy) OnInsert(node *node) { pushEvict(node, s.evict) } // OnUpdate increments the access count of the node and reorders the list. + // OnUpdate increments the access count of the node and reorders the list. func (s lfuPolicy) OnUpdate(node *node) { s.OnAccess(node) } // OnAccess increments the access count of the node and reorders the list. + // OnAccess increments the access count of the node and reorders the list. func (s lfuPolicy) OnAccess(node *node) { node.Access++ @@ -163,6 +182,7 @@ func (s lfuPolicy) OnAccess(node *node) { } // Evict returns the least frequently used node for LFU. + // Evict returns the least frequently used node for LFU. func (s lfuPolicy) Evict() *node { if s.evict.EvictPrev != s.evict { return s.evict.EvictPrev @@ -172,6 +192,7 @@ func (s lfuPolicy) Evict() *node { } // ltrPolicy struct represents the Least Remaining Time eviction policy. + // ltrPolicy struct represents the Least Remaining Time eviction policy. type ltrPolicy struct { evict *node evictZero bool @@ -179,6 +200,7 @@ type ltrPolicy struct { // OnInsert adds a node to the eviction list based on its TTL (Time To Live). // It places the node in the correct position in the list based on TTL. + // OnInsert adds a node to the eviction list based on its TTL (Time To Live). func (s ltrPolicy) OnInsert(node *node) { pushEvict(node, s.evict) @@ -187,11 +209,13 @@ func (s ltrPolicy) OnInsert(node *node) { // OnAccess is a no-op for ltrPolicy. // It does not perform any action when a node is accessed. + // OnAccess is a no-op for ltrPolicy. func (s ltrPolicy) OnAccess(node *node) { } // OnUpdate updates the position of the node in the eviction list based on its TTL. // It reorders the list to maintain the correct order based on TTL. + // OnUpdate updates the position of the node in the eviction list based on its TTL. func (s ltrPolicy) OnUpdate(node *node) { if node.TTL() == 0 { return @@ -230,6 +254,7 @@ func (s ltrPolicy) OnUpdate(node *node) { // Evict returns the node with the least remaining time to live for ltrPolicy. // It returns the node at the end of the eviction list. + // Evict returns the node with the least remaining time to live for ltrPolicy. func (s ltrPolicy) Evict() *node { if s.evict.EvictPrev != s.evict && (s.evict.EvictPrev.TTL() != 0 || s.evictZero) { return s.evict.EvictPrev |