@@ -165,25 +165,27 @@ func (cache *Cache) WithEvictionPolicy(policy EvictionPolicy) *Cache {
165165// is nil or not.
166166//
167167// If set to true (default):
168- // cache := gocache.NewCache().WithForceNilInterfaceOnNilPointer(true)
169- // cache.Set("key", (*Struct)(nil))
170- // value, _ := cache.Get("key")
171- // // the following returns true, because the interface{} was forcefully set to nil
172- // if value == nil {}
173- // // the following will panic, because the value has been casted to its type (which is nil)
174- // if value.(*Struct) == nil {}
168+ //
169+ // cache := gocache.NewCache().WithForceNilInterfaceOnNilPointer(true)
170+ // cache.Set("key", (*Struct)(nil))
171+ // value, _ := cache.Get("key")
172+ // // the following returns true, because the interface{} (any) was forcefully set to nil
173+ // if value == nil {}
174+ // // the following will panic, because the value has been casted to its type (which is nil)
175+ // if value.(*Struct) == nil {}
175176//
176177// If set to false:
177- // cache := gocache.NewCache().WithForceNilInterfaceOnNilPointer(false)
178- // cache.Set("key", (*Struct)(nil))
179- // value, _ := cache.Get("key")
180- // // the following returns false, because the interface{} returned has a non-nil type (*Struct)
181- // if value == nil {}
182- // // the following returns true, because the value has been casted to its type
183- // if value.(*Struct) == nil {}
178+ //
179+ // cache := gocache.NewCache().WithForceNilInterfaceOnNilPointer(false)
180+ // cache.Set("key", (*Struct)(nil))
181+ // value, _ := cache.Get("key")
182+ // // the following returns false, because the interface{} (any) returned has a non-nil type (*Struct)
183+ // if value == nil {}
184+ // // the following returns true, because the value has been cast to its type
185+ // if value.(*Struct) == nil {}
184186//
185187// In other words, if set to true, you do not need to cast the value returned from the cache to
186- // to check if the value is nil.
188+ // check if the value is nil.
187189//
188190// Defaults to true
189191func (cache * Cache ) WithForceNilInterfaceOnNilPointer (forceNilInterfaceOnNilPointer bool ) * Cache {
@@ -194,8 +196,8 @@ func (cache *Cache) WithForceNilInterfaceOnNilPointer(forceNilInterfaceOnNilPoin
194196// NewCache creates a new Cache
195197//
196198// Should be used in conjunction with Cache.WithMaxSize, Cache.WithMaxMemoryUsage and/or Cache.WithEvictionPolicy
197- // gocache.NewCache().WithMaxSize(10000).WithEvictionPolicy(gocache.LeastRecentlyUsed)
198199//
200+ // gocache.NewCache().WithMaxSize(10000).WithEvictionPolicy(gocache.LeastRecentlyUsed)
199201func NewCache () * Cache {
200202 return & Cache {
201203 maxSize : DefaultMaxSize ,
@@ -209,15 +211,15 @@ func NewCache() *Cache {
209211}
210212
211213// Set creates or updates a key with a given value
212- func (cache * Cache ) Set (key string , value interface {} ) {
214+ func (cache * Cache ) Set (key string , value any ) {
213215 cache .SetWithTTL (key , value , NoExpiration )
214216}
215217
216218// SetWithTTL creates or updates a key with a given value and sets an expiration time (-1 is NoExpiration)
217219//
218220// The TTL provided must be greater than 0, or NoExpiration (-1). If a negative value that isn't -1 (NoExpiration) is
219221// provided, the entry will not be created if the key doesn't exist
220- func (cache * Cache ) SetWithTTL (key string , value interface {} , ttl time.Duration ) {
222+ func (cache * Cache ) SetWithTTL (key string , value any , ttl time.Duration ) {
221223 // An interface is only nil if both its value and its type are nil, however, passing a nil pointer as an interface{}
222224 // means that the interface itself is not nil, because the interface value is nil but not the type.
223225 if cache .forceNilInterfaceOnNilPointer {
@@ -298,7 +300,7 @@ func (cache *Cache) SetWithTTL(key string, value interface{}, ttl time.Duration)
298300}
299301
300302// SetAll creates or updates multiple values
301- func (cache * Cache ) SetAll (entries map [string ]interface {} ) {
303+ func (cache * Cache ) SetAll (entries map [string ]any ) {
302304 for key , value := range entries {
303305 cache .SetWithTTL (key , value , NoExpiration )
304306 }
@@ -307,7 +309,7 @@ func (cache *Cache) SetAll(entries map[string]interface{}) {
307309// Get retrieves an entry using the key passed as parameter
308310// If there is no such entry, the value returned will be nil and the boolean will be false
309311// If there is an entry, the value returned will be the value cached and the boolean will be true
310- func (cache * Cache ) Get (key string ) (interface {} , bool ) {
312+ func (cache * Cache ) Get (key string ) (any , bool ) {
311313 cache .mutex .Lock ()
312314 entry , ok := cache .get (key )
313315 if ! ok {
@@ -337,7 +339,7 @@ func (cache *Cache) Get(key string) (interface{}, bool) {
337339
338340// GetValue retrieves an entry using the key passed as parameter
339341// Unlike Get, this function only returns the value
340- func (cache * Cache ) GetValue (key string ) interface {} {
342+ func (cache * Cache ) GetValue (key string ) any {
341343 value , _ := cache .Get (key )
342344 return value
343345}
@@ -346,8 +348,8 @@ func (cache *Cache) GetValue(key string) interface{} {
346348// All keys are returned in the map, regardless of whether they exist or not, however, entries that do not exist in the
347349// cache will return nil, meaning that there is no way of determining whether a key genuinely has the value nil, or
348350// whether it doesn't exist in the cache using only this function.
349- func (cache * Cache ) GetByKeys (keys []string ) map [string ]interface {} {
350- entries := make (map [string ]interface {} )
351+ func (cache * Cache ) GetByKeys (keys []string ) map [string ]any {
352+ entries := make (map [string ]any )
351353 for _ , key := range keys {
352354 entries [key ], _ = cache .Get (key )
353355 }
@@ -365,8 +367,8 @@ func (cache *Cache) GetByKeys(keys []string) map[string]interface{} {
365367// GetKeysByPattern is a good alternative if you want to retrieve entries that you do not have the key for, as it only
366368// retrieves the keys and does not trigger active eviction and has a parameter for setting a limit to the number of keys
367369// you wish to retrieve.
368- func (cache * Cache ) GetAll () map [string ]interface {} {
369- entries := make (map [string ]interface {} )
370+ func (cache * Cache ) GetAll () map [string ]any {
371+ entries := make (map [string ]any )
370372 cache .mutex .Lock ()
371373 for key , entry := range cache .entries {
372374 if entry .Expired () {
@@ -385,8 +387,9 @@ func (cache *Cache) GetAll() map[string]interface{} {
385387// If the limit is above 0, the search will stop once the specified number of matching keys have been found.
386388//
387389// e.g.
388- // cache.GetKeysByPattern("*some*", 0) will return all keys containing "some" in them
389- // cache.GetKeysByPattern("*some*", 5) will return 5 keys (or less) containing "some" in them
390+ //
391+ // cache.GetKeysByPattern("*some*", 0) will return all keys containing "some" in them
392+ // cache.GetKeysByPattern("*some*", 5) will return 5 keys (or less) containing "some" in them
390393//
391394// Note that GetKeysByPattern does not trigger active evictions, nor does it count as accessing the entry (if LRU).
392395// The reason for that behavior is that these two (active eviction and access) only applies when you access the value
0 commit comments