Documentation ¶
Index ¶
- func ExtractCacheGenNumber(ctx context.Context) string
- func HashKey(key string) string
- func InjectCacheGenNumber(ctx context.Context, cacheGen string) context.Context
- func IsCacheConfigured(cfg Config) bool
- func IsEmbeddedCacheSet(cfg Config) bool
- func IsEmptyTieredCache(cache Cache) bool
- func IsMemcacheSet(cfg Config) bool
- func IsRedisSet(cfg Config) bool
- func IsSpecificImplementationSet(cfg Config) bool
- func StringToBytes(s string) []byte
- type BackgroundConfig
- type Cache
- func CollectStats(cache Cache) Cache
- func Instrument(name string, cache Cache, reg prometheus.Registerer) Cache
- func New(cfg Config, reg prometheus.Registerer, logger log.Logger, ...) (Cache, error)
- func NewBackground(name string, cfg BackgroundConfig, cache Cache, reg prometheus.Registerer) Cache
- func NewCacheGenNumMiddleware(downstreamCache Cache) Cache
- func NewNoopCache() Cache
- func NewSnappy(next Cache, logger log.Logger) Cache
- func NewTiered(caches []Cache) Cache
- func StopOnce(cache Cache) Cache
- type Config
- type EmbeddedCache
- func (c *EmbeddedCache[K, V]) Fetch(ctx context.Context, keys []K) (foundKeys []K, foundValues []V, missingKeys []K, err error)
- func (c *EmbeddedCache[K, V]) Get(_ context.Context, key K) (V, bool)
- func (c *EmbeddedCache[K, V]) GetCacheType() stats.CacheType
- func (c *EmbeddedCache[K, V]) Stop()
- func (c *EmbeddedCache[K, V]) Store(_ context.Context, keys []K, values []V) error
- type EmbeddedCacheConfig
- type Entry
- type GenNumMiddleware
- func (c GenNumMiddleware) Fetch(ctx context.Context, keys []string) (found []string, bufs [][]byte, missing []string, err error)
- func (c GenNumMiddleware) GetCacheType() stats.CacheType
- func (c GenNumMiddleware) Stop()
- func (c GenNumMiddleware) Store(ctx context.Context, keys []string, buf [][]byte) error
- type Memcached
- type MemcachedClient
- type MemcachedClientConfig
- type MemcachedConfig
- type MockCache
- type RedisCache
- type RedisClient
- type RedisConfig
- type TypedCache
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractCacheGenNumber ¶
ExtractCacheGenNumber gets the cache gen from the context.
func InjectCacheGenNumber ¶
InjectCacheGenNumber returns a derived context containing the cache gen.
func IsCacheConfigured ¶
IsCacheConfigured determines if either of the following caches is configured: - memcached - redis - embedded-cache - specific cache implementation
func IsEmbeddedCacheSet ¶
func IsEmptyTieredCache ¶
IsEmptyTieredCache is used to determine whether the current Cache is implemented by an empty tiered.
func IsMemcacheSet ¶
IsMemcacheSet returns whether a non empty Memcache config is set or not, based on the configured host or addresses.
Internally, this function is used to set Memcache as the cache storage to be used.
func IsRedisSet ¶
IsRedisSet returns whether a non empty Redis config is set or not, based on the configured endpoint.
Internally, this function is used to set Redis as the cache storage to be used.
func StringToBytes ¶
StringToBytes converts string to byte slice. (copied from vendor/github.com/go-redis/redis/v8/internal/util/unsafe.go)
Types ¶
type BackgroundConfig ¶
type BackgroundConfig struct { WriteBackGoroutines int `yaml:"writeback_goroutines"` WriteBackBuffer int `yaml:"writeback_buffer"` WriteBackSizeLimit flagext.ByteSize `yaml:"writeback_size_limit"` }
BackgroundConfig is config for a Background Cache.
func (*BackgroundConfig) RegisterFlagsWithPrefix ¶
func (cfg *BackgroundConfig) RegisterFlagsWithPrefix(prefix string, description string, f *flag.FlagSet)
RegisterFlagsWithPrefix adds the flags required to config this to the given FlagSet
type Cache ¶
type Cache interface { Store(ctx context.Context, key []string, buf [][]byte) error Fetch(ctx context.Context, keys []string) (found []string, bufs [][]byte, missing []string, err error) Stop() // GetCacheType returns a string indicating the cache "type" for the purpose of grouping cache usage statistics GetCacheType() stats.CacheType }
Cache byte arrays by key.
func CollectStats ¶
CollectStats returns a new Cache that keeps various statistics on cache usage.
func Instrument ¶
func Instrument(name string, cache Cache, reg prometheus.Registerer) Cache
Instrument returns an instrumented cache.
func New ¶
func New(cfg Config, reg prometheus.Registerer, logger log.Logger, cacheType stats.CacheType, metricsNamespace string) (Cache, error)
New creates a new Cache using Config.
func NewBackground ¶
func NewBackground(name string, cfg BackgroundConfig, cache Cache, reg prometheus.Registerer) Cache
NewBackground returns a new Cache that does stores on background goroutines.
func NewCacheGenNumMiddleware ¶
NewCacheGenNumMiddleware creates a new GenNumMiddleware.
type Config ¶
type Config struct { DefaultValidity time.Duration `yaml:"default_validity"` Background BackgroundConfig `yaml:"background"` Memcache MemcachedConfig `yaml:"memcached"` MemcacheClient MemcachedClientConfig `yaml:"memcached_client"` Redis RedisConfig `yaml:"redis"` EmbeddedCache EmbeddedCacheConfig `yaml:"embedded_cache"` // This is to name the cache metrics properly. Prefix string `yaml:"prefix" doc:"hidden"` // For tests to inject specific implementations. Cache Cache `yaml:"-"` }
Config for building Caches.
type EmbeddedCache ¶
type EmbeddedCache[K comparable, V any] struct { // contains filtered or unexported fields }
EmbeddedCache is a simple (comparable -> any) cache which uses a fifo slide to manage evictions. O(1) inserts and updates, O(1) gets.
This embedded cache implementation supports two eviction methods - based on number of items in the cache, and based on memory usage. For the memory-based eviction, set EmbeddedCacheConfig.MaxSizeMB to a positive integer, indicating upper limit of memory allocated by items in the cache. Alternatively, set EmbeddedCacheConfig.MaxSizeItems to a positive integer, indicating maximum number of items in the cache. If both parameters are set, both methods are enforced, whichever hits first.
func NewEmbeddedCache ¶
func NewEmbeddedCache(name string, cfg EmbeddedCacheConfig, reg prometheus.Registerer, logger log.Logger, cacheType stats.CacheType) *EmbeddedCache[string, []byte]
NewEmbeddedCache returns a new initialised EmbeddedCache where the key is a string and the value is a slice of bytes.
func NewTypedEmbeddedCache ¶
func NewTypedEmbeddedCache[K comparable, V any]( name string, cfg EmbeddedCacheConfig, reg prometheus.Registerer, logger log.Logger, cacheType stats.CacheType, entrySizeCalculator cacheEntrySizeCalculator[K, V], onEntryRemoved func(key K, value V), ) *EmbeddedCache[K, V]
NewTypedEmbeddedCache returns a new initialised EmbeddedCache with the key and value of requested types. To limit the memory allocated by items in the cache, it's necessary to pass cacheEntrySizeCalculator that calculates the size of an entry in bytes. Also, this constructor allows passing the callback that will be called for the entry whenever it is removed from the cache.
func (*EmbeddedCache[K, V]) Fetch ¶
func (c *EmbeddedCache[K, V]) Fetch(ctx context.Context, keys []K) (foundKeys []K, foundValues []V, missingKeys []K, err error)
Fetch implements Cache.
func (*EmbeddedCache[K, V]) Get ¶
func (c *EmbeddedCache[K, V]) Get(_ context.Context, key K) (V, bool)
Get returns the stored value against the key and when the key was last updated.
func (*EmbeddedCache[K, V]) GetCacheType ¶
func (c *EmbeddedCache[K, V]) GetCacheType() stats.CacheType
type EmbeddedCacheConfig ¶
type EmbeddedCacheConfig struct { Enabled bool `yaml:"enabled,omitempty"` MaxSizeMB int64 `yaml:"max_size_mb"` MaxSizeItems int `yaml:"max_size_items"` TTL time.Duration `yaml:"ttl"` // PurgeInterval tell how often should we remove keys that are expired. // by default it takes `defaultPurgeInterval` PurgeInterval time.Duration `yaml:"-"` }
EmbeddedCacheConfig represents in-process embedded cache config.
func (*EmbeddedCacheConfig) IsEnabled ¶
func (cfg *EmbeddedCacheConfig) IsEnabled() bool
func (*EmbeddedCacheConfig) RegisterFlagsWithPrefix ¶
func (cfg *EmbeddedCacheConfig) RegisterFlagsWithPrefix(prefix, description string, f *flag.FlagSet)
func (*EmbeddedCacheConfig) RegisterFlagsWithPrefixAndDefaults ¶
type Entry ¶
type Entry[K comparable, V any] struct { Key K Value V // contains filtered or unexported fields }
type GenNumMiddleware ¶
type GenNumMiddleware struct {
// contains filtered or unexported fields
}
GenNumMiddleware adds gen number to keys from context. Expected size of gen numbers is upto 2 digits. If we start seeing problems with keys exceeding length limit, we need to look into resetting gen numbers.
func (GenNumMiddleware) Fetch ¶
func (c GenNumMiddleware) Fetch(ctx context.Context, keys []string) (found []string, bufs [][]byte, missing []string, err error)
Fetch adds cache gen number to keys before calling Fetch method of downstream cache. It also removes gen number before responding back with found and missing keys to make sure consumer of response gets to see same keys.
func (GenNumMiddleware) GetCacheType ¶
func (c GenNumMiddleware) GetCacheType() stats.CacheType
func (GenNumMiddleware) Stop ¶
func (c GenNumMiddleware) Stop()
Stop calls Stop method of downstream cache.
type Memcached ¶
type Memcached struct {
// contains filtered or unexported fields
}
Memcached type caches chunks in memcached
func NewMemcached ¶
func NewMemcached(cfg MemcachedConfig, client MemcachedClient, name string, reg prometheus.Registerer, logger log.Logger, cacheType stats.CacheType) *Memcached
NewMemcached makes a new Memcached.
func (*Memcached) Fetch ¶
func (c *Memcached) Fetch(ctx context.Context, keys []string) (found []string, bufs [][]byte, missed []string, err error)
Fetch gets keys from the cache. The keys that are found must be in the order of the keys requested.
func (*Memcached) GetCacheType ¶
type MemcachedClient ¶
type MemcachedClient interface { GetMulti(keys []string, opts ...memcache.Option) (map[string]*memcache.Item, error) Set(item *memcache.Item) error }
MemcachedClient interface exists for mocking memcacheClient.
func NewMemcachedClient ¶
func NewMemcachedClient(cfg MemcachedClientConfig, name string, r prometheus.Registerer, logger log.Logger, metricsNamespace string) MemcachedClient
NewMemcachedClient creates a new MemcacheClient that gets its server list from SRV and updates the server list on a regular basis.
type MemcachedClientConfig ¶
type MemcachedClientConfig struct { Host string `yaml:"host"` Service string `yaml:"service"` Addresses string `yaml:"addresses"` Timeout time.Duration `yaml:"timeout"` MaxIdleConns int `yaml:"max_idle_conns"` MaxItemSize int `yaml:"max_item_size"` UpdateInterval time.Duration `yaml:"update_interval"` ConsistentHash bool `yaml:"consistent_hash"` CBFailures uint `yaml:"circuit_breaker_consecutive_failures"` CBTimeout time.Duration `yaml:"circuit_breaker_timeout"` // reset error count after this long CBInterval time.Duration `yaml:"circuit_breaker_interval"` // remain closed for this long after CBFailures errors // TLSEnabled enables connecting to Memcached with TLS. TLSEnabled bool `yaml:"tls_enabled"` // TLS to use to connect to the Memcached server. TLS dstls.ClientConfig `yaml:",inline"` }
MemcachedClientConfig defines how a MemcachedClient should be constructed.
func (*MemcachedClientConfig) RegisterFlagsWithPrefix ¶
func (cfg *MemcachedClientConfig) RegisterFlagsWithPrefix(prefix, description string, f *flag.FlagSet)
RegisterFlagsWithPrefix adds the flags required to config this to the given FlagSet
type MemcachedConfig ¶
type MemcachedConfig struct { Expiration time.Duration `yaml:"expiration"` BatchSize int `yaml:"batch_size"` Parallelism int `yaml:"parallelism"` }
MemcachedConfig is config to make a Memcached
func (*MemcachedConfig) RegisterFlagsWithPrefix ¶
func (cfg *MemcachedConfig) RegisterFlagsWithPrefix(prefix, description string, f *flag.FlagSet)
RegisterFlagsWithPrefix adds the flags required to config this to the given FlagSet
type MockCache ¶
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache type caches chunks in redis
func NewRedisCache ¶
func NewRedisCache(name string, redisClient *RedisClient, logger log.Logger, cacheType stats.CacheType) *RedisCache
NewRedisCache creates a new RedisCache
func (*RedisCache) Fetch ¶
func (c *RedisCache) Fetch(ctx context.Context, keys []string) (found []string, bufs [][]byte, missed []string, err error)
Fetch gets keys from the cache. The keys that are found must be in the order of the keys requested.
func (*RedisCache) GetCacheType ¶
func (c *RedisCache) GetCacheType() stats.CacheType
type RedisClient ¶
type RedisClient struct {
// contains filtered or unexported fields
}
func NewRedisClient ¶
func NewRedisClient(cfg *RedisConfig) (*RedisClient, error)
NewRedisClient creates Redis client
func (*RedisClient) Close ¶
func (c *RedisClient) Close() error
type RedisConfig ¶
type RedisConfig struct { Endpoint string `yaml:"endpoint"` MasterName string `yaml:"master_name"` Timeout time.Duration `yaml:"timeout"` Expiration time.Duration `yaml:"expiration"` DB int `yaml:"db"` PoolSize int `yaml:"pool_size"` Username string `yaml:"username"` Password flagext.Secret `yaml:"password"` EnableTLS bool `yaml:"tls_enabled"` InsecureSkipVerify bool `yaml:"tls_insecure_skip_verify"` IdleTimeout time.Duration `yaml:"idle_timeout"` MaxConnAge time.Duration `yaml:"max_connection_age"` RouteRandomly bool `yaml:"route_randomly"` }
RedisConfig defines how a RedisCache should be constructed.
func (*RedisConfig) RegisterFlagsWithPrefix ¶
func (cfg *RedisConfig) RegisterFlagsWithPrefix(prefix, description string, f *flag.FlagSet)
RegisterFlagsWithPrefix adds the flags required to config this to the given FlagSet
type TypedCache ¶
type TypedCache[K comparable, V any] interface { Store(ctx context.Context, keys []K, values []V) error Fetch(ctx context.Context, keys []K) (found []K, values []V, missing []K, err error) Stop() // GetCacheType returns a string indicating the cache "type" for the purpose of grouping cache usage statistics GetCacheType() stats.CacheType }
Interface for EmbeddedCache Matches the interface from cache.Cache but has generics
func NewNoopTypedCache ¶
func NewNoopTypedCache[K comparable, V any]() TypedCache[K, V]