Debian Codegraph @

gocache.go

raw symbols SCIP
1 package gocachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/
2
3 import (
4 "errorsscip-go gomod github.com/golang/go/src go1.24.4 errors/"
5 "reflectscip-go gomod github.com/golang/go/src go1.24.4 reflect/"
6 "syncscip-go gomod github.com/golang/go/src go1.24.4 sync/"
7 "timescip-go gomod github.com/golang/go/src go1.24.4 time/"
8 )
9
10 var (
11 Debugscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Debug. = false
12 )
13
14 const (
15 // NoMaxSize means that the cache has no maximum number of entries in the cache
16 // Setting Cache.maxSize to this value also means there will be no eviction
17 NoMaxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoMaxSize. = 0
18
19 // NoMaxMemoryUsage means that the cache has no maximum number of entries in the cache
20 NoMaxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoMaxMemoryUsage. = 0
21
22 // DefaultMaxSize is the max size set if no max size is specified
23 DefaultMaxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/DefaultMaxSize. = 100000
24
25 // NoExpiration is the value that must be used as TTL to specify that the given key should never expire
26 NoExpirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoExpiration. = -1
27
28 Kilobytescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Kilobyte. = 1024
29 Megabytescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Megabyte. = 1024 * Kilobytescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Kilobyte.
30 Gigabytescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Gigabyte. = 1024 * Megabytescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Megabyte.
31 )
32
33 var (
34 ErrKeyDoesNotExistscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/ErrKeyDoesNotExist. = errorsscip-go gomod github.com/golang/go/src go1.24.4 errors/.Newscip-go gomod github.com/golang/go/src go1.24.4 errors/New().("key does not exist") // Returned when a cache key does not exist
35 ErrKeyHasNoExpirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/ErrKeyHasNoExpiration. = errorsscip-go gomod github.com/golang/go/src go1.24.4 errors/.Newscip-go gomod github.com/golang/go/src go1.24.4 errors/New().("key has no expiration") // Returned when a cache key has no expiration
36 ErrJanitorAlreadyRunningscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/ErrJanitorAlreadyRunning. = errorsscip-go gomod github.com/golang/go/src go1.24.4 errors/.Newscip-go gomod github.com/golang/go/src go1.24.4 errors/New().("janitor is already running") // Returned when the janitor has already been started
37 )
38
39 // Cache is the core struct of gocache which contains the data as well as all relevant configuration fields
40 //
41 // Do not instantiate this struct directly, use NewCache instead
42 type Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache# struct {
43 // maxSize is the maximum amount of entries that can be in the cache at any given time
44 // By default, this is set to DefaultMaxSize
45 maxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxSize. int
46
47 // maxMemoryUsage is the maximum amount of memory that can be taken up by the cache at any time
48 // By default, this is set to NoMaxMemoryUsage, meaning that the default behavior is to not evict
49 // based on maximum memory usage
50 maxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxMemoryUsage. int
51
52 // evictionPolicy is the eviction policy
53 evictionPolicyscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#evictionPolicy. EvictionPolicyscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/EvictionPolicy#
54
55 // defaultTTL is the default TTL for each entry
56 // Defaults to NoExpiration
57 defaultTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#defaultTTL. timescip-go gomod github.com/golang/go/src go1.24.4 time/.Durationscip-go gomod github.com/golang/go/src go1.24.4 time/Duration#
58
59 // stats is the object that contains cache statistics/metrics
60 statsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stats. *Statisticsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#
61
62 // entries is the content of the cache
63 entriesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#entries. map[string]*Entryscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#
64
65 // mutex is the lock for making concurrent operations on the cache
66 mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex. syncscip-go gomod github.com/golang/go/src go1.24.4 sync/.RWMutexscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#
67
68 // head is the cache entry at the head of the cache
69 headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head. *Entryscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#
70
71 // tail is the last cache node and also the next entry that will be evicted
72 tailscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#tail. *Entryscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#
73
74 // stopJanitor is the channel used to stop the janitor
75 stopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stopJanitor. chan bool
76
77 // memoryUsage is the approximate memory usage of the cache (dataset only) in bytes
78 memoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#memoryUsage. int
79
80 // forceNilInterfaceOnNilPointer determines whether all Set-like functions should set a value as nil if the
81 // interface passed has a nil value but not a nil type.
82 //
83 // By default, interfaces are only nil when both their type and value is nil.
84 // This means that when you pass a pointer to a nil value, the type of the interface
85 // will still show as nil, which means that if you don't cast the interface after
86 // retrieving it, a nil check will return that the value is not false.
87 forceNilInterfaceOnNilPointerscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#forceNilInterfaceOnNilPointer. bool
88 }
89
90 // MaxSize returns the maximum amount of keys that can be present in the cache before
91 // new entries trigger the eviction of the tail
92 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) MaxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#MaxSize().() int {
93 return cache.maxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxSize.
94 }
95
96 // MaxMemoryUsage returns the configured maxMemoryUsage of the cache
97 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) MaxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#MaxMemoryUsage().() int {
98 return cache.maxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxMemoryUsage.
99 }
100
101 // EvictionPolicy returns the EvictionPolicy of the Cache
102 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) EvictionPolicyscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#EvictionPolicy().() EvictionPolicyscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/EvictionPolicy# {
103 return cache.evictionPolicyscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#evictionPolicy.
104 }
105
106 // Stats returns statistics from the cache
107 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) Statsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Stats().() Statisticsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics# {
108 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..RLockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#RLock().()
109 stats := Statisticsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#{
110 EvictedKeysscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#EvictedKeys.: cache.statsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stats..EvictedKeysscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#EvictedKeys.,
111 ExpiredKeysscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#ExpiredKeys.: cache.statsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stats..ExpiredKeysscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#ExpiredKeys.,
112 Hitsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#Hits.: cache.statsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stats..Hitsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#Hits.,
113 Missesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#Misses.: cache.statsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stats..Missesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#Misses.,
114 }
115 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..RUnlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#RUnlock().()
116 return stats
117 }
118
119 // MemoryUsage returns the current memory usage of the cache's dataset in bytes
120 // If MaxMemoryUsage is set to NoMaxMemoryUsage, this will return 0
121 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) MemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#MemoryUsage().() int {
122 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..RLockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#RLock().()
123 defer cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..RUnlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#RUnlock().()
124 return cache.memoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#memoryUsage.
125 }
126
127 // WithMaxSize sets the maximum amount of entries that can be in the cache at any given time
128 // A maxSize of 0 or less means infinite
129 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) WithMaxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#WithMaxSize().(maxSize int) *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache# {
130 if maxSize < 0 {
131 maxSize = NoMaxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoMaxSize.
132 }
133 if maxSize != NoMaxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoMaxSize. && cache.Countscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Count().() == 0 {
134 cache.entriesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#entries. = make(map[string]*Entryscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#, maxSize)
135 }
136 cache.maxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxSize. = maxSize
137 return cache
138 }
139
140 // WithMaxMemoryUsage sets the maximum amount of memory that can be used by the cache at any given time
141 //
142 // NOTE: This is approximate.
143 //
144 // Setting this to NoMaxMemoryUsage will disable eviction by memory usage
145 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) WithMaxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#WithMaxMemoryUsage().(maxMemoryUsageInBytes int) *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache# {
146 if maxMemoryUsageInBytes < 0 {
147 maxMemoryUsageInBytes = NoMaxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoMaxMemoryUsage.
148 }
149 cache.maxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxMemoryUsage. = maxMemoryUsageInBytes
150 return cache
151 }
152
153 // WithEvictionPolicy sets eviction algorithm.
154 //
155 // Defaults to FirstInFirstOut (FIFO)
156 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) WithEvictionPolicyscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#WithEvictionPolicy().(policy EvictionPolicyscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/EvictionPolicy#) *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache# {
157 cache.evictionPolicyscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#evictionPolicy. = policy
158 return cache
159 }
160
161 // WithDefaultTTL sets the default TTL for each entry (unless a different TTL is specified using SetWithTTL or SetAllWithTTL)
162 //
163 // Defaults to NoExpiration (-1)
164 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) WithDefaultTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#WithDefaultTTL().(ttl timescip-go gomod github.com/golang/go/src go1.24.4 time/.Durationscip-go gomod github.com/golang/go/src go1.24.4 time/Duration#) *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache# {
165 if ttl > 1 {
166 cache.defaultTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#defaultTTL. = ttl
167 }
168 return cache
169 }
170
171 // WithForceNilInterfaceOnNilPointer sets whether all Set-like functions should set a value as nil if the
172 // interface passed has a nil value but not a nil type.
173 //
174 // In Go, an interface is only nil if both its type and value are nil, which means that a nil pointer
175 // (e.g. (*Struct)(nil)) will retain its attribution to the type, and the unmodified value returned from
176 // Cache.Get, for instance, would return false when compared with nil if this option is set to false.
177 //
178 // We can bypass this by detecting if the interface's value is nil and setting it to nil rather than
179 // a nil pointer, which will make the value returned from Cache.Get return true when compared with nil.
180 // This is exactly what passing true to WithForceNilInterfaceOnNilPointer does, and it's also the default behavior.
181 //
182 // Alternatively, you may pass false to WithForceNilInterfaceOnNilPointer, which will mean that you'll have
183 // to cast the value returned from Cache.Get to its original type to check for whether the pointer returned
184 // is nil or not.
185 //
186 // If set to true (default):
187 //
188 // cache := gocache.NewCache().WithForceNilInterfaceOnNilPointer(true)
189 // cache.Set("key", (*Struct)(nil))
190 // value, _ := cache.Get("key")
191 // // the following returns true, because the interface{} (any) was forcefully set to nil
192 // if value == nil {}
193 // // the following will panic, because the value has been casted to its type (which is nil)
194 // if value.(*Struct) == nil {}
195 //
196 // If set to false:
197 //
198 // cache := gocache.NewCache().WithForceNilInterfaceOnNilPointer(false)
199 // cache.Set("key", (*Struct)(nil))
200 // value, _ := cache.Get("key")
201 // // the following returns false, because the interface{} (any) returned has a non-nil type (*Struct)
202 // if value == nil {}
203 // // the following returns true, because the value has been cast to its type
204 // if value.(*Struct) == nil {}
205 //
206 // In other words, if set to true, you do not need to cast the value returned from the cache to
207 // check if the value is nil.
208 //
209 // Defaults to true
210 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) WithForceNilInterfaceOnNilPointerscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#WithForceNilInterfaceOnNilPointer().(forceNilInterfaceOnNilPointer bool) *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache# {
211 cache.forceNilInterfaceOnNilPointerscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#forceNilInterfaceOnNilPointer. = forceNilInterfaceOnNilPointer
212 return cache
213 }
214
215 // NewCache creates a new Cache
216 //
217 // Should be used in conjunction with Cache.WithMaxSize, Cache.WithMaxMemoryUsage and/or Cache.WithEvictionPolicy
218 //
219 // gocache.NewCache().WithMaxSize(10000).WithEvictionPolicy(gocache.LeastRecentlyUsed)
220 func NewCachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NewCache().() *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache# {
221 return &Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#{
222 maxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxSize.: DefaultMaxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/DefaultMaxSize.,
223 evictionPolicyscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#evictionPolicy.: FirstInFirstOutscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/FirstInFirstOut.,
224 defaultTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#defaultTTL.: NoExpirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoExpiration.,
225 statsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stats.: &Statisticsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#{},
226 entriesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#entries.: make(map[string]*Entryscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#),
227 mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex.: syncscip-go gomod github.com/golang/go/src go1.24.4 sync/.RWMutexscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#{},
228 stopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stopJanitor.: nil,
229 forceNilInterfaceOnNilPointerscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#forceNilInterfaceOnNilPointer.: true,
230 }
231 }
232
233 // Set creates or updates a key with a given value
234 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) Setscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Set().(key string, value any) {
235 cache.SetWithTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#SetWithTTL().(key, value, cache.defaultTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#defaultTTL.)
236 }
237
238 // SetWithTTL creates or updates a key with a given value and sets an expiration time (-1 is NoExpiration)
239 //
240 // The TTL provided must be greater than 0, or NoExpiration (-1). If a negative value that isn't -1 (NoExpiration) is
241 // provided, the entry will not be created if the key doesn't exist
242 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) SetWithTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#SetWithTTL().(key string, value any, ttl timescip-go gomod github.com/golang/go/src go1.24.4 time/.Durationscip-go gomod github.com/golang/go/src go1.24.4 time/Duration#) {
243 // An interface is only nil if both its value and its type are nil, however, passing a nil pointer as an interface{}
244 // means that the interface itself is not nil, because the interface value is nil but not the type.
245 if cache.forceNilInterfaceOnNilPointerscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#forceNilInterfaceOnNilPointer. {
246 if value != nil && (reflectscip-go gomod github.com/golang/go/src go1.24.4 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.24.4 reflect/ValueOf().(value).Kindscip-go gomod github.com/golang/go/src go1.24.4 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.24.4 reflect/.Ptrscip-go gomod github.com/golang/go/src go1.24.4 reflect/Ptr. && reflectscip-go gomod github.com/golang/go/src go1.24.4 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.24.4 reflect/ValueOf().(value).IsNilscip-go gomod github.com/golang/go/src go1.24.4 reflect/Value#IsNil().()) {
247 value = nil
248 }
249 }
250 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Lockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Lock().()
251 entry, ok := cache.getscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#get().(key)
252 if !ok {
253 // A negative TTL that isn't -1 (NoExpiration) or 0 is an entry that will expire instantly,
254 // so might as well just not create it in the first place
255 if ttl != NoExpirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoExpiration. && ttl < 1 {
256 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Unlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Unlock().()
257 return
258 }
259 // Cache entry doesn't exist, so we have to create a new one
260 entry = &Entryscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#{
261 Keyscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Key.: key,
262 RelevantTimestampscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#RelevantTimestamp.: timescip-go gomod github.com/golang/go/src go1.24.4 time/.Nowscip-go gomod github.com/golang/go/src go1.24.4 time/Now().(),
263 nextscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#next.: cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head.,
264 }
265 entry.Valuescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Value. = value
266 if cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head. == nil {
267 cache.tailscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#tail. = entry
268 } else {
269 cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head..previousscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#previous. = entry
270 }
271 cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head. = entry
272 cache.entriesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#entries.[key] = entry
273 if cache.maxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxMemoryUsage. != NoMaxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoMaxMemoryUsage. {
274 cache.memoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#memoryUsage. += entry.SizeInBytesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#SizeInBytes().()
275 }
276 } else {
277 // A negative TTL that isn't -1 (NoExpiration) or 0 is an entry that will expire instantly,
278 // so might as well just delete it immediately instead of updating it
279 if ttl != NoExpirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoExpiration. && ttl < 1 {
280 cache.deletescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#delete().(key)
281 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Unlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Unlock().()
282 return
283 }
284 if cache.maxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxMemoryUsage. != NoMaxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoMaxMemoryUsage. {
285 // Subtract the old entry from the cache's memoryUsage
286 cache.memoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#memoryUsage. -= entry.SizeInBytesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#SizeInBytes().()
287 }
288 entry.Valuescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Value. = value
289 entry.RelevantTimestampscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#RelevantTimestamp. = timescip-go gomod github.com/golang/go/src go1.24.4 time/.Nowscip-go gomod github.com/golang/go/src go1.24.4 time/Now().()
290 if cache.maxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxMemoryUsage. != NoMaxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoMaxMemoryUsage. {
291 // Add the memory usage of the new entry to the cache's memoryUsage
292 cache.memoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#memoryUsage. += entry.SizeInBytesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#SizeInBytes().()
293 }
294 // Because we just updated the entry, we need to move it back to HEAD
295 cache.moveExistingEntryToHeadscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#moveExistingEntryToHead().(entry)
296 }
297 if ttl != NoExpirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoExpiration. {
298 entry.Expirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Expiration. = timescip-go gomod github.com/golang/go/src go1.24.4 time/.Nowscip-go gomod github.com/golang/go/src go1.24.4 time/Now().().Addscip-go gomod github.com/golang/go/src go1.24.4 time/Time#Add().(ttl).UnixNanoscip-go gomod github.com/golang/go/src go1.24.4 time/Time#UnixNano().()
299 } else {
300 entry.Expirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Expiration. = NoExpirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoExpiration.
301 }
302 // If the cache doesn't have a maxSize/maxMemoryUsage, then there's no point
303 // checking if we need to evict an entry, so we'll just return now
304 if cache.maxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxSize. == NoMaxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoMaxSize. && cache.maxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxMemoryUsage. == NoMaxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoMaxMemoryUsage. {
305 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Unlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Unlock().()
306 return
307 }
308 // If there's a maxSize and the cache has more entries than the maxSize, evict
309 if cache.maxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxSize. != NoMaxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoMaxSize. && len(cache.entriesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#entries.) > cache.maxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxSize. {
310 cache.evictscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#evict().()
311 }
312 // If there's a maxMemoryUsage and the memoryUsage is above the maxMemoryUsage, evict
313 if cache.maxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxMemoryUsage. != NoMaxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoMaxMemoryUsage. && cache.memoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#memoryUsage. > cache.maxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxMemoryUsage. {
314 for cache.memoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#memoryUsage. > cache.maxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxMemoryUsage. && len(cache.entriesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#entries.) > 0 {
315 cache.evictscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#evict().()
316 }
317 }
318 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Unlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Unlock().()
319 }
320
321 // SetAll creates or updates multiple values
322 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) SetAllscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#SetAll().(entries map[string]any) {
323 cache.SetAllWithTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#SetAllWithTTL().(entries, cache.defaultTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#defaultTTL.)
324 }
325
326 // SetAllWithTTL creates or updates multiple values
327 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) SetAllWithTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#SetAllWithTTL().(entries map[string]any, ttl timescip-go gomod github.com/golang/go/src go1.24.4 time/.Durationscip-go gomod github.com/golang/go/src go1.24.4 time/Duration#) {
328 for key, value := range entries {
329 cache.SetWithTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#SetWithTTL().(key, value, ttl)
330 }
331 }
332
333 // Get retrieves an entry using the key passed as parameter
334 // If there is no such entry, the value returned will be nil and the boolean will be false
335 // If there is an entry, the value returned will be the value cached and the boolean will be true
336 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) Getscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Get().(key string) (any, bool) {
337 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Lockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Lock().()
338 entry, ok := cache.getscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#get().(key)
339 if !ok {
340 cache.statsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stats..Missesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#Misses.++
341 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Unlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Unlock().()
342 return nil, false
343 }
344 if entry.Expiredscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Expired().() {
345 cache.statsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stats..ExpiredKeysscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#ExpiredKeys.++
346 cache.deletescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#delete().(key)
347 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Unlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Unlock().()
348 return nil, false
349 }
350 cache.statsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stats..Hitsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#Hits.++
351 if cache.evictionPolicyscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#evictionPolicy. == LeastRecentlyUsedscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/LeastRecentlyUsed. {
352 entry.Accessedscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Accessed().()
353 if cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head. == entry {
354 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Unlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Unlock().()
355 return entry.Valuescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Value., true
356 }
357 // Because the eviction policy is LRU, we need to move the entry back to HEAD
358 cache.moveExistingEntryToHeadscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#moveExistingEntryToHead().(entry)
359 }
360 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Unlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Unlock().()
361 return entry.Valuescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Value., true
362 }
363
364 // GetValue retrieves an entry using the key passed as parameter
365 // Unlike Get, this function only returns the value
366 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) GetValuescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#GetValue().(key string) any {
367 value, _ := cache.Getscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Get().(key)
368 return value
369 }
370
371 // GetByKeys retrieves multiple entries using the keys passed as parameter
372 // All keys are returned in the map, regardless of whether they exist or not, however, entries that do not exist in the
373 // cache will return nil, meaning that there is no way of determining whether a key genuinely has the value nil, or
374 // whether it doesn't exist in the cache using only this function.
375 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) GetByKeysscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#GetByKeys().(keys []string) map[string]any {
376 entries := make(map[string]any)
377 for _, key := range keys {
378 entries[key], _ = cache.Getscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Get().(key)
379 }
380 return entries
381 }
382
383 // GetAll retrieves all cache entries
384 //
385 // If the eviction policy is LeastRecentlyUsed, note that unlike Get and GetByKeys, this does not update the last access
386 // timestamp. The reason for this is that since all cache entries will be accessed, updating the last access timestamp
387 // would provide very little benefit while harming the ability to accurately determine the next key that will be evicted
388 //
389 // You should probably avoid using this if you have a lot of entries.
390 //
391 // GetKeysByPattern is a good alternative if you want to retrieve entries that you do not have the key for, as it only
392 // retrieves the keys and does not trigger active eviction and has a parameter for setting a limit to the number of keys
393 // you wish to retrieve.
394 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) GetAllscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#GetAll().() map[string]any {
395 entries := make(map[string]any)
396 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Lockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Lock().()
397 for key, entry := range cache.entriesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#entries. {
398 if entry.Expiredscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Expired().() {
399 cache.deletescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#delete().(key)
400 continue
401 }
402 entries[key] = entry.Valuescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Value.
403 }
404 cache.statsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stats..Hitsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#Hits. += uint64(len(entries))
405 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Unlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Unlock().()
406 return entries
407 }
408
409 // GetKeysByPattern retrieves a slice of keys that match a given pattern
410 // If the limit is set to 0, the entire cache will be searched for matching keys.
411 // If the limit is above 0, the search will stop once the specified number of matching keys have been found.
412 //
413 // e.g.
414 //
415 // cache.GetKeysByPattern("*some*", 0) will return all keys containing "some" in them
416 // cache.GetKeysByPattern("*some*", 5) will return 5 keys (or less) containing "some" in them
417 //
418 // Note that GetKeysByPattern does not trigger active evictions, nor does it count as accessing the entry (if LRU).
419 // The reason for that behavior is that these two (active eviction and access) only applies when you access the value
420 // of the cache entry, and this function only returns the keys.
421 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) GetKeysByPatternscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#GetKeysByPattern().(pattern string, limit int) []string {
422 var matchingKeys []string
423 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Lockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Lock().()
424 for key, value := range cache.entriesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#entries. {
425 if value.Expiredscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Expired().() {
426 continue
427 }
428 if MatchPatternscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/MatchPattern().(pattern, key) {
429 matchingKeys = append(matchingKeys, key)
430 if limit > 0 && len(matchingKeys) >= limit {
431 break
432 }
433 }
434 }
435 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Unlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Unlock().()
436 return matchingKeys
437 }
438
439 // Delete removes a key from the cache
440 //
441 // Returns false if the key did not exist.
442 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) Deletescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Delete().(key string) bool {
443 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Lockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Lock().()
444 ok := cache.deletescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#delete().(key)
445 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Unlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Unlock().()
446 return ok
447 }
448
449 // DeleteAll deletes multiple entries based on the keys passed as parameter
450 //
451 // Returns the number of keys deleted
452 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) DeleteAllscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#DeleteAll().(keys []string) int {
453 numberOfKeysDeleted := 0
454 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Lockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Lock().()
455 for _, key := range keys {
456 if cache.deletescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#delete().(key) {
457 numberOfKeysDeleted++
458 }
459 }
460 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Unlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Unlock().()
461 return numberOfKeysDeleted
462 }
463
464 // DeleteKeysByPattern deletes all entries matching a given key pattern and returns the number of entries deleted.
465 //
466 // Note that DeleteKeysByPattern does not trigger active evictions, nor does it count as accessing the entry (if LRU).
467 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) DeleteKeysByPatternscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#DeleteKeysByPattern().(pattern string) int {
468 return cache.DeleteAllscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#DeleteAll().(cache.GetKeysByPatternscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#GetKeysByPattern().(pattern, 0))
469 }
470
471 // Count returns the total amount of entries in the cache, regardless of whether they're expired or not
472 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) Countscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Count().() int {
473 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..RLockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#RLock().()
474 count := len(cache.entriesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#entries.)
475 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..RUnlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#RUnlock().()
476 return count
477 }
478
479 // Clear deletes all entries from the cache
480 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) Clearscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Clear().() {
481 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Lockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Lock().()
482 cache.entriesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#entries. = make(map[string]*Entryscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#)
483 cache.memoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#memoryUsage. = 0
484 cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head. = nil
485 cache.tailscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#tail. = nil
486 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Unlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Unlock().()
487 }
488
489 // TTL returns the time until the cache entry specified by the key passed as parameter
490 // will be deleted.
491 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) TTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#TTL().(key string) (timescip-go gomod github.com/golang/go/src go1.24.4 time/.Durationscip-go gomod github.com/golang/go/src go1.24.4 time/Duration#, error) {
492 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..RLockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#RLock().()
493 entry, ok := cache.getscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#get().(key)
494 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..RUnlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#RUnlock().()
495 if !ok {
496 return 0, ErrKeyDoesNotExistscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/ErrKeyDoesNotExist.
497 }
498 if entry.Expirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Expiration. == NoExpirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoExpiration. {
499 return 0, ErrKeyHasNoExpirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/ErrKeyHasNoExpiration.
500 }
501 timeUntilExpiration := timescip-go gomod github.com/golang/go/src go1.24.4 time/.Untilscip-go gomod github.com/golang/go/src go1.24.4 time/Until().(timescip-go gomod github.com/golang/go/src go1.24.4 time/.Unixscip-go gomod github.com/golang/go/src go1.24.4 time/Unix().(0, entry.Expirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Expiration.))
502 if timeUntilExpiration < 0 {
503 // The key has already expired but hasn't been deleted yet.
504 // From the client's perspective, this means that the cache entry doesn't exist
505 return 0, ErrKeyDoesNotExistscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/ErrKeyDoesNotExist.
506 }
507 return timeUntilExpiration, nil
508 }
509
510 // Expire sets a key's expiration time
511 //
512 // A TTL of -1 means that the key will never expire
513 // A TTL of 0 means that the key will expire immediately
514 // If using LRU, note that this does not reset the position of the key
515 //
516 // Returns true if the cache key exists and has had its expiration time altered
517 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) Expirescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Expire().(key string, ttl timescip-go gomod github.com/golang/go/src go1.24.4 time/.Durationscip-go gomod github.com/golang/go/src go1.24.4 time/Duration#) bool {
518 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Lockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Lock().()
519 entry, ok := cache.getscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#get().(key)
520 if !ok || entry.Expiredscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Expired().() {
521 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Unlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Unlock().()
522 return false
523 }
524 if ttl != NoExpirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoExpiration. {
525 entry.Expirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Expiration. = timescip-go gomod github.com/golang/go/src go1.24.4 time/.Nowscip-go gomod github.com/golang/go/src go1.24.4 time/Now().().Addscip-go gomod github.com/golang/go/src go1.24.4 time/Time#Add().(ttl).UnixNanoscip-go gomod github.com/golang/go/src go1.24.4 time/Time#UnixNano().()
526 } else {
527 entry.Expirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Expiration. = NoExpirationscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoExpiration.
528 }
529 cache.mutexscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#mutex..Unlockscip-go gomod github.com/golang/go/src go1.24.4 sync/RWMutex#Unlock().()
530 return true
531 }
532
533 // get retrieves an entry using the key passed as parameter, but unlike Get, it doesn't update the access time or
534 // move the position of the entry to the head
535 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) getscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#get().(key string) (*Entryscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#, bool) {
536 entry, ok := cache.entriesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#entries.[key]
537 return entry, ok
538 }
539
540 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) deletescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#delete().(key string) bool {
541 entry, ok := cache.entriesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#entries.[key]
542 if ok {
543 if cache.maxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxMemoryUsage. != NoMaxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoMaxMemoryUsage. {
544 cache.memoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#memoryUsage. -= entry.SizeInBytesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#SizeInBytes().()
545 }
546 cache.removeExistingEntryReferencesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#removeExistingEntryReferences().(entry)
547 delete(cache.entriesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#entries., key)
548 }
549 return ok
550 }
551
552 // moveExistingEntryToHead replaces the current cache head for an existing entry
553 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) moveExistingEntryToHeadscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#moveExistingEntryToHead().(entry *Entryscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#) {
554 if !(entry == cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head. && entry == cache.tailscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#tail.) {
555 cache.removeExistingEntryReferencesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#removeExistingEntryReferences().(entry)
556 }
557 if entry != cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head. {
558 entry.nextscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#next. = cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head.
559 entry.previousscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#previous. = nil
560 if cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head. != nil {
561 cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head..previousscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#previous. = entry
562 }
563 cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head. = entry
564 }
565 }
566
567 // removeExistingEntryReferences modifies the next and previous reference of an existing entry and re-links
568 // the next and previous entry accordingly, as well as the cache head or/and the cache tail if necessary.
569 // Note that it does not remove the entry from the cache, only the references.
570 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) removeExistingEntryReferencesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#removeExistingEntryReferences().(entry *Entryscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#) {
571 if cache.tailscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#tail. == entry && cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head. == entry {
572 cache.tailscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#tail. = nil
573 cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head. = nil
574 } else if cache.tailscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#tail. == entry {
575 cache.tailscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#tail. = cache.tailscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#tail..previousscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#previous.
576 } else if cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head. == entry {
577 cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head. = cache.headscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#head..nextscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#next.
578 }
579 if entry.previousscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#previous. != nil {
580 entry.previousscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#previous..nextscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#next. = entry.nextscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#next.
581 }
582 if entry.nextscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#next. != nil {
583 entry.nextscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#next..previousscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#previous. = entry.previousscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#previous.
584 }
585 entry.nextscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#next. = nil
586 entry.previousscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#previous. = nil
587 }
588
589 // evict removes the tail from the cache
590 func (cache *Cachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#) evictscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#evict().() {
591 if cache.tailscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#tail. == nil || len(cache.entriesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#entries.) == 0 {
592 return
593 }
594 if cache.tailscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#tail. != nil {
595 oldTail := cache.tailscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#tail.
596 cache.removeExistingEntryReferencesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#removeExistingEntryReferences().(oldTail)
597 delete(cache.entriesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#entries., oldTail.Keyscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#Key.)
598 if cache.maxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#maxMemoryUsage. != NoMaxMemoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NoMaxMemoryUsage. {
599 cache.memoryUsagescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#memoryUsage. -= oldTail.SizeInBytesscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Entry#SizeInBytes().()
600 }
601 cache.statsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stats..EvictedKeysscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#EvictedKeys.++
602 }
603 }