cache2

package
v1.2.96 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package cache2 Cache item old implementation Based on https://github.com/ReneKroon/ttlcache

Index

Constants

View Source
const (
	// ItemNotExpire Will avoid the cachedItem being expired by TTL, but can still be exired by callback etc.
	ItemNotExpire time.Duration = -1

	// ItemExpireWithGlobalTTL will use the global TTL when set.
	ItemExpireWithGlobalTTL time.Duration = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache2

type Cache2 struct {
	// contains filtered or unexported fields
}

Cache2 is a synchronized map of items that can auto-expire once stale

func NewTtlCache2

func NewTtlCache2() *Cache2

NewTtlCache2 is a helper to create instance of the Cache struct

func (*Cache2) Close

func (cache *Cache2) Close()

Close calls Purge, and then stops the goroutine that does ttl checking, for a clean shutdown. The cache is no longer cleaning up after the first call to Close, repeated calls are safe though.

func (*Cache2) Count

func (cache *Cache2) Count() int

Count returns the number of items in the cache

func (*Cache2) Delete

func (cache *Cache2) Delete(key any)

Delete deletes the key value.

func (*Cache2) Get

func (cache *Cache2) Get(key string) (any, bool)

Get is a thread-safe way to lookup items Every lookup, also touches the cachedItem, hence extending it's life

func (*Cache2) Load

func (cache *Cache2) Load(key any) (any, bool)

Load returns key value.

func (*Cache2) Purge

func (cache *Cache2) Purge()

Purge will remove all entries

func (*Cache2) Range

func (cache *Cache2) Range(cb func(k, v any) bool)

Iterate over all items in the cache

func (*Cache2) Remove

func (cache *Cache2) Remove(key string) bool

func (*Cache2) Set

func (cache *Cache2) Set(key string, data any)

Set is a thread-safe way to add new items to the map

func (*Cache2) SetCheckExpirationCallback

func (cache *Cache2) SetCheckExpirationCallback(callback checkExpireCallback)

SetCheckExpirationCallback sets a callback that will be called when an cachedItem is about to expire in order to allow external code to decide whether the cachedItem expires or remains for another TTL cycle

func (*Cache2) SetExpirationCallback

func (cache *Cache2) SetExpirationCallback(callback expireCallback)

SetExpirationCallback sets a callback that will be called when an cachedItem expires

func (*Cache2) SetNewItemCallback

func (cache *Cache2) SetNewItemCallback(callback expireCallback)

SetNewItemCallback sets a callback that will be called when a new cachedItem is added to the cache

func (*Cache2) SetTTL

func (cache *Cache2) SetTTL(ttl time.Duration)

func (*Cache2) SetWithTTL

func (cache *Cache2) SetWithTTL(key string, data any, ttl time.Duration)

SetWithTTL is a thread-safe way to add new items to the map with individual ttl

func (*Cache2) SkipTtlExtensionOnHit

func (cache *Cache2) SkipTtlExtensionOnHit(value bool)

SkipTtlExtensionOnHit allows the user to change the cache behaviour. When this flag is set to true it will no longer extend TTL of items when they are retrieved using Get, or when their expiration condition is evaluated using SetCheckExpirationCallback.

func (*Cache2) Store

func (cache *Cache2) Store(key any, value any)

Store sets the key value.

func (*Cache2) StoreWithTTL

func (cache *Cache2) StoreWithTTL(key any, value any, ttl time.Duration)

StoreWithTTL sets the key value with TTL overrides the default.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL