cache

package
v3.1.0-rc.1.0...-6d180dd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2026 License: AGPL-3.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes returns a byte slice aliasing s without a copy.

The returned slice shares storage with s, so callers MUST NOT write to it — mutating the bytes would violate the immutability of the source string and is undefined behavior. Use this only when you immediately read the result; if you need to retain or modify the data, copy it first.

Types

type BackgroundConfig

type BackgroundConfig struct {
	WriteBackGoroutines int `yaml:"writeback_goroutines"`
	WriteBackBuffer     int `yaml:"writeback_buffer"`
}

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)
	Remove(ctx context.Context, key []string)
	MaxItemSize() int
	Fetch(ctx context.Context, keys []string) (found []string, bufs [][]byte, missing []string)
	FetchKey(ctx context.Context, key string) (buf []byte, found bool)
	// Release allows compliant implementations to reclaim buffers back into a pool for memory efficiency
	Release([]byte)
	Stop()
}

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 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 NewProbabilistic

func NewProbabilistic(cache Cache, probability float64) Cache

NewProbabilistic returns a Cache that forwards each Store call with the given probability. Probability must be between 0 and 1, inclusive.

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, maxItemSize int, reg prometheus.Registerer, logger log.Logger) *Memcached

NewMemcached makes a new Memcached.

func (*Memcached) Fetch

func (c *Memcached) 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 (*Memcached) FetchKey

func (c *Memcached) FetchKey(ctx context.Context, key string) ([]byte, bool)

FetchKey gets a single key from the cache

func (*Memcached) MaxItemSize

func (c *Memcached) MaxItemSize() int

func (*Memcached) Release

func (c *Memcached) Release(buf []byte)

func (*Memcached) Remove

func (c *Memcached) Remove(ctx context.Context, keys []string)

Remove deletes the given keys from the cache.

func (*Memcached) Stop

func (c *Memcached) Stop()

func (*Memcached) Store

func (c *Memcached) Store(ctx context.Context, keys []string, bufs [][]byte)

Store stores the key in the cache.

type MemcachedClient

type MemcachedClient interface {
	GetMulti(ctx context.Context, keys []string, opts ...memcache.Option) (map[string]*memcache.Item, error)
	Set(item *memcache.Item) error
	Get(key string, opts ...memcache.Option) (*memcache.Item, error)
	Delete(key string) error
	Close()
}

MemcachedClient interface exists for mocking memcacheClient.

func NewMemcachedClient

func NewMemcachedClient(cfg MemcachedClientConfig, name string, r prometheus.Registerer, logger log.Logger) 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"` // EXPERIMENTAL.
	Timeout        time.Duration `yaml:"timeout"`
	ConnectTimeout time.Duration `yaml:"connect_timeout"` // Maximum time to establish a connection. If 0, Timeout is used.
	MaxIdleConns   int           `yaml:"max_idle_conns"`
	// MinIdleConnsHeadroomPercentage is the percentage of idle connections to keep open,
	// relative to the number of recently used connections, when reaping idle connections.
	// If negative, idle connections are never closed. If 0, all connections idle for longer
	// than 2 minutes are closed.
	MinIdleConnsHeadroomPercentage float64       `yaml:"min_idle_conns_headroom_percentage"`
	RecentlyUsedConnsThreshold     time.Duration `yaml:"recently_used_conns_threshold"` // How long a free connection counts as recently used when reaping idle connections. If 0, 2 minutes is used.
	MaxItemSize                    int           `yaml:"max_item_size"`
	UpdateInterval                 time.Duration `yaml:"update_interval"`
	ConsistentHash                 bool          `yaml:"consistent_hash"`
	// PinServers selects this many proxy pods per Tempo pod, independently of
	// cache key. Use only when the proxies share the same backend cache pool.
	PinServers uint               `yaml:"pin_servers"`
	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 bool               `yaml:"tls_enabled"`              // Enable connecting to Memcached with TLS.
	TLS        dstls.ClientConfig `yaml:",inline"`                  // TLS to use to connect to the Memcached server.
}

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"`
}

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 Provider

type Provider interface {
	services.Service

	CacheFor(role Role) Cache
	AddCache(role Role, c Cache) error
}

