Documentation ¶
Index ¶
- Variables
- type Cache
- func (c *Cache[T]) CountPredicate(pred Predicate) (int, error)
- func (c *Cache[T]) CountRegExp(pattern string) (int, error)
- func (c *Cache[T]) Delete(key string) error
- func (c *Cache[T]) DeletePredicate(pred Predicate) (int, error)
- func (c *Cache[T]) DeleteRegExp(pattern string) (int, error)
- func (c *Cache[T]) DeleteWithPrefix(prefix string) (int, error)
- func (c *Cache[T]) Get(key string) (*T, error)
- func (c *Cache[T]) GetIndirect(key string, linkResolver func(*T) string) (*T, error)
- func (c *Cache[T]) GetOrCompute(key string, evaluator func() (*T, error)) (*T, error)
- func (c *Cache[T]) GetOrComputeEx(key string, evaluator func() (*T, error), validator func(*T) bool, ...) (*T, error)
- func (c *Cache[T]) Keys() ([]string, error)
- func (c *Cache[T]) Peek(key string) (*T, error)
- func (c *Cache[T]) Purge() error
- func (c *Cache[T]) Set(key string, value *T) error
- func (c *Cache[T]) SetIndirect(key string, value *T, linkResolver func(*T) string, linkGenerator func(*T) *T) error
- type CacheEngine
- type CacheWithSubcache
- func (cs *CacheWithSubcache[T]) Delete(key string) error
- func (cs *CacheWithSubcache[T]) Get(key string) (interface{}, error)
- func (cs *CacheWithSubcache[T]) Keys() ([]string, error)
- func (cs *CacheWithSubcache[T]) Peek(key string) (interface{}, error)
- func (cs *CacheWithSubcache[T]) Purge() error
- func (cs *CacheWithSubcache[T]) Set(key string, value interface{}) error
- type DummyLogger
- type LRUCache
- func (lc *LRUCache) Delete(key string) error
- func (lc *LRUCache) Get(key string) (v interface{}, err error)
- func (lc *LRUCache) Keys() ([]string, error)
- func (lc *LRUCache) Peek(key string) (v interface{}, err error)
- func (lc *LRUCache) Purge() error
- func (lc *LRUCache) Set(key string, value interface{}) (err error)
- type Logger
- type Predicate
- type RedisCache
- func (rc *RedisCache) Delete(key string) error
- func (rc *RedisCache) Get(key string) (v interface{}, err error)
- func (rc *RedisCache) Keys() ([]string, error)
- func (rc *RedisCache) Peek(key string) (interface{}, error)
- func (rc *RedisCache) Purge() error
- func (rc *RedisCache) Set(key string, value interface{}) (err error)
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("key not found") ErrWrongDataType = errors.New("data in wrong format") )
Errors
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[T any] struct { // contains filtered or unexported fields }
Cache is an implementation of a cache (key-value store). It needs to be provided with cache engine.
func MakeCache ¶
func MakeCache[T any](engine CacheEngine) *Cache[T]
MakeCache creates cache with provided engine
func (*Cache[T]) CountPredicate ¶ added in v1.0.2
CountPredicate counts cache keys satisfying the given predicate
func (*Cache[T]) CountRegExp ¶ added in v1.0.1
CountRegExp counts all keys matching the supplied regexp
func (*Cache[T]) DeletePredicate ¶ added in v1.0.2
DeletePredicate deletes all keys matching the supplied predicate, returns number of deleted keys
func (*Cache[T]) DeleteRegExp ¶
DeleteRegExp deletes all keys matching the supplied regexp, returns number of deleted keys
func (*Cache[T]) DeleteWithPrefix ¶
DeleteWithPrefix removes all keys that start with given prefix, returns number of deleted keys
func (*Cache[T]) GetIndirect ¶ added in v1.0.3
GetIndirect gets a key value following any intermediary links
func (*Cache[T]) GetOrCompute ¶
GetOrCompute tries to get value from cache. If not found, it computes the value using provided evaluator function and stores it into cache. In case of other errors the value is evaluated but not stored in the cache.
func (*Cache[T]) GetOrComputeEx ¶ added in v1.0.3
func (c *Cache[T]) GetOrComputeEx(key string, evaluator func() (*T, error), validator func(*T) bool, linkResolver func(*T) string, linkGenerator func(*T) *T, writeApprover func(*T) bool) (*T, error)
GetOrComputeEx tries to get value from cache. If not found, it computes the value using provided evaluator function and stores it into cache. In case of other errors the value is evaluated but not stored in the cache. Additional parameters (can be nil): validator - validates cache records, on false evaluator will be run again (new results will not be validated) linkResolver - checks if cached value is a link and returns the key it's pointing to linkGenerator - generates intermediate link value if needed when a new record is inserted writeApprover - decides if new value is to be written in the cache
func (*Cache[T]) Peek ¶ added in v1.1.0
Peek gets a value by given key and does not change it's "lruness"
type CacheEngine ¶
type CacheEngine interface { Get(key string) (interface{}, error) Peek(key string) (interface{}, error) Set(key string, value interface{}) error Delete(key string) error Keys() ([]string, error) Purge() error }
CacheEngine is an interface for cache engine (e.g. in-memory cache or Redis Cache)
type CacheWithSubcache ¶
CacheWithSubcache is a Cache with L1 subcache.
func (*CacheWithSubcache[T]) Delete ¶
func (cs *CacheWithSubcache[T]) Delete(key string) error
Delete removes a key from cache
func (*CacheWithSubcache[T]) Get ¶
func (cs *CacheWithSubcache[T]) Get(key string) (interface{}, error)
Get gets a cached value by key
func (*CacheWithSubcache[T]) Keys ¶
func (cs *CacheWithSubcache[T]) Keys() ([]string, error)
Keys returns a slice of all keys in the cache
func (*CacheWithSubcache[T]) Peek ¶
func (cs *CacheWithSubcache[T]) Peek(key string) (interface{}, error)
Peek gets a cached key value without side-effects (i.e. without adding to L1 cache)
func (*CacheWithSubcache[T]) Purge ¶
func (cs *CacheWithSubcache[T]) Purge() error
Purge removes all the records from the cache
func (*CacheWithSubcache[T]) Set ¶
func (cs *CacheWithSubcache[T]) Set(key string, value interface{}) error
Set stores a key-value pair into cache
type DummyLogger ¶
type DummyLogger struct{}
DummyLogger is implementation of Logger that does not log anything
type LRUCache ¶
type LRUCache struct {
// contains filtered or unexported fields
}
LRUCache is a wrapper of hashicorp's golang-lru cache which implements cachier.Cache interface
func NewLRUCache ¶
func NewLRUCache( size int, marshal func(value interface{}) ([]byte, error), unmarshal func(b []byte, value *interface{}) error, compressionEngine *compression.Engine, ) (*LRUCache, error)
NewLRUCache is a constructor that creates LRU cache of given size If you want to compress the entries before storing them the marshal and unmarshal functions must be provided If the compression.Engine is nil the marshal and unmarshal functions are not used
func NewLRUCacheWithLogger ¶
type Logger ¶
type Logger interface { Error(...interface{}) Warn(...interface{}) Print(...interface{}) }
Logger is interface for logging
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache implements cachier.CacheTTL interface using redis storage
func NewRedisCache ¶
func NewRedisCache( redisClient *redis.Client, keyPrefix string, marshal func(value interface{}) ([]byte, error), unmarshal func(b []byte, value *interface{}) error, ttl time.Duration, compressionEngine *compression.Engine, ) *RedisCache
NewRedisCache is a constructor that creates a RedisCache
func NewRedisCacheWithLogger ¶
func NewRedisCacheWithLogger( redisClient *redis.Client, keyPrefix string, marshal func(value interface{}) ([]byte, error), unmarshal func(b []byte, value *interface{}) error, ttl time.Duration, logger Logger, compressionEngine *compression.Engine, ) *RedisCache
NewRedisCacheWithLogger is a constructor that creates a RedisCache
func (*RedisCache) Delete ¶
func (rc *RedisCache) Delete(key string) error
Delete removes a key from cache
func (*RedisCache) Get ¶
func (rc *RedisCache) Get(key string) (v interface{}, err error)
Get gets a cached value by key
func (*RedisCache) Keys ¶
func (rc *RedisCache) Keys() ([]string, error)
Keys returns all the keys in the cache
func (*RedisCache) Peek ¶
func (rc *RedisCache) Peek(key string) (interface{}, error)
Peek gets a cached value by key without any sideeffects (identical as Get in this implementation)
func (*RedisCache) Purge ¶
func (rc *RedisCache) Purge() error
Purge removes all the records from the cache
func (*RedisCache) Set ¶
func (rc *RedisCache) Set(key string, value interface{}) (err error)
Set stores a key-value pair into cache