Documentation ¶
Index ¶
- Constants
- Variables
- type Cached
- func (c *Cached) Delete(ctx context.Context, key string) bool
- func (c *Cached) Flush(ctx context.Context) error
- func (c *Cached) Get(ctx context.Context, key string) []byte
- func (c *Cached) Keys(ctx context.Context, prefix string) []string
- func (c *Cached) Purge(ctx context.Context, pattern string) error
- func (c *Cached) Set(key string, value []byte, ttl time.Duration)
- func (c *Cached) Size() int
- type Entry
- type InMemoryCacheConfig
- type Iterator
- type Options
- type Provider
- type ProviderBackendConfig
- type RedisCache
- func (c RedisCache) Delete(ctx context.Context, key string) bool
- func (c RedisCache) Flush(ctx context.Context) error
- func (c RedisCache) Get(ctx context.Context, key string) []byte
- func (c RedisCache) Keys(ctx context.Context, prefix string) []string
- func (c RedisCache) Purge(ctx context.Context, pattern string) error
- func (c RedisCache) Set(key string, value []byte, ttl time.Duration)
- func (c RedisCache) Size() int
- type RedisClientConfig
- type RemoteCacheClient
- type SimpleOptions
Constants ¶
const ( BackendInMemory = "inmemory" BackendRedis = "redis" )
Variables ¶
var ( ErrRedisConfigNoEndpoint = errors.New("no redis endpoint configured") ErrRedisMaxQueueConcurrency = errors.New("max job queue concurrency must be positive") ErrRedisMaxItemSize = errors.New("max item size exceeded") ErrRedisJobQueueFull = errors.New("job queue is full") )
var ( // DefaultCreateTime is the create time used by all entries in the cache. DefaultCreateTime = time.Time{} )
var DefaultInMemoryCacheConfig = InMemoryCacheConfig{
MaxSize: 1 << 28,
MaxItemSize: 1 << 27,
DefaultTTL: "120s",
}
DefaultInMemoryCacheConfig provides default config values for the cache.
Functions ¶
This section is empty.
Types ¶
type Cached ¶
type Cached struct {
// contains filtered or unexported fields
}
Cached is the two-tiered cache provider, adding a caching layer on top of a `Provider“.
func NewCached ¶
func NewCached(cache Provider, name string, ttl time.Duration, config InMemoryCacheConfig) (*Cached, error)
NewCached adds a caching layer on top of a cache `Provider` (typically a remote cache) and wraps it with a local in-memory cache. Items will always be stored in both caches. Fetches are only satified by the underlying remote cache, if the item does not exist in the local cache. The local cache will remove items, depending on the capacity constraints of the cache or the lifetime constraints of the cached item, respectively.
func (*Cached) Get ¶
Get retrieves an element based on a key, returning nil if the element does not exist.
type Entry ¶
type Entry interface { // Key represents the key. Key() string // Value represents the value. Value() []byte // CreateTime represents the time when the entry is created. CreateTime() time.Time }
Entry represents a key-value entry within the map.
type InMemoryCacheConfig ¶
type InMemoryCacheConfig struct { // MaxSize is the overall maximum number of bytes the cache can hold. MaxSize uint64 `yaml:"max_size"` // MaxItemSize is the maximum size of a single item. MaxItemSize uint64 `yaml:"max_item_size"` // DefaultTTL is the defautl ttl of a single item. DefaultTTL string `yaml:"default_ttl"` // TTLEviction specifies if evction of items by TTL is enabled. // Set to true if`DefaultTTL` is -1. TTLEviction bool }
InMemoryCacheConfig holds the in-memory cache config.
func (*InMemoryCacheConfig) Sanitize ¶
func (c *InMemoryCacheConfig) Sanitize()
Sanitize checks the config and adds defaults to missing values.
type Iterator ¶
type Iterator interface { // HasNext return true if there is more items to be returned. HasNext() bool // Next return the next item. Next() Entry // Close closes the iterator // and releases any allocated resources. Close() }
Iterator represents the interface for cache iterators.
type Options ¶
type Options struct { // TTL controls the time-to-live for a given cache entry. // Cache entries that are older than the TTL will not be returned. TTL time.Duration // InitialCapacity controls the initial capacity of the cache. InitialCapacity int }
Options control the behavior of the cache.
type Provider ¶
type Provider interface { // Get retrieves an element based on a key, returning nil if the element // does not exist. Get(ctx context.Context, key string) []byte // Set adds an element to the cache. Set(key string, value []byte, ttl time.Duration) // Delete deletes an element in the cache. Delete(ctx context.Context, key string) bool // Keys returns a slice of cache keys. Keys(ctx context.Context, prefix string) []string // Purge purges all keys matching the specified pattern from the cache. Purge(ctx context.Context, pattern string) error // Flush deletes all elements from the cache. Flush(ctx context.Context) error // Size returns the number of entries currently stored in the Cache. Size() int }
Provider is a generalized interface to a cache. See provider.Simple for a specific implementation.
func CreateCacheProvider ¶
func CreateCacheProvider(name string, config ProviderBackendConfig) (Provider, error)
CreateCacheProvider creates a cache backend based on the provided configuration.
func NewInMemoryCache ¶
func NewInMemoryCache(config InMemoryCacheConfig) (Provider, error)
NewInMemoryCache creates a new thread-safe LRU in memory cache. It ensures the total cache size approximately does not exceed maxBytes.
func NewSimpleCache ¶
func NewSimpleCache(opts *SimpleOptions) (Provider, error)
NewSimpleCache creates a new simple cache with given options. Simple cache will never evict entries and it will never reorder the elements.
type ProviderBackendConfig ¶
type ProviderBackendConfig struct { Backend string `yaml:"backend"` Layered bool `yaml:"layered"` LayeredTTL string `yaml:"layered_ttl"` InMemory InMemoryCacheConfig `yaml:"inmemory"` Redis RedisClientConfig `yaml:"redis"` }
ProviderBackendConfig holds the configuration for the caching provider backend.
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache is a Redis-based cache.
func NewRedisCache ¶
func NewRedisCache(name string, client RemoteCacheClient) *RedisCache
NewRedisCache makes a new RedisCache.
func (RedisCache) Get ¶
Get retrieves an element based on a key, returning nil if the element does not exist.
type RedisClientConfig ¶
type RedisClientConfig struct { // Endpoint holds the endpoint addresses of the Redis server. // Either a single address or a list of host:port addresses // of cluster/sentinel nodes. Endpoint string `yaml:"endpoint"` // Username to authenticate the current connection with one of the connections // defined in the ACL list when connecting to a Redis 6.0 instance, or greater, // that is using the Redis ACL system. Username string `yaml:"username"` // Optional password. Must match the password specified in the // requirepass server configuration option (if connecting to a Redis 5.0 instance, or lower), // or the User Password when connecting to a Redis 6.0 instance, or greater, // that is using the Redis ACL system. Password string `yaml:"password"` // DB Database to be selected after connecting to the server. DB int `yaml:"db"` // MaxItemSize specifies the maximum size of an item stored in Redis. // Items bigger than MaxItemSize are skipped. If set to 0, no maximum size is enforced. MaxItemSize int `yaml:"max_item_size"` // MaxQueueBufferSize is the maximum number of enqueued job operations allowed. MaxQueueBufferSize int `yaml:"max_queue_buffer_size"` // MaxQueueConcurrency is the maximum number of concurrent async job operations. MaxQueueConcurrency int `yaml:"max_queue_concurrency"` }
RedisClientConfig holds the configuration for the Redis client.
func (*RedisClientConfig) Validate ¶
func (c *RedisClientConfig) Validate() error
Validate validates the RedisClientConfig.
type RemoteCacheClient ¶
type RemoteCacheClient interface { // Fetch fetches a key from the remote cache. // Returns nil if an error occurs. Fetch(ctx context.Context, keys string) []byte // Store stores a key and value into the the remote cache. // Returns an error in case the operation fails. Store(key string, value []byte, ttl time.Duration) error // StoreAsync asynchronously stores a key and value into the the remote cache. StoreAsync(key string, value []byte, ttl time.Duration) error // Delete deletes a key from the remote cache. Delete(ctx context.Context, key string) error // Keys returns a slice of cache keys. Keys(ctx context.Context, prefix string) []string // Purge purges all keys matching the spedified pattern from the remote cache. Purge(ctx context.Context, pattern string) error // Flush deletes all keys from the remote cache. Flush(ctx context.Context) error // Stop closes the client connection. Stop() }
RemoteCacheClient is a generalized interface to interact with a remote cache.
func NewRedisClient ¶
func NewRedisClient(name string, config RedisClientConfig) (RemoteCacheClient, error)
NewRedisClient creates a new Redis client with the provided configuration.
type SimpleOptions ¶
type SimpleOptions struct { // InitialCapacity controls the initial capacity of the cache. InitialCapacity int }
SimpleOptions provides options that can be used to configure SimpleCache.