Documentation ¶
Index ¶
- Variables
- func DeserializeValue(buf []byte, valPtr any) error
- func GetCachedCalc[T any](ctx context.Context, key any, minTTL, maxTTL time.Duration, limitWorker bool, ...) (result T, err error)
- func GetCachedCalcOpt[T any](ctx context.Context, key any, ...) (T, error)
- func GetCachedCalcOptX[T any](cc *CachedCalculations, ctx context.Context, key any, ...) (result T, err error)
- func GetCachedCalcX[T any](cc *CachedCalculations, ctx context.Context, key any, ...) (T, error)
- func GetRedis(ctx context.Context) (*redis.Client, error)
- type CacheEntry
- type CachedCalcOpts
- type CachedCalculations
- type CalculateValue
- type CalculateValueAndOpt
- type ExternalCache
- type PostgresCache
- func (p *PostgresCache) Close() error
- func (p *PostgresCache) Del(ctx context.Context, key string) error
- func (p *PostgresCache) Get(ctx context.Context, key string) (value []byte, exists bool, err error)
- func (p *PostgresCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (p *PostgresCache) SetNX(ctx context.Context, key string, value []byte, ttl time.Duration) (keyCreated bool, err error)
- type RedisExternalCache
- func (r *RedisExternalCache) Close() error
- func (r *RedisExternalCache) Del(ctx context.Context, key string) error
- func (r *RedisExternalCache) Get(ctx context.Context, key string) (value []byte, exists bool, err error)
- func (r *RedisExternalCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (r *RedisExternalCache) SetNX(ctx context.Context, key string, value []byte, ttl time.Duration) (bool, error)
- type SQLiteCache
- func (c *SQLiteCache) Close() error
- func (c *SQLiteCache) Del(ctx context.Context, key string) error
- func (c *SQLiteCache) Get(ctx context.Context, key string) (value []byte, exists bool, err error)
- func (c *SQLiteCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (c *SQLiteCache) SetNX(ctx context.Context, key string, value []byte, ttl time.Duration) (keyCreated bool, err error)
Constants ¶
This section is empty.
Variables ¶
var DefaultCCs = NewCachedCalculations(4, nil)
DefaultCCs default cached calculations cache used by GetCachedCalc It does not use external cache for coordinating between multiple distributed
Functions ¶
func DeserializeValue ¶ added in v1.3.0
func GetCachedCalc ¶
func GetCachedCalc[T any](ctx context.Context, key any, minTTL, maxTTL time.Duration, limitWorker bool, calculateValue func(ctx context.Context) (T, error)) (result T, err error)
GetCachedCalc uses default cached calculations cache as GetCachedCalcX(DefaultCCs,...) for convenience it is created with default for no external cache, but that can be redefined by app
func GetCachedCalcOpt ¶ added in v1.1.1
func GetCachedCalcOpt[T any](ctx context.Context, key any, calculateValueAndOpt func(ctx context.Context) (T, CachedCalcOpts, error), limitWorkers bool) (T, error)
GetCachedCalc uses default cached calculations cache as GetCachedCalcX(DefaultCCs,...) for convenience it is created with default for no external cache, but that can be redefined by app
func GetCachedCalcOptX ¶ added in v1.1.1
func GetCachedCalcX ¶
func GetCachedCalcX[T any](cc *CachedCalculations, ctx context.Context, key any, minTTL, maxTTL time.Duration, limitWorker bool, calculateValue func(ctx context.Context) (T, error)) (T, error)
GetCachedCalcX is used wherever you need to perform cached and coordinated calculation instead of regular and uncoordinated
ctx is a parent context for calculation if parent context is cancelled then all child context are cancelled as well
params of cachedCalculation - see description of how cachedCalculation defined
Types ¶
type CacheEntry ¶ added in v1.2.1
type CacheEntry struct { Expire time.Time // time of expiration of this entry Refresh time.Time // time when this value should be refreshed CalcDuration time.Duration // how much time it took to calculate the value Err error // stores the last error status of calculations Value []byte // stores the serialized value of last calculations sync.WaitGroup sync.RWMutex // contains filtered or unexported fields }
type CachedCalcOpts ¶ added in v1.1.1
type CachedCalculations ¶
type CachedCalculations struct { sync.Mutex sync.WaitGroup // contains filtered or unexported fields }
CachedCalculations has the only method: GetCachedCalc. It is used for easy refactoring of slow/long calculating backend methods. See examples
func NewCachedCalculations ¶
func NewCachedCalculations(maxWorkers int, externalCache ExternalCache) *CachedCalculations
NewCachedCalculations is used to create app's instance of CachedCalculations. It creates two threads which handle and coordinate cached backend calculations Graceful exit from app should include expiring ctx context and then smartCacheInstance.Wait() This will gracefully finish the job of those threads
func (*CachedCalculations) Close ¶
func (cc *CachedCalculations) Close()
Close is automatically called on expired context. It is safe to call it multiple times it tries to gracefully interrupt all ongoing calculations using their context when it succeeds in this it removes the record about job
func (*CachedCalculations) RemoveEntries ¶ added in v1.2.1
func (cc *CachedCalculations) RemoveEntries(filter func(key any, entry *CacheEntry) bool)
type CalculateValue ¶
CalculateValue this is type of function which returns interface{} type
type CalculateValueAndOpt ¶ added in v1.1.1
type CalculateValueAndOpt func(context.Context) (any, CachedCalcOpts, error)
CalculateValue this is type of function which returns interface{} type
type ExternalCache ¶
type ExternalCache interface { // Set sets key to hold the string value. // If key already holds a value, it is overwritten, regardless of its type. // Any previous time to live associated with the key is discarded on successful SET operation. // Should return nil if successful Set(ctx context.Context, key string, value []byte, ttl time.Duration) error // SetNX sets key to hold string value if key does not exist. // In that case, it is equal to SET. // When key already holds a value, no operation is performed. // SETNX is short for "SET if Not eXists". // keyCreated must return true if value is set SetNX(ctx context.Context, key string, value []byte, ttl time.Duration) (keyCreated bool, err error) // GetCachedCalc gets the value of key. // exists will be false If the key does not exist. // An error is returned if the implementor want to signal any errors. Get(ctx context.Context, key string) (value []byte, exists bool, err error) // Del Removes the specified key. A key is ignored if it does not exist. Del(ctx context.Context, key string) error Close() error // closes the connection }
ExternalCache interface defines a set of required method to provide service of external cache which SmartCache can use to coordinate several of its distributed instances.
func NewPostgresCache ¶ added in v1.4.1
func NewPostgresCache(ctx context.Context, dbUrl string) (ExternalCache, error)
func NewRedisCache ¶
func NewRedisCache(ctx context.Context) (ExternalCache, error)
NewRedisCache creates an instance of ExternalCache which is connected to ENV{REDIS_URL} or local redis server if not specified.
type PostgresCache ¶ added in v1.4.1
type PostgresCache struct {
// contains filtered or unexported fields
}
func (*PostgresCache) Close ¶ added in v1.4.1
func (p *PostgresCache) Close() error
func (*PostgresCache) Del ¶ added in v1.4.1
func (p *PostgresCache) Del(ctx context.Context, key string) error
type RedisExternalCache ¶
type RedisExternalCache struct {
// contains filtered or unexported fields
}
func (*RedisExternalCache) Close ¶ added in v1.4.1
func (r *RedisExternalCache) Close() error
func (*RedisExternalCache) Del ¶
func (r *RedisExternalCache) Del(ctx context.Context, key string) error
type SQLiteCache ¶
type SQLiteCache struct {
// contains filtered or unexported fields
}
SQLiteCache implements the ExternalCache interface using a SQLite database.
func NewSQLiteCache ¶
func NewSQLiteCache(dbPath string) (*SQLiteCache, error)
NewSQLiteCache creates a new instance of SQLiteCache.
func (*SQLiteCache) Close ¶ added in v1.4.1
func (c *SQLiteCache) Close() error
Close closes the SQLite database connection.
func (*SQLiteCache) Del ¶
func (c *SQLiteCache) Del(ctx context.Context, key string) error
Del Removes the specified key. A key is ignored if it does not exist.
func (*SQLiteCache) Get ¶
GetCachedCalc gets the value of key. exists will be false If the key does not exist. An error is returned if the implementor want to signal any errors.
func (*SQLiteCache) Set ¶
Set sets key to hold the string value. If key already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on successful SET operation. Should return nil if successful
func (*SQLiteCache) SetNX ¶
func (c *SQLiteCache) SetNX(ctx context.Context, key string, value []byte, ttl time.Duration) (keyCreated bool, err error)
SetNX sets key to hold string value if key does not exist. In that case, it is equal to SET. When key already holds a value, no operation is performed. SETNX is short for "SET if Not eXists". keyCreated must return true if value is set