Provider is an object that can return a cache for a requested role

type RedisCache

type RedisCache struct {
	// contains filtered or unexported fields
}

RedisCache type caches chunks in redis

func NewRedisCache

func NewRedisCache(name string, redisClient *RedisClient, maxItemSizeBytes int, reg prometheus.Registerer, logger log.Logger) *RedisCache

NewRedisCache creates a new RedisCache

func (*RedisCache) Fetch

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) FetchKey

func (c *RedisCache) FetchKey(ctx context.Context, key string) (buf []byte, found bool)

Fetch gets a single keys from the cache

func (*RedisCache) MaxItemSize

func (c *RedisCache) MaxItemSize() int

func (*RedisCache) Release

func (c *RedisCache) Release(_ []byte)

func (*RedisCache) Remove

func (c *RedisCache) Remove(ctx context.Context, keys []string)

Remove deletes the given keys from the cache.

func (*RedisCache) Stop

func (c *RedisCache) Stop()

Stop stops the redis client.

func (*RedisCache) Store

func (c *RedisCache) Store(ctx context.Context, keys []string, bufs [][]byte)

Store stores the key in the cache.

type RedisClient

type RedisClient struct {
	// contains filtered or unexported fields
}

func NewRedisClient

func NewRedisClient(cfg *RedisConfig, name string, reg prometheus.Registerer) (*RedisClient, error)

NewRedisClient creates Redis client. It fails closed if cfg.TLSEnabled is set but the TLS configuration cannot be assembled — silently falling back to a cleartext connection would violate the operator's intent.

Routing is explicit: cfg.SingleNode chooses redis.NewClient; otherwise redis.NewClusterClient. The previous UniversalClient-based selection inferred routing from address count, which silently picked single-node for cluster endpoints that expanded to a single host (e.g. AWS ElastiCache configuration endpoints).

func (*RedisClient) Close

func (c *RedisClient) Close() error

func (*RedisClient) Del

func (c *RedisClient) Del(ctx context.Context, keys []string) error

func (*RedisClient) Get

func (c *RedisClient) Get(ctx context.Context, key string) ([]byte, error)

func (*RedisClient) MGet

func (c *RedisClient) MGet(ctx context.Context, keys []string) ([][]byte, error)

func (*RedisClient) MSet

func (c *RedisClient) MSet(ctx context.Context, keys []string, values [][]byte) error

func (*RedisClient) Ping

func (c *RedisClient) Ping(ctx context.Context) error

type RedisConfig

type RedisConfig struct {
	Endpoint   string         `yaml:"endpoint"`
	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"`

	// SingleNode opts into a single-node Redis client. Default (false) targets
	// a Redis Cluster — the YAML key is inverted so its Go zero value matches
	// the desired default and a missing field can never silently flip routing.
	SingleNode bool `yaml:"single_node"`

	TLSEnabled bool               `yaml:"tls_enabled"`
	TLS        dstls.ClientConfig `yaml:",inline"`

	ConnMaxIdleTime time.Duration `yaml:"conn_max_idle_time"`
	ConnMaxLifetime time.Duration `yaml:"conn_max_lifetime"`

	// Cluster-only routing options. Ignored on the single-node path.
	RouteByLatency bool `yaml:"route_by_latency"`
	RouteRandomly  bool `yaml:"route_randomly"`
	ReadOnly       bool `yaml:"read_only"`
	MaxRedirects   int  `yaml:"max_redirects"`
	MinIdleConns   int  `yaml:"min_idle_conns"`

	// MaxItemSize is the maximum size in bytes of an item stored in Redis.
	// Items larger than this are not stored. A value of 0 disables the limit.
	MaxItemSize int `yaml:"max_item_size"`
}

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 Role

type Role string
const (
	// individual roles
	RoleNone             Role = "none"
	RoleBloom            Role = "bloom"
	RoleTraceIDIdx       Role = "trace-id-index"
	RoleParquetFooter    Role = "parquet-footer"
	RoleParquetColumnIdx Role = "parquet-column-idx"
	RoleParquetOffsetIdx Role = "parquet-offset-idx"
	RoleFrontendSearch   Role = "frontend-search"
	RoleParquetPage      Role = "parquet-page"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址