Documentation ¶
Overview ¶
Package cachestore is the caching (key->value) service abstraction layer
Index ¶
- Constants
- Variables
- func DefaultRistrettoConfig() *ristretto.Config
- type CacheService
- type Client
- func (c *Client) Close(ctx context.Context)
- func (c *Client) Debug(on bool)
- func (c *Client) Engine() Engine
- func (c *Client) Get(ctx context.Context, key string) (interface{}, error)
- func (c *Client) GetModel(ctx context.Context, key string, model interface{}) error
- func (c *Client) IsDebug() bool
- func (c *Client) IsNewRelicEnabled() bool
- func (c *Client) MCache() *mcache.CacheDriver
- func (c *Client) Redis() *cache.Client
- func (c *Client) RedisConfig() *RedisConfig
- func (c *Client) ReleaseLock(ctx context.Context, lockKey, secret string) (bool, error)
- func (c *Client) Ristretto() *ristretto.Cache
- func (c *Client) RistrettoConfig() *ristretto.Config
- func (c *Client) Set(ctx context.Context, key string, value interface{}, dependencies ...string) error
- func (c *Client) SetModel(ctx context.Context, key string, model interface{}, ttl time.Duration, ...) error
- func (c *Client) WaitWriteLock(ctx context.Context, lockKey string, ttl, ttw int64) (string, error)
- func (c *Client) WriteLock(ctx context.Context, lockKey string, ttl int64) (string, error)
- type ClientInterface
- type ClientOps
- func WithDebugging() ClientOps
- func WithMcache() ClientOps
- func WithMcacheConnection(driver *mcache.CacheDriver) ClientOps
- func WithNewRelic() ClientOps
- func WithRedis(redisConfig *RedisConfig) ClientOps
- func WithRedisConnection(redisClient *cache.Client) ClientOps
- func WithRistretto(config *ristretto.Config) ClientOps
- func WithRistrettoConnection(ristrettoClient *ristretto.Cache) ClientOps
- type Engine
- type LockService
- type RedisConfig
Constants ¶
const ( // DefaultRedisMaxIdleTimeout is the default max timeout on an idle connection DefaultRedisMaxIdleTimeout = 240 * time.Second // RedisPrefix is the prefix for URL based connections RedisPrefix = "redis://" )
Variables ¶
var ErrEngineNotSupported = errors.New("engine is not supported")
ErrEngineNotSupported is the error when the engine is not supported for the requested method
var ErrFailedToSet = errors.New("failed to set value in cache")
ErrFailedToSet is when the key failed to set in cache, check the cost/allocated
var ErrInvalidRedisConfig = errors.New("invalid redis config")
ErrInvalidRedisConfig is when the redis config is missing or invalid
var ErrInvalidRistrettoConfig = errors.New("invalid ristretto config")
ErrInvalidRistrettoConfig is when the ristretto config is missing or invalid
var ErrKeyNotFound = errors.New("key not found")
ErrKeyNotFound is returned when a record is not found for a given key
var ErrKeyRequired = errors.New("key is empty and required")
ErrKeyRequired is returned when the key is empty (key->value)
var ErrLockCreateFailed = errors.New("failed creating cache lock")
ErrLockCreateFailed is the error when creating a lock fails
var ErrLockExists = errors.New("lock already exists with a different secret")
ErrLockExists is the error when trying to create a lock fails due to an existing lock
var ErrNoEngine = errors.New("cachestore engine is empty: choose redis or memory (IE: WithRedis())")
ErrNoEngine is returned when there is no engine set (missing engine)
var ErrRistrettoSetFailed = errors.New("failed to set key in ristretto")
ErrRistrettoSetFailed is when the ristretto failed to set a key
var ErrSecretGenerationFailed = errors.New("failed generating secret")
ErrSecretGenerationFailed is the error if the secret failed to generate
var ErrSecretRequired = errors.New("secret is empty and required")
ErrSecretRequired is returned when the secret is empty (value)
var ErrTTWCannotBeEmpty = errors.New("the TTW value cannot be empty")
ErrTTWCannotBeEmpty is when the TTW field is empty
Functions ¶
func DefaultRistrettoConfig ¶
DefaultRistrettoConfig will return a default configuration that can be modified
Types ¶
type CacheService ¶
type CacheService interface { Get(ctx context.Context, key string) (interface{}, error) GetModel(ctx context.Context, key string, model interface{}) error Set(ctx context.Context, key string, value interface{}, dependencies ...string) error SetModel(ctx context.Context, key string, model interface{}, ttl time.Duration, dependencies ...string) error }
CacheService are the cache related methods
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the client (configuration)
func (*Client) Get ¶
Get will return a value from a given key
Redis will be an interface{} but really a string (empty string) mCache/ristretto will be an interface{} and usually a pointer (empty nil)
func (*Client) GetModel ¶
GetModel will get a model (parsing JSON (bytes) -> Model)
Model needs to be a pointer to a struct
func (*Client) IsNewRelicEnabled ¶
IsNewRelicEnabled will return if new relic is enabled
func (*Client) MCache ¶
func (c *Client) MCache() *mcache.CacheDriver
MCache will return the mCache client if found
func (*Client) Redis ¶
func (c *Client) Redis() *cache.Client
Redis will return the Redis client if found
func (*Client) RedisConfig ¶
func (c *Client) RedisConfig() *RedisConfig
RedisConfig will return the Redis config client if found
func (*Client) ReleaseLock ¶
ReleaseLock will release a given lock key only if the secret matches
func (*Client) RistrettoConfig ¶
RistrettoConfig will return the Ristretto config
func (*Client) Set ¶
func (c *Client) Set(ctx context.Context, key string, value interface{}, dependencies ...string) error
Set will set a key->value using the current engine
NOTE: redis only supports dependency keys at this time
func (*Client) SetModel ¶
func (c *Client) SetModel(ctx context.Context, key string, model interface{}, ttl time.Duration, dependencies ...string) error
SetModel will set any model or struct (parsing Model->JSON (bytes))
Model needs to be a pointer to a struct NOTE: redis only supports dependency keys at this time
func (*Client) WaitWriteLock ¶
WaitWriteLock will aggressively try to make a lock until the TTW (in seconds) is reached
type ClientInterface ¶
type ClientInterface interface { CacheService LockService Close(ctx context.Context) Debug(on bool) Engine() Engine IsDebug() bool IsNewRelicEnabled() bool MCache() *mcache.CacheDriver Redis() *cache.Client RedisConfig() *RedisConfig Ristretto() *ristretto.Cache RistrettoConfig() *ristretto.Config }
ClientInterface is the cachestore interface
type ClientOps ¶
type ClientOps func(c *clientOptions)
ClientOps allow functional options to be supplied that overwrite default client options.
func WithMcache ¶
func WithMcache() ClientOps
WithMcache will set the cache to local memory using mCache
func WithMcacheConnection ¶
func WithMcacheConnection(driver *mcache.CacheDriver) ClientOps
WithMcacheConnection will set the cache to a current mCache driver connection
func WithRedis ¶
func WithRedis(redisConfig *RedisConfig) ClientOps
WithRedis will set the redis configuration
func WithRedisConnection ¶
func WithRedisConnection(redisClient *cache.Client) ClientOps
WithRedisConnection will set an existing redis connection (read & write)
func WithRistretto ¶
WithRistretto will set the cache to local in-memory using Ristretto
func WithRistrettoConnection ¶
WithRistrettoConnection will set an existing ristretto connection (read & write)
type Engine ¶
type Engine string
Engine is the different engines that are supported (key->value)
const ( Empty Engine = "empty" MCache Engine = "mcache" Redis Engine = "redis" Ristretto Engine = "ristretto" )
Supported engines
type LockService ¶
type LockService interface { ReleaseLock(ctx context.Context, lockKey, secret string) (bool, error) WaitWriteLock(ctx context.Context, lockKey string, ttl, ttw int64) (string, error) WriteLock(ctx context.Context, lockKey string, ttl int64) (string, error) }
LockService are the locking related methods
type RedisConfig ¶
type RedisConfig struct { DependencyMode bool `json:"dependency_mode" mapstructure:"dependency_mode"` // false for digital ocean (not supported) MaxActiveConnections int `json:"max_active_connections" mapstructure:"max_active_connections"` // 0 MaxConnectionLifetime time.Duration `json:"max_connection_lifetime" mapstructure:"max_connection_lifetime"` // 0 MaxIdleConnections int `json:"max_idle_connections" mapstructure:"max_idle_connections"` // 10 MaxIdleTimeout time.Duration `json:"max_idle_timeout" mapstructure:"max_idle_timeout"` // 240 * time.Second URL string `json:"url" mapstructure:"url"` // redis://localhost:6379 UseTLS bool `json:"use_tls" mapstructure:"use_tls"` // true for digital ocean (required) }
RedisConfig is the configuration for the cache client (redis)