Documentation ¶
Index ¶
- func HashKey(key string) string
- type BackgroundConfig
- type Cache
- type Config
- type FifoCache
- func (c *FifoCache) Fetch(ctx context.Context, keys []string) (found []string, bufs [][]byte, missing []string)
- func (c *FifoCache) Get(ctx context.Context, key string) (interface{}, bool)
- func (c *FifoCache) Put(ctx context.Context, keys []string, values []interface{})
- func (c *FifoCache) Stop() error
- func (c *FifoCache) Store(ctx context.Context, keys []string, bufs [][]byte)
- type FifoCacheConfig
- type Memcached
- type MemcachedClient
- type MemcachedClientConfig
- type MemcachedConfig
- type MemcachedJumpHashSelector
- type RedisCache
- type RedisConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BackgroundConfig ¶
type BackgroundConfig struct { WriteBackGoroutines int `yaml:"writeback_goroutines,omitempty"` WriteBackBuffer int `yaml:"writeback_buffer,omitempty"` }
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) Fetch(ctx context.Context, keys []string) (found []string, bufs [][]byte, missing []string) Stop() error }
Cache byte arrays by key.
NB we intentionally do not return errors in this interface - caching is best effort by definition. We found that when these methods did return errors, the caller would just log them - so its easier for implementation to do that. Whatsmore, we found partially successful Fetchs were often treated as failed when they returned an error.
func Instrument ¶
Instrument returns an instrumented cache.
func NewBackground ¶
func NewBackground(name string, cfg BackgroundConfig, cache Cache) Cache
NewBackground returns a new Cache that does stores on background goroutines.
type Config ¶
type Config struct { EnableFifoCache bool `yaml:"enable_fifocache,omitempty"` DefaultValidity time.Duration `yaml:"defaul_validity,omitempty"` Background BackgroundConfig `yaml:"background,omitempty"` Memcache MemcachedConfig `yaml:"memcached,omitempty"` MemcacheClient MemcachedClientConfig `yaml:"memcached_client,omitempty"` Redis RedisConfig `yaml:"redis,omitempty"` Fifocache FifoCacheConfig `yaml:"fifocache,omitempty"` // This is to name the cache metrics properly. Prefix string `yaml:"prefix,omitempty"` // For tests to inject specific implementations. Cache Cache }
Config for building Caches.
type FifoCache ¶
type FifoCache struct {
// contains filtered or unexported fields
}
FifoCache is a simple string -> interface{} cache which uses a fifo slide to manage evictions. O(1) inserts and updates, O(1) gets.
func NewFifoCache ¶
func NewFifoCache(name string, cfg FifoCacheConfig) *FifoCache
NewFifoCache returns a new initialised FifoCache of size. TODO(bwplotka): Fix metrics, get them out of globals, separate or allow prefixing.
func (*FifoCache) Fetch ¶
func (c *FifoCache) Fetch(ctx context.Context, keys []string) (found []string, bufs [][]byte, missing []string)
Fetch implements Cache.
func (*FifoCache) Get ¶
Get returns the stored value against the key and when the key was last updated.
type FifoCacheConfig ¶
type FifoCacheConfig struct { Size int `yaml:"size,omitempty"` Validity time.Duration `yaml:"validity,omitempty"` }
FifoCacheConfig holds config for the FifoCache.
func (*FifoCacheConfig) RegisterFlagsWithPrefix ¶
func (cfg *FifoCacheConfig) RegisterFlagsWithPrefix(prefix, description string, f *flag.FlagSet)
RegisterFlagsWithPrefix adds the flags required to config this to the given FlagSet
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) *Memcached
NewMemcached makes a new Memcache. TODO(bwplotka): Fix metrics, get them out of globals, separate or allow prefixing. TODO(bwplotka): Remove globals & util packages from cache package entirely (e.g util.Logger).
type MemcachedClient ¶
type MemcachedClient interface { GetMulti(keys []string) (map[string]*memcache.Item, error) Set(item *memcache.Item) error }
MemcachedClient interface exists for mocking memcacheClient.
func NewMemcachedClient ¶
func NewMemcachedClient(cfg MemcachedClientConfig) 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,omitempty"` Service string `yaml:"service,omitempty"` Timeout time.Duration `yaml:"timeout,omitempty"` MaxIdleConns int `yaml:"max_idle_conns,omitempty"` UpdateInterval time.Duration `yaml:"update_interval,omitempty"` ConsistentHash bool `yaml:"consistent_hash,omitempty"` }
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,omitempty"` BatchSize int `yaml:"batch_size,omitempty"` Parallelism int `yaml:"parallelism,omitempty"` }
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 MemcachedJumpHashSelector ¶
type MemcachedJumpHashSelector struct {
// contains filtered or unexported fields
}
MemcachedJumpHashSelector implements the memcache.ServerSelector interface. MemcachedJumpHashSelector utilizes a jump hash to distribute keys to servers.
While adding or removing servers only requires 1/N keys to move, servers are treated as a stack and can only be pushed/popped. Therefore, MemcachedJumpHashSelector works best for servers with consistent DNS names where the naturally sorted order is predictable.
func (*MemcachedJumpHashSelector) Each ¶
func (s *MemcachedJumpHashSelector) Each(f func(net.Addr) error) error
Each iterates over each server and calls the given function. If f returns a non-nil error, iteration will stop and that error will be returned.
func (*MemcachedJumpHashSelector) PickServer ¶
func (s *MemcachedJumpHashSelector) PickServer(key string) (net.Addr, error)
PickServer returns the server address that a given item should be shared onto.
func (*MemcachedJumpHashSelector) SetServers ¶
func (s *MemcachedJumpHashSelector) SetServers(servers ...string) error
SetServers changes a MemcachedJumpHashSelector's set of servers at runtime and is safe for concurrent use by multiple goroutines.
Each server is given equal weight. A server is given more weight if it's listed multiple times.
SetServers returns an error if any of the server names fail to resolve. No attempt is made to connect to the server. If any error occurs, no changes are made to the internal server list.
To minimize the number of rehashes for keys when scaling the number of servers in subsequent calls to SetServers, servers are stored in natural sort order.
type RedisCache ¶ added in v0.4.0
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache type caches chunks in redis
func NewRedisCache ¶ added in v0.4.0
func NewRedisCache(cfg RedisConfig, name string, pool *redis.Pool) *RedisCache
NewRedisCache creates a new RedisCache
func (*RedisCache) Fetch ¶ added in v0.4.0
func (c *RedisCache) Fetch(ctx context.Context, keys []string) (found []string, bufs [][]byte, missed []string)
Fetch gets keys from the cache. The keys that are found must be in the order of the keys requested.
func (*RedisCache) Stop ¶ added in v0.4.0
func (c *RedisCache) Stop() error
Stop stops the redis client.
type RedisConfig ¶ added in v0.4.0
type RedisConfig struct { Endpoint string `yaml:"endpoint,omitempty"` Timeout time.Duration `yaml:"timeout,omitempty"` Expiration time.Duration `yaml:"expiration,omitempty"` MaxIdleConns int `yaml:"max_idle_conns,omitempty"` MaxActiveConns int `yaml:"max_active_conns,omitempty"` }
RedisConfig defines how a RedisCache should be constructed.
func (*RedisConfig) RegisterFlagsWithPrefix ¶ added in v0.4.0
func (cfg *RedisConfig) RegisterFlagsWithPrefix(prefix, description string, f *flag.FlagSet)
RegisterFlagsWithPrefix adds the flags required to config this to the given FlagSet