Documentation
¶
Index ¶
- Constants
- Variables
- type ARC
- func (c *ARC[K, V]) Get(key K) (V, error)
- func (c *ARC[K, V]) GetALL(checkExpired bool) map[K]V
- func (c *ARC[K, V]) GetIFPresent(key K) (V, error)
- func (c *ARC[K, V]) GetIFPresentWithContext(ctx context.Context, key K) (V, error)
- func (c *ARC[K, V]) GetWithContext(ctx context.Context, key K) (V, error)
- func (c *ARC[K, V]) Has(key K) bool
- func (c *ARC[K, V]) Keys(checkExpired bool) []K
- func (c *ARC[K, V]) Len(checkExpired bool) int
- func (c *ARC[K, V]) Purge()
- func (c *ARC[K, V]) Remove(key K) bool
- func (c *ARC[K, V]) Set(key K, value V) error
- func (c *ARC[K, V]) SetWithExpire(key K, value V, expiration time.Duration) error
- type AddedFunc
- type Cache
- type CacheBuilder
- func (cb *CacheBuilder[K, V]) ARC() *CacheBuilder[K, V]
- func (cb *CacheBuilder[K, V]) AddedFunc(addedFunc AddedFunc[K, V]) *CacheBuilder[K, V]
- func (cb *CacheBuilder[K, V]) Build() Cache[K, V]
- func (cb *CacheBuilder[K, V]) Clock(clock Clock) *CacheBuilder[K, V]
- func (cb *CacheBuilder[K, V]) DeserializeFunc(deserializeFunc DeserializeFunc[K, V]) *CacheBuilder[K, V]
- func (cb *CacheBuilder[K, V]) EvictType(tp string) *CacheBuilder[K, V]
- func (cb *CacheBuilder[K, V]) EvictedFunc(evictedFunc EvictedFunc[K, V]) *CacheBuilder[K, V]
- func (cb *CacheBuilder[K, V]) Expiration(expiration time.Duration) *CacheBuilder[K, V]
- func (cb *CacheBuilder[K, V]) LFU() *CacheBuilder[K, V]
- func (cb *CacheBuilder[K, V]) LRU() *CacheBuilder[K, V]
- func (cb *CacheBuilder[K, V]) LoaderExpireFunc(loaderExpireFunc LoaderExpireFunc[K, V]) *CacheBuilder[K, V]
- func (cb *CacheBuilder[K, V]) LoaderFunc(loaderFunc LoaderFunc[K, V]) *CacheBuilder[K, V]
- func (cb *CacheBuilder[K, V]) PurgeVisitorFunc(purgeVisitorFunc PurgeVisitorFunc[K, V]) *CacheBuilder[K, V]
- func (cb *CacheBuilder[K, V]) SerializeFunc(serializeFunc SerializeFunc[K, V]) *CacheBuilder[K, V]
- func (cb *CacheBuilder[K, V]) Simple() *CacheBuilder[K, V]
- type Clock
- type DeserializeFunc
- type EvictedFunc
- type FakeClock
- type Group
- type LFUCache
- func (c *LFUCache[K, V]) Get(key K) (V, error)
- func (c *LFUCache[K, V]) GetALL(checkExpired bool) map[K]V
- func (c *LFUCache[K, V]) GetIFPresent(key K) (V, error)
- func (c *LFUCache[K, V]) GetIFPresentWithContext(ctx context.Context, key K) (V, error)
- func (c *LFUCache[K, V]) GetWithContext(ctx context.Context, key K) (V, error)
- func (c *LFUCache[K, V]) Has(key K) bool
- func (c *LFUCache[K, V]) Keys(checkExpired bool) []K
- func (c *LFUCache[K, V]) Len(checkExpired bool) int
- func (c *LFUCache[K, V]) Purge()
- func (c *LFUCache[K, V]) Remove(key K) bool
- func (c *LFUCache[K, V]) Set(key K, value V) error
- func (c *LFUCache[K, V]) SetWithExpire(key K, value V, expiration time.Duration) error
- type LRUCache
- func (c *LRUCache[K, V]) Get(key K) (V, error)
- func (c *LRUCache[K, V]) GetALL(checkExpired bool) map[K]V
- func (c *LRUCache[K, V]) GetIFPresent(key K) (V, error)
- func (c *LRUCache[K, V]) GetIFPresentWithContext(ctx context.Context, key K) (V, error)
- func (c *LRUCache[K, V]) GetWithContext(ctx context.Context, key K) (V, error)
- func (c *LRUCache[K, V]) Has(key K) bool
- func (c *LRUCache[K, V]) Keys(checkExpired bool) []K
- func (c *LRUCache[K, V]) Len(checkExpired bool) int
- func (c *LRUCache[K, V]) Purge()
- func (c *LRUCache[K, V]) Remove(key K) bool
- func (c *LRUCache[K, V]) Set(key K, value V) error
- func (c *LRUCache[K, V]) SetWithExpire(key K, value V, expiration time.Duration) error
- type LoaderExpireFunc
- type LoaderFunc
- type PurgeVisitorFunc
- type RealClock
- type SerializeFunc
- type SimpleCache
- func (c *SimpleCache[K, V]) Get(key K) (V, error)
- func (c *SimpleCache[K, V]) GetALL(checkExpired bool) map[K]V
- func (c *SimpleCache[K, V]) GetIFPresent(key K) (V, error)
- func (c *SimpleCache[K, V]) GetIFPresentWithContext(ctx context.Context, key K) (V, error)
- func (c *SimpleCache[K, V]) GetWithContext(ctx context.Context, key K) (V, error)
- func (c *SimpleCache[K, V]) Has(key K) bool
- func (c *SimpleCache[K, V]) Keys(checkExpired bool) []K
- func (c *SimpleCache[K, V]) Len(checkExpired bool) int
- func (c *SimpleCache[K, V]) Purge()
- func (c *SimpleCache[K, V]) Remove(key K) bool
- func (c *SimpleCache[K, V]) Set(key K, value V) error
- func (c *SimpleCache[K, V]) SetWithExpire(key K, value V, expiration time.Duration) error
Constants ¶
const ( TYPE_SIMPLE = "simple" TYPE_LRU = "lru" TYPE_LFU = "lfu" TYPE_ARC = "arc" )
Variables ¶
var KeyNotFoundError = errors.New("key not found")
Functions ¶
This section is empty.
Types ¶
type ARC ¶
type ARC[K comparable, V any] struct { // contains filtered or unexported fields }
ARC Constantly balances between LRU and LFU, to improve the combined result.
func (*ARC[K, V]) Get ¶
Get a value from cache pool using key if it exists. If not exists and it has LoaderFunc, it will generate the value using you have specified LoaderFunc method returns value.
func (*ARC[K, V]) GetIFPresent ¶
GetIFPresent gets a value from cache pool using key if it exists. If it does not exists key, returns KeyNotFoundError. And send a request which refresh value for specified key if cache object has LoaderFunc.
func (*ARC[K, V]) GetIFPresentWithContext ¶ added in v1.1.0
func (*ARC[K, V]) GetWithContext ¶ added in v1.1.0
type AddedFunc ¶
type AddedFunc[K comparable, V any] func(K, V)
type Cache ¶
type Cache[K comparable, V any] interface { // Set inserts or updates the specified key-value pair. Set(key K, value V) error // SetWithExpire inserts or updates the specified key-value pair with an // expiration time. SetWithExpire(key K, value V, expiration time.Duration) error // Get returns the value for the specified key if it is present in the // cache. If the key is not present in the cache and the cache has // LoaderFunc, invoke the `LoaderFunc` function and inserts the key-value // pair in the cache. If the key is not present in the cache and the cache // does not have a LoaderFunc, return KeyNotFoundError. Get(K) (V, error) // GetIFPresent returns the value for the specified key if it is present in // the cache. Return KeyNotFoundError if the key is not present. GetIFPresent(K) (V, error) GetWithContext(context.Context, K) (V, error) GetIFPresentWithContext(context.Context, K) (V, error) // GetALL returns a map containing all key-value pairs in the cache. GetALL(checkExpired bool) map[K]V // Remove removes the specified key from the cache if the key is present. // Returns true if the key was present and the key has been deleted. Remove(key K) bool // Purge removes all key-value pairs from the cache. Purge() // Keys returns a slice containing all keys in the cache. Keys(checkExpired bool) []K // Len returns the number of items in the cache. Len(checkExpired bool) int // Has returns true if the key exists in the cache. Has(key K) bool // contains filtered or unexported methods }
type CacheBuilder ¶
type CacheBuilder[K comparable, V any] struct { // contains filtered or unexported fields }
func New ¶
func New[K comparable, V any](size int) *CacheBuilder[K, V]
func (*CacheBuilder[K, V]) ARC ¶
func (cb *CacheBuilder[K, V]) ARC() *CacheBuilder[K, V]
func (*CacheBuilder[K, V]) AddedFunc ¶
func (cb *CacheBuilder[K, V]) AddedFunc(addedFunc AddedFunc[K, V]) *CacheBuilder[K, V]
func (*CacheBuilder[K, V]) Build ¶
func (cb *CacheBuilder[K, V]) Build() Cache[K, V]
func (*CacheBuilder[K, V]) Clock ¶
func (cb *CacheBuilder[K, V]) Clock(clock Clock) *CacheBuilder[K, V]
func (*CacheBuilder[K, V]) DeserializeFunc ¶
func (cb *CacheBuilder[K, V]) DeserializeFunc(deserializeFunc DeserializeFunc[K, V]) *CacheBuilder[K, V]
func (*CacheBuilder[K, V]) EvictType ¶
func (cb *CacheBuilder[K, V]) EvictType(tp string) *CacheBuilder[K, V]
func (*CacheBuilder[K, V]) EvictedFunc ¶
func (cb *CacheBuilder[K, V]) EvictedFunc(evictedFunc EvictedFunc[K, V]) *CacheBuilder[K, V]
func (*CacheBuilder[K, V]) Expiration ¶
func (cb *CacheBuilder[K, V]) Expiration(expiration time.Duration) *CacheBuilder[K, V]
func (*CacheBuilder[K, V]) LFU ¶
func (cb *CacheBuilder[K, V]) LFU() *CacheBuilder[K, V]
func (*CacheBuilder[K, V]) LRU ¶
func (cb *CacheBuilder[K, V]) LRU() *CacheBuilder[K, V]
func (*CacheBuilder[K, V]) LoaderExpireFunc ¶
func (cb *CacheBuilder[K, V]) LoaderExpireFunc(loaderExpireFunc LoaderExpireFunc[K, V]) *CacheBuilder[K, V]
LoaderExpireFunc Set a loader function with expiration. loaderExpireFunc: create a new value with this function if cached value is expired. If nil returned instead of time.Duration from loaderExpireFunc than value will never expire.
func (*CacheBuilder[K, V]) LoaderFunc ¶
func (cb *CacheBuilder[K, V]) LoaderFunc(loaderFunc LoaderFunc[K, V]) *CacheBuilder[K, V]
LoaderFunc Set a loader function. loaderFunc: create a new value with this function if cached value is expired.
func (*CacheBuilder[K, V]) PurgeVisitorFunc ¶
func (cb *CacheBuilder[K, V]) PurgeVisitorFunc(purgeVisitorFunc PurgeVisitorFunc[K, V]) *CacheBuilder[K, V]
func (*CacheBuilder[K, V]) SerializeFunc ¶
func (cb *CacheBuilder[K, V]) SerializeFunc(serializeFunc SerializeFunc[K, V]) *CacheBuilder[K, V]
func (*CacheBuilder[K, V]) Simple ¶
func (cb *CacheBuilder[K, V]) Simple() *CacheBuilder[K, V]
type DeserializeFunc ¶
type DeserializeFunc[K comparable, V any] func(K, V) (V, error)
type EvictedFunc ¶
type EvictedFunc[K comparable, V any] func(K, V)
type FakeClock ¶
func NewFakeClock ¶
func NewFakeClock() FakeClock
type Group ¶
type Group[K comparable, V any] struct { // contains filtered or unexported fields }
Group represents a class of work and forms a namespace in which units of work can be executed with duplicate suppression.
type LFUCache ¶
type LFUCache[K comparable, V any] struct { // contains filtered or unexported fields }
LFUCache Discards the least frequently used items first.
func (*LFUCache[K, V]) Get ¶
Get a value from cache pool using key if it exists. If it does not exists key and has LoaderFunc, generate a value using `LoaderFunc` method returns value.
func (*LFUCache[K, V]) GetIFPresent ¶
GetIFPresent gets a value from cache pool using key if it exists. If it does not exists key, returns KeyNotFoundError. And send a request which refresh value for specified key if cache object has LoaderFunc.
func (*LFUCache[K, V]) GetIFPresentWithContext ¶ added in v1.1.0
func (*LFUCache[K, V]) GetWithContext ¶ added in v1.1.0
type LRUCache ¶
type LRUCache[K comparable, V any] struct { // contains filtered or unexported fields }
LRUCache Discards the least recently used items first.
func (*LRUCache[K, V]) Get ¶
Get a value from cache pool using key if it exists. If it does not exists key and has LoaderFunc, generate a value using `LoaderFunc` method returns value.
func (*LRUCache[K, V]) GetIFPresent ¶
GetIFPresent gets a value from cache pool using key if it exists. If it does not exists key, returns KeyNotFoundError. And send a request which refresh value for specified key if cache object has LoaderFunc.
func (*LRUCache[K, V]) GetIFPresentWithContext ¶ added in v1.1.0
func (*LRUCache[K, V]) GetWithContext ¶ added in v1.1.0
type LoaderExpireFunc ¶
type LoaderFunc ¶
type LoaderFunc[K comparable, V any] func(context.Context, K) (V, error)
type PurgeVisitorFunc ¶
type PurgeVisitorFunc[K comparable, V any] func(K, V)
type SerializeFunc ¶
type SerializeFunc[K comparable, V any] func(K, V) (V, error)
type SimpleCache ¶
type SimpleCache[K comparable, V any] struct { // contains filtered or unexported fields }
SimpleCache has no clear priority for evict cache. It depends on key-value map order.
func (*SimpleCache[K, V]) Get ¶
func (c *SimpleCache[K, V]) Get(key K) (V, error)
Get a value from cache pool using key if it exists. If it does not exists key and has LoaderFunc, generate a value using `LoaderFunc` method returns value.
func (*SimpleCache[K, V]) GetALL ¶
func (c *SimpleCache[K, V]) GetALL(checkExpired bool) map[K]V
GetALL returns all key-value pairs in the cache.
func (*SimpleCache[K, V]) GetIFPresent ¶
func (c *SimpleCache[K, V]) GetIFPresent(key K) (V, error)
GetIFPresent gets a value from cache pool using key if it exists. If it does not exists key, returns KeyNotFoundError. And send a request which refresh value for specified key if cache object has LoaderFunc.
func (*SimpleCache[K, V]) GetIFPresentWithContext ¶ added in v1.1.0
func (c *SimpleCache[K, V]) GetIFPresentWithContext(ctx context.Context, key K) (V, error)
func (*SimpleCache[K, V]) GetWithContext ¶ added in v1.1.0
func (c *SimpleCache[K, V]) GetWithContext(ctx context.Context, key K) (V, error)
func (*SimpleCache[K, V]) Has ¶
func (c *SimpleCache[K, V]) Has(key K) bool
Has checks if key exists in cache
func (*SimpleCache[K, V]) Keys ¶
func (c *SimpleCache[K, V]) Keys(checkExpired bool) []K
Keys returns a slice of the keys in the cache.
func (*SimpleCache[K, V]) Len ¶
func (c *SimpleCache[K, V]) Len(checkExpired bool) int
Len returns the number of items in the cache.
func (*SimpleCache[K, V]) Remove ¶
func (c *SimpleCache[K, V]) Remove(key K) bool
Remove removes the provided key from the cache.
func (*SimpleCache[K, V]) Set ¶
func (c *SimpleCache[K, V]) Set(key K, value V) error
Set a new key-value pair
func (*SimpleCache[K, V]) SetWithExpire ¶
func (c *SimpleCache[K, V]) SetWithExpire(key K, value V, expiration time.Duration) error
Set a new key-value pair with an expiration time