Documentation
¶
Index ¶
- Constants
- Variables
- type AddressProvider
- type Allocator
- type BackendConfig
- type Cache
- type CompressionConfig
- type ErroringMockCache
- type InstrumentedMockCache
- func (m *InstrumentedMockCache) Add(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (m *InstrumentedMockCache) Advance(d time.Duration)
- func (m *InstrumentedMockCache) CountDeleteCalls() int
- func (m *InstrumentedMockCache) CountFetchCalls() int
- func (m *InstrumentedMockCache) CountStoreCalls() int
- func (m *InstrumentedMockCache) Delete(ctx context.Context, key string) error
- func (m *InstrumentedMockCache) Flush()
- func (m *InstrumentedMockCache) GetItems() map[string]Item
- func (m *InstrumentedMockCache) GetMulti(ctx context.Context, keys []string, opts ...Option) map[string][]byte
- func (m *InstrumentedMockCache) GetMultiWithError(ctx context.Context, keys []string, opts ...Option) (map[string][]byte, error)
- func (m *InstrumentedMockCache) Name() string
- func (m *InstrumentedMockCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (m *InstrumentedMockCache) SetAsync(key string, value []byte, ttl time.Duration)
- func (m *InstrumentedMockCache) SetMultiAsync(data map[string][]byte, ttl time.Duration)
- func (m *InstrumentedMockCache) Stop()
- type Item
- type LRUCache
- func (l *LRUCache) Add(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (l *LRUCache) Delete(ctx context.Context, key string) error
- func (l *LRUCache) GetMulti(ctx context.Context, keys []string, opts ...Option) (result map[string][]byte)
- func (l *LRUCache) GetMultiWithError(ctx context.Context, keys []string, opts ...Option) (result map[string][]byte, err error)
- func (l *LRUCache) Name() string
- func (l *LRUCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (l *LRUCache) SetAsync(key string, value []byte, ttl time.Duration)
- func (l *LRUCache) SetMultiAsync(data map[string][]byte, ttl time.Duration)
- func (l *LRUCache) Stop()
- type MemcachedClient
- func (c *MemcachedClient) Add(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (c *MemcachedClient) CompareAndSwap(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (c *MemcachedClient) Decrement(ctx context.Context, key string, delta uint64) (uint64, error)
- func (c *MemcachedClient) Delete(ctx context.Context, key string) error
- func (c *MemcachedClient) FlushAll(ctx context.Context) error
- func (c *MemcachedClient) GetMulti(ctx context.Context, keys []string, opts ...Option) map[string][]byte
- func (c *MemcachedClient) GetMultiWithError(ctx context.Context, keys []string, opts ...Option) (map[string][]byte, error)
- func (c *MemcachedClient) Increment(ctx context.Context, key string, delta uint64) (uint64, error)
- func (c *MemcachedClient) Name() string
- func (c *MemcachedClient) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (c *MemcachedClient) SetAsync(key string, value []byte, ttl time.Duration)
- func (c *MemcachedClient) SetMultiAsync(data map[string][]byte, ttl time.Duration)
- func (c *MemcachedClient) Stop()
- func (c *MemcachedClient) Touch(ctx context.Context, key string, ttl time.Duration) error
- type MemcachedClientConfig
- type MemcachedJumpHashSelector
- type MockCache
- func (m *MockCache) Add(_ context.Context, key string, value []byte, ttl time.Duration) error
- func (m *MockCache) Advance(d time.Duration)
- func (m *MockCache) Delete(_ context.Context, key string) error
- func (m *MockCache) Flush()
- func (m *MockCache) GetItems() map[string]Item
- func (m *MockCache) GetMulti(ctx context.Context, keys []string, opts ...Option) map[string][]byte
- func (m *MockCache) GetMultiWithError(_ context.Context, keys []string, _ ...Option) (map[string][]byte, error)
- func (m *MockCache) Name() string
- func (m *MockCache) Set(_ context.Context, key string, value []byte, ttl time.Duration) error
- func (m *MockCache) SetAsync(key string, value []byte, ttl time.Duration)
- func (m *MockCache) SetMultiAsync(data map[string][]byte, ttl time.Duration)
- func (m *MockCache) Stop()
- type Option
- type Options
- type SnappyCache
- func (s *SnappyCache) Add(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (s *SnappyCache) Delete(ctx context.Context, key string) error
- func (s *SnappyCache) GetMulti(ctx context.Context, keys []string, opts ...Option) map[string][]byte
- func (s *SnappyCache) GetMultiWithError(ctx context.Context, keys []string, opts ...Option) (map[string][]byte, error)
- func (s *SnappyCache) Name() string
- func (s *SnappyCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (s *SnappyCache) SetAsync(key string, value []byte, ttl time.Duration)
- func (s *SnappyCache) SetMultiAsync(data map[string][]byte, ttl time.Duration)
- func (s *SnappyCache) Stop()
- type SpanlessTracingCache
- func (t *SpanlessTracingCache) Add(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (t *SpanlessTracingCache) Delete(ctx context.Context, key string) error
- func (t *SpanlessTracingCache) GetMulti(ctx context.Context, keys []string, opts ...Option) (result map[string][]byte)
- func (t *SpanlessTracingCache) GetMultiWithError(ctx context.Context, keys []string, opts ...Option) (result map[string][]byte, err error)
- func (t *SpanlessTracingCache) Name() string
- func (t *SpanlessTracingCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (t *SpanlessTracingCache) SetAsync(key string, value []byte, ttl time.Duration)
- func (t *SpanlessTracingCache) SetMultiAsync(data map[string][]byte, ttl time.Duration)
- func (t *SpanlessTracingCache) Stop()
- type Versioned
- func (c *Versioned) Add(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (c *Versioned) Delete(ctx context.Context, key string) error
- func (c *Versioned) GetMulti(ctx context.Context, keys []string, opts ...Option) map[string][]byte
- func (c *Versioned) GetMultiWithError(ctx context.Context, keys []string, opts ...Option) (map[string][]byte, error)
- func (c *Versioned) Name() string
- func (c *Versioned) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (c *Versioned) SetAsync(key string, value []byte, ttl time.Duration)
- func (c *Versioned) SetMultiAsync(data map[string][]byte, ttl time.Duration)
- func (c *Versioned) Stop()
Constants ¶
const (
BackendMemcached = "memcached"
)
const (
// CompressionSnappy is the value of the snappy compression.
CompressionSnappy = "snappy"
)
Variables ¶
var ( ErrNotStored = errors.New("item not stored") ErrInvalidTTL = errors.New("invalid TTL") )
Functions ¶
This section is empty.
Types ¶
type AddressProvider ¶
type AddressProvider interface {
// Resolve resolves the provided list of memcached cluster to the actual nodes
Resolve(context.Context, []string) error
// Addresses returns the nodes
Addresses() []string
}
AddressProvider performs node address resolution given a list of clusters.
type Allocator ¶
type Allocator interface {
// Get returns a byte slice with at least sz capacity. Length of the slice is
// not guaranteed and so must be asserted by callers (cache clients).
Get(sz int) *[]byte
// Put returns the byte slice to the underlying allocator. The cache clients
// will only call this method during error handling when allocated values are
// not returned to the caller as cache results.
Put(b *[]byte)
}
Allocator allows memory for cache result values to be managed by callers instead of by a cache client itself. For example, this can be used by callers to implement arena-style memory management if a workload tends to be request-centric.
type BackendConfig ¶
type BackendConfig struct {
Backend string `yaml:"backend"`
Memcached MemcachedClientConfig `yaml:"memcached"`
}
type Cache ¶
type Cache interface {
// GetMulti fetches multiple keys at once from a cache. In case of error,
// an empty map is returned and the error tracked/logged. One or more Option
// instances may be passed to modify the behavior of this GetMulti call.
GetMulti(ctx context.Context, keys []string, opts ...Option) map[string][]byte
// GetMultiWithError fetches multiple keys at once from a cache and returns an error
// if the operation failed. It may return both results and an error if there are only partial
// failures. One or more Option instances may be passed to modify the
// behavior of this GetMultiWithError call.
GetMultiWithError(ctx context.Context, keys []string, opts ...Option) (map[string][]byte, error)
// SetAsync enqueues an operation to store a key into a cache. In case the underlying
// operation fails, the error will be tracked/logged.
SetAsync(key string, value []byte, ttl time.Duration)
// SetMultiAsync enqueues operations to store a keys and values into a cache. In case
// any underlying async operations fail, the errors will be tracked/logged.
SetMultiAsync(data map[string][]byte, ttl time.Duration)
// Set stores a key and value into a cache.
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
// Add stores a key and value into a cache only if it does not already exist. If the
// item was not stored because an entry already exists in the cache, ErrNotStored will
// be returned.
Add(ctx context.Context, key string, value []byte, ttl time.Duration) error
// Delete deletes a key from a cache.
Delete(ctx context.Context, key string) error
// Stop client and release underlying resources.
Stop()
// Name returns the name of this particular cache instance.
Name() string
}
Cache is a high level interface to interact with a cache.
func CreateClient ¶
func CreateClient(cacheName string, cfg BackendConfig, logger log.Logger, reg prometheus.Registerer) (Cache, error)
func NewCompression ¶
func NewCompression(cfg CompressionConfig, next Cache, logger log.Logger) Cache
type CompressionConfig ¶
type CompressionConfig struct {
Compression string `yaml:"compression"`
}
func (*CompressionConfig) RegisterFlagsWithPrefix ¶
func (cfg *CompressionConfig) RegisterFlagsWithPrefix(f *flag.FlagSet, prefix string)
RegisterFlagsWithPrefix registers flags with provided prefix.
func (*CompressionConfig) Validate ¶
func (cfg *CompressionConfig) Validate() error
type ErroringMockCache ¶
ErroringMockCache is a mock cache that can be configured to return errors from GetMultiWithError. Useful for testing error propagation through cache wrappers.
func NewErroringMockCache ¶
func NewErroringMockCache(err error) *ErroringMockCache
NewErroringMockCache creates a new ErroringMockCache with the given error.
func (*ErroringMockCache) GetMultiWithError ¶
type InstrumentedMockCache ¶
type InstrumentedMockCache struct {
// contains filtered or unexported fields
}
InstrumentedMockCache is a mocked cache implementation which also tracks the number of times its functions are called.
func NewInstrumentedMockCache ¶
func NewInstrumentedMockCache() *InstrumentedMockCache
NewInstrumentedMockCache makes a new InstrumentedMockCache.
func (*InstrumentedMockCache) Advance ¶
func (m *InstrumentedMockCache) Advance(d time.Duration)
Advance changes "now" by the given duration
func (*InstrumentedMockCache) CountDeleteCalls ¶
func (m *InstrumentedMockCache) CountDeleteCalls() int
func (*InstrumentedMockCache) CountFetchCalls ¶
func (m *InstrumentedMockCache) CountFetchCalls() int
func (*InstrumentedMockCache) CountStoreCalls ¶
func (m *InstrumentedMockCache) CountStoreCalls() int
func (*InstrumentedMockCache) Delete ¶
func (m *InstrumentedMockCache) Delete(ctx context.Context, key string) error
func (*InstrumentedMockCache) Flush ¶
func (m *InstrumentedMockCache) Flush()
Flush removes all entries from the cache
func (*InstrumentedMockCache) GetItems ¶
func (m *InstrumentedMockCache) GetItems() map[string]Item
func (*InstrumentedMockCache) GetMultiWithError ¶
func (*InstrumentedMockCache) Name ¶
func (m *InstrumentedMockCache) Name() string
func (*InstrumentedMockCache) SetAsync ¶
func (m *InstrumentedMockCache) SetAsync(key string, value []byte, ttl time.Duration)
func (*InstrumentedMockCache) SetMultiAsync ¶
func (m *InstrumentedMockCache) SetMultiAsync(data map[string][]byte, ttl time.Duration)
func (*InstrumentedMockCache) Stop ¶
func (m *InstrumentedMockCache) Stop()
type LRUCache ¶
type LRUCache struct {
// contains filtered or unexported fields
}
func WrapWithLRUCache ¶
func WrapWithLRUCache(c Cache, name string, reg prometheus.Registerer, lruSize int, defaultTTL time.Duration, logger log.Logger) (*LRUCache, error)
WrapWithLRUCache wraps a given `Cache` c with a LRU cache. The LRU cache will always store items in both caches. However it will only fetch items from the underlying cache if the LRU cache doesn't have the item. Items fetched from the underlying cache will be stored in the LRU cache with a default TTL. The LRU cache will also remove items from the underlying cache if they are expired. The LRU cache is limited in number of items using `lruSize`. This means this cache is not tailored for large items or items that have a big variation in size.
func (*LRUCache) GetMultiWithError ¶
func (*LRUCache) SetMultiAsync ¶
type MemcachedClient ¶
type MemcachedClient struct {
// contains filtered or unexported fields
}
func NewMemcachedClientWithConfig ¶
func NewMemcachedClientWithConfig(logger log.Logger, name string, config MemcachedClientConfig, reg prometheus.Registerer) (*MemcachedClient, error)
NewMemcachedClientWithConfig makes a new MemcachedClient.
func (*MemcachedClient) CompareAndSwap ¶
func (*MemcachedClient) Delete ¶
func (c *MemcachedClient) Delete(ctx context.Context, key string) error
func (*MemcachedClient) GetMultiWithError ¶
func (*MemcachedClient) Name ¶
func (c *MemcachedClient) Name() string
func (*MemcachedClient) SetAsync ¶
func (c *MemcachedClient) SetAsync(key string, value []byte, ttl time.Duration)
func (*MemcachedClient) SetMultiAsync ¶
func (c *MemcachedClient) SetMultiAsync(data map[string][]byte, ttl time.Duration)
func (*MemcachedClient) Stop ¶
func (c *MemcachedClient) Stop()
type MemcachedClientConfig ¶
type MemcachedClientConfig struct {
// Addresses specifies the list of memcached addresses. The addresses get
// resolved with the DNS provider.
Addresses flagext.StringSliceCSV `yaml:"addresses"`
// AddressesLookupPeriod specifies how often addresses are resolved with the
// DNS provider.
AddressesLookupPeriod time.Duration `yaml:"addresses_lookup_period" category:"advanced"`
// AddressesLookupPoolSize specifies how many idle connections to the DNS provider
// are kept open. Use 0 to disable keeping any idle connections open.
AddressesLookupPoolSize uint `yaml:"addresses_lookup_pool_size" category:"advanced"`
// Timeout specifies the socket read/write timeout.
Timeout time.Duration `yaml:"timeout"`
// ConnectTimeout specifies the connection timeout.
ConnectTimeout time.Duration `yaml:"connect_timeout"`
// MinIdleConnectionsHeadroomPercentage specifies the minimum number of idle connections
// to keep open as a percentage of the number of recently used idle connections.
// If negative, idle connections are kept open indefinitely.
MinIdleConnectionsHeadroomPercentage float64 `yaml:"min_idle_connections_headroom_percentage" category:"advanced"`
// MaxIdleConnections specifies the maximum number of idle connections that
// will be maintained per address. For better performances, this should be
// set to a number higher than your peak parallel requests.
MaxIdleConnections int `yaml:"max_idle_connections" category:"advanced"`
// MaxAsyncConcurrency specifies the maximum number of SetAsync goroutines.
MaxAsyncConcurrency int `yaml:"max_async_concurrency" category:"advanced"`
// MaxAsyncBufferSize specifies the queue buffer size for SetAsync operations.
MaxAsyncBufferSize int `yaml:"max_async_buffer_size" category:"advanced"`
// MaxGetMultiConcurrency specifies the maximum number of concurrent GetMulti() operations.
// If set to 0, concurrency is unlimited.
MaxGetMultiConcurrency int `yaml:"max_get_multi_concurrency" category:"advanced"`
// MaxGetMultiBatchSize specifies the maximum number of keys a single underlying
// GetMulti() should run. If more keys are specified, internally keys are split
// into multiple batches and fetched concurrently, honoring MaxGetMultiConcurrency parallelism.
// If set to 0, the max batch size is unlimited.
MaxGetMultiBatchSize int `yaml:"max_get_multi_batch_size" category:"advanced"`
// MaxItemSize specifies the maximum size of an item stored in memcached, in bytes.
// Items bigger than MaxItemSize are skipped. If set to 0, no maximum size is enforced.
MaxItemSize int `yaml:"max_item_size" category:"advanced"`
// TLSEnabled enables connecting to Memcached with TLS.
TLSEnabled bool `yaml:"tls_enabled" category:"advanced"`
// TLS to use to connect to the Memcached server.
TLS dstls.ClientConfig `yaml:",inline"`
}
MemcachedClientConfig is the config accepted by RemoteCacheClient.
func (*MemcachedClientConfig) RegisterFlagsWithPrefix ¶
func (c *MemcachedClientConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet)
func (*MemcachedClientConfig) Validate ¶
func (c *MemcachedClientConfig) Validate() error
type MemcachedJumpHashSelector ¶
type MemcachedJumpHashSelector struct {
// contains filtered or unexported fields
}
MemcachedJumpHashSelector implements the memcache.ServerSelector interface, utilizing 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 (ie. Kubernetes statefulsets).
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 sharded 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 MockCache ¶
type MockCache struct {
// contains filtered or unexported fields
}
func NewMockCache ¶
func NewMockCache() *MockCache
func (*MockCache) GetMultiWithError ¶
func (*MockCache) SetMultiAsync ¶
type Option ¶
type Option func(opts *Options)
Option is a callback used to modify the Options that a particular client method uses.
func WithAllocator ¶
WithAllocator creates a new Option that makes use of a specific memory Allocator for cache result values.
type Options ¶
type Options struct {
Alloc Allocator
}
Options are used to modify the behavior of an individual call to get results from a cache backend. They are constructed by applying Option callbacks passed to a client method to a default Options instance.
type SnappyCache ¶
type SnappyCache struct {
// contains filtered or unexported fields
}
func NewSnappy ¶
func NewSnappy(next Cache, logger log.Logger) *SnappyCache
NewSnappy makes a new snappy encoding cache wrapper.
func (*SnappyCache) Delete ¶
func (s *SnappyCache) Delete(ctx context.Context, key string) error
Delete implements Cache.
func (*SnappyCache) GetMulti ¶
func (s *SnappyCache) GetMulti(ctx context.Context, keys []string, opts ...Option) map[string][]byte
GetMulti implements Cache.
func (*SnappyCache) GetMultiWithError ¶
func (s *SnappyCache) GetMultiWithError(ctx context.Context, keys []string, opts ...Option) (map[string][]byte, error)
GetMultiWithError implements Cache.
func (*SnappyCache) SetAsync ¶
func (s *SnappyCache) SetAsync(key string, value []byte, ttl time.Duration)
SetAsync implements Cache.
func (*SnappyCache) SetMultiAsync ¶
func (s *SnappyCache) SetMultiAsync(data map[string][]byte, ttl time.Duration)
SetMultiAsync implements Cache.
type SpanlessTracingCache ¶
type SpanlessTracingCache struct {
// contains filtered or unexported fields
}
SpanlessTracingCache wraps a Cache and logs Fetch operation in the parent spans.
func NewSpanlessTracingCache ¶
func NewSpanlessTracingCache(cache Cache, logger log.Logger, resolver spanlogger.TenantResolver) *SpanlessTracingCache
func (*SpanlessTracingCache) Delete ¶
func (t *SpanlessTracingCache) Delete(ctx context.Context, key string) error
func (*SpanlessTracingCache) GetMultiWithError ¶
func (*SpanlessTracingCache) Name ¶
func (t *SpanlessTracingCache) Name() string
func (*SpanlessTracingCache) SetAsync ¶
func (t *SpanlessTracingCache) SetAsync(key string, value []byte, ttl time.Duration)
func (*SpanlessTracingCache) SetMultiAsync ¶
func (t *SpanlessTracingCache) SetMultiAsync(data map[string][]byte, ttl time.Duration)
func (*SpanlessTracingCache) Stop ¶
func (t *SpanlessTracingCache) Stop()
type Versioned ¶
type Versioned struct {
// contains filtered or unexported fields
}
Versioned cache adds a version prefix to the keys. This allows cache keys to be changed in a newer version of the code (after a bugfix or a cached data format change).
func NewVersioned ¶
NewVersioned creates a new Versioned cache.