Debian Codegraph @

policy/clock/clock.go

raw symbols SCIP
1 package clock
2
3 import (
4 "container/ring"
5
6 "github.com/Code-Hex/go-generics-cache/policy/internal/policyutil"
7 )
8
9 // Cache is used The clock cache replacement policy.
10 //
11 // The clock algorithm keeps a circular list of pages in memory, with
12 // the "hand" (iterator) pointing to the last examined page frame in the list.
13 // When a page fault occurs and no empty frames exist, then the R (referenced) bit
14 // is inspected at the hand's location. If R is 0, the new page is put in place of
15 // the page the "hand" points to, and the hand is advanced one position. Otherwise,
16 // the R bit is cleared, then the clock hand is incremented and the process is
17 // repeated until a page is replaced.
18 type Cache[K comparable, V any] struct {
19 items map[K]*ring.Ring
20 hand *ring.Ring
21 head *ring.Ring
22 capacity int
23 }
24
25 type entry[K comparable, V any] struct {
26 key K
27 val V
28 referenceCount int
29 }
30
31 // Option is an option for clock cache.
32 type Option func(*options)
33
34 type options struct {
35 capacity int
36 }
37
38 func newOptions() *options {
39 return &options{
40 capacity: 128,
41 }
42 }
43
44 // WithCapacity is an option to set cache capacity.
45 func WithCapacity(cap int) Option {
46 return func(o *options) {
47 o.capacity = cap
48 }
49 }
50
51 // NewCache creates a new non-thread safe clock cache whose capacity is the default size (128).
52 func NewCache[K comparable, V any](opts ...Option) *Cache[K, V] {
53 o := newOptions()
54 for _, optFunc := range opts {
55 optFunc(o)
56 }
57 r := ring.New(o.capacity)
58 return &Cache[K, V]{
59 items: make(map[K]*ring.Ring, o.capacity),
60 hand: r,
61 head: r,
62 capacity: o.capacity,
63 }
64 }
65
66 // Set sets any item to the cache. replacing any existing item.
67 //
68 // If value satisfies "interface{ GetReferenceCount() int }", the value of
69 // the GetReferenceCount() method is used to set the initial value of reference count.
70 func (c *Cache[K, V]) Set(key K, val V) {
71 if e, ok := c.items[key]; ok {
72 entry := e.Value.(*entry[K, V])
73 entry.referenceCount++
74 entry.val = val
75 return
76 }
77 c.evict()
78 c.hand.Value = &entry[K, V]{
79 key: key,
80 val: val,
81 referenceCount: policyutil.GetReferenceCount(val),
82 }
83 c.items[key] = c.hand
84 c.hand = c.hand.Next()
85 }
86
87 // Get looks up a key's value from the cache.
88 func (c *Cache[K, V]) Get(key K) (zero V, _ bool) {
89 e, ok := c.items[key]
90 if !ok {
91 return
92 }
93 entry := e.Value.(*entry[K, V])
94 entry.referenceCount++
95 return entry.val, true
96 }
97
98 func (c *Cache[K, V]) evict() {
99 for c.hand.Value != nil && c.hand.Value.(*entry[K, V]).referenceCount > 0 {
100 c.hand.Value.(*entry[K, V]).referenceCount--
101 c.hand = c.hand.Next()
102 }
103 if c.hand.Value != nil {
104 entry := c.hand.Value.(*entry[K, V])
105 delete(c.items, entry.key)
106 c.hand.Value = nil
107 }
108 }
109
110 // Keys returns the keys of the cache. the order as same as current ring order.
111 func (c *Cache[K, V]) Keys() []K {
112 keys := make([]K, 0, len(c.items))
113 r := c.head
114 if r.Value == nil {
115 return []K{}
116 }
117 // the first element
118 keys = append(keys, r.Value.(*entry[K, V]).key)
119
120 // iterating
121 for p := c.head.Next(); p != r; p = p.Next() {
122 if p.Value == nil {
123 continue
124 }
125 e := p.Value.(*entry[K, V])
126 keys = append(keys, e.key)
127 }
128 return keys
129 }
130
131 // Delete deletes the item with provided key from the cache.
132 func (c *Cache[K, V]) Delete(key K) {
133 if e, ok := c.items[key]; ok {
134 delete(c.items, key)
135 e.Value = nil
136 }
137 }
138
139 // Len returns the number of items in the cache.
140 func (c *Cache[K, V]) Len() int {
141 return len(c.items)
142 }