Documentation
¶
Overview ¶
Package anycache provide laze caching with posibility to use diffent cache storages
Index ¶
Constants ¶
const (
HundredPercent = 100
)
Variables ¶
This section is empty.
Functions ¶
func WithTTLRandomization ¶ added in v0.0.3
WithTTLRandomization sets max shift of TTL in persent
Types ¶
type Cache ¶
type Cache struct { Storage CacheStorage // contains filtered or unexported fields }
Cache
func NewCache ¶
func NewCache(store CacheStorage, opts ...CacheOptions) Cache
NewCache creates a new Cache instance with the provided CacheStorage and CacheOptions. WithTTLRandomization sets max shift of TTL in persent It returns the created Cache instance.
func (*Cache) Cache ¶
func (c *Cache) Cache(key string, generator CacheGenerator, opts ...CacheItemOptions) (string, error)
Cache caches the result of the generator function for the given key. If the key already exists in the cache, the cached value is returned. Otherwise, the generator function is called to generate a new value, which is then cached and returned. The function takes an optional list of CacheItemOptions to customize the caching behavior. WithTTL sets TTL for cache item WithWarmUpTTL sets TTL threshold for cache item to be warmed up
func (*Cache) CacheStruct ¶ added in v0.0.3
func (c *Cache) CacheStruct(key string, generator func(context.Context) (any, error), result any, opts ...CacheItemOptions) error
CacheStruct caches the result of a function that returns a struct. The key is used to identify the cached value. The generator function is called to generate the value if it is not already cached. The result parameter is a pointer to the struct that will be populated with the cached value. The opts parameter is optional and can be used to set additional cache item options. WithTTL sets TTL for cache item WithWarmUpTTL sets TTL threshold for cache item to be warmed up Returns an error if there was a problem caching or unmarshalling the value.
type CacheItemOptions ¶
type CacheItemOptions func(*CacheReuest)
func WithCtx ¶ added in v0.0.4
func WithCtx(ctx context.Context) CacheItemOptions
func WithTTL ¶ added in v0.0.3
func WithTTL(ttl time.Duration) CacheItemOptions
WithTTL sets TTL for cache item
func WithWarmUpTTL ¶ added in v0.0.3
func WithWarmUpTTL(ttl time.Duration) CacheItemOptions
WithWarmUpTTL sets TTL threshold for cache item to be warmed up
type CacheOptions ¶
type CacheOptions func(*Cache)
type CacheQueue ¶
type CacheQueue struct { WarmingUp bool // contains filtered or unexported fields }
type CacheResponse ¶
type CacheResponse struct {
// contains filtered or unexported fields
}
type CacheReuest ¶
type CacheStorage ¶
type CacheStorage interface { Get(context.Context, string) (string, error) Set(context.Context, string, string, storage.CacheStorageItemOptions) error TTL(context.Context, string) (bool, time.Duration, error) Del(context.Context, string) (bool, error) GetWithTTL(context.Context, string) (string, time.Duration, error) Close() error }
CacheStorage