Debian Codegraph @

example_test.go

raw symbols SCIP
1 package cache_test
2
3 import (
4 "context"
5 "fmt"
6 "time"
7
8 cache "github.com/Code-Hex/go-generics-cache"
9 "github.com/Code-Hex/go-generics-cache/policy/lfu"
10 "github.com/Code-Hex/go-generics-cache/policy/lru"
11 )
12
13 func ExampleCache() {
14 // use simple cache algorithm without options.
15 c := cache.New[string, int]()
16 c.Set("a", 1)
17 gota, aok := c.Get("a")
18 gotb, bok := c.Get("b")
19 fmt.Println(gota, aok)
20 fmt.Println(gotb, bok)
21 // Output:
22 // 1 true
23 // 0 false
24 }
25
26 func ExampleNewContext() {
27 ctx, cancel := context.WithCancel(context.Background())
28 defer cancel()
29
30 // use simple cache algorithm without options.
31 // an internal janitor will be stopped if specified the context is cancelled.
32 c := cache.NewContext(ctx, cache.WithJanitorInterval[string, int](3*time.Second))
33 c.Set("a", 1)
34 gota, aok := c.Get("a")
35 gotb, bok := c.Get("b")
36 fmt.Println(gota, aok)
37 fmt.Println(gotb, bok)
38 // Output:
39 // 1 true
40 // 0 false
41 }
42
43 func ExampleAsClock() {
44 // use clock cache algorithm.
45 c := cache.New(cache.AsClock[string, int]())
46 c.Set("a", 1)
47 gota, aok := c.Get("a")
48 gotb, bok := c.Get("b")
49 fmt.Println(gota, aok)
50 fmt.Println(gotb, bok)
51 // Output:
52 // 1 true
53 // 0 false
54 }
55
56 func ExampleWithExpiration() {
57 c := cache.New(cache.AsFIFO[string, int]())
58 exp := 250 * time.Millisecond
59 c.Set("a", 1, cache.WithExpiration(exp))
60
61 // check item is set.
62 gota, aok := c.Get("a")
63 fmt.Println(gota, aok)
64
65 // set again
66 c.Set("a", 2, cache.WithExpiration(exp))
67 gota2, aok2 := c.Get("a")
68 fmt.Println(gota2, aok2)
69
70 // waiting expiration.
71 time.Sleep(exp + 100*time.Millisecond) // + buffer
72
73 gota3, aok3 := c.Get("a") // expired
74 fmt.Println(gota3, aok3)
75 // Output:
76 // 1 true
77 // 2 true
78 // 0 false
79 }
80
81 func ExampleWithReferenceCount() {
82 c := cache.New(cache.AsLFU[string, int](lfu.WithCapacity(2)))
83
84 // set item with reference count
85 c.Set("a", 1, cache.WithReferenceCount(5))
86
87 // check item is set.
88 gota, aok := c.Get("a")
89 fmt.Println(gota, aok)
90
91 c.Set("b", 2)
92 c.Set("c", 3)
93
94 // evicted becauce the lowest reference count.
95 gotb, bok := c.Get("b")
96 fmt.Println(gotb, bok)
97
98 gotc, cok := c.Get("c")
99 fmt.Println(gotc, cok)
100
101 // Output:
102 // 1 true
103 // 0 false
104 // 3 true
105 }
106
107 func ExampleCache_Delete() {
108 c := cache.New(cache.AsMRU[string, int]())
109 c.Set("a", 1)
110 c.Delete("a")
111 gota, aok := c.Get("a")
112 fmt.Println(gota, aok)
113 // Output:
114 // 0 false
115 }
116
117 func ExampleCache_Keys() {
118 c := cache.New(cache.AsLFU[string, int]())
119 c.Set("a", 1)
120 c.Set("b", 1)
121 c.Set("c", 1)
122 fmt.Println(c.Keys())
123 // Output:
124 // [a b c]
125 }
126
127 func ExampleCache_Len() {
128 c := cache.New(cache.AsLFU[string, int]())
129 c.Set("a", 1)
130 c.Set("b", 1)
131 c.Set("c", 1)
132 fmt.Println(c.Len())
133 // Output:
134 // 3
135 }
136
137 func ExampleCache_Contains() {
138 c := cache.New(cache.AsLRU[string, int]())
139 c.Set("a", 1)
140 fmt.Println(c.Contains("a"))
141 fmt.Println(c.Contains("b"))
142 // Output:
143 // true
144 // false
145 }
146
147 func ExampleCache_GetOrSet() {
148 c := cache.New(cache.AsLRU[string, int](lru.WithCapacity(10)))
149 c.Set("a", 1)
150
151 val1, ok1 := c.GetOrSet("b", 2)
152 fmt.Println(val1, ok1)
153 val2, ok2 := c.GetOrSet("a", 3)
154 fmt.Println(val2, ok2)
155 // Output:
156 // 2 false
157 // 1 true
158 }
159
160 func ExampleNewNumber() {
161 nc := cache.NewNumber[string, int]()
162 nc.Set("a", 1)
163 nc.Set("b", 2, cache.WithExpiration(time.Minute))
164 av := nc.Increment("a", 1)
165 gota, aok := nc.Get("a")
166
167 bv := nc.Decrement("b", 1)
168 gotb, bok := nc.Get("b")
169
170 // not set keys
171 cv := nc.Increment("c", 100)
172 dv := nc.Decrement("d", 100)
173 fmt.Println(av, gota, aok)
174 fmt.Println(bv, gotb, bok)
175 fmt.Println(cv)
176 fmt.Println(dv)
177 // Output:
178 // 2 2 true
179 // 1 1 true
180 // 100
181 // -100
182 }