cache

package
v0.36.4 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2018 License: MIT Imports: 22 Imported by: 4

Documentation

Overview

Package cache implements the types.Cache interface for storing and retrieving key/value pairs from a range of storage strategies.

Index

Constants

View Source
const (
	TypeDynamoDB  = "dynamodb"
	TypeMemcached = "memcached"
	TypeMemory    = "memory"
	TypeRedis     = "redis"
)

String constants representing each cache type.

Variables

View Source
var Constructors = map[string]TypeSpec{}

Constructors is a map of all cache types with their specs.

Functions

func Descriptions

func Descriptions() string

Descriptions returns a formatted string of descriptions for each type.

func New

func New(
	conf Config,
	mgr types.Manager,
	log log.Modular,
	stats metrics.Type,
) (types.Cache, error)

New creates a cache type based on an cache configuration.

func NewDynamoDB added in v0.28.0

func NewDynamoDB(conf Config, mgr types.Manager, log log.Modular, stats metrics.Type) (types.Cache, error)

NewDynamoDB creates a new DynamoDB cache type.

func NewMemcached

func NewMemcached(
	conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (types.Cache, error)

NewMemcached returns a Memcached processor.

func NewMemory

func NewMemory(conf Config, mgr types.Manager, log log.Modular, stats metrics.Type) (types.Cache, error)

NewMemory creates a new Memory cache type.

func NewRedis added in v0.34.7

func NewRedis(
	conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (types.Cache, error)

NewRedis returns a Redis processor.

func SanitiseConfig added in v0.16.2

func SanitiseConfig(conf Config) (interface{}, error)

SanitiseConfig creates a sanitised version of a config.

Types

type Config

type Config struct {
	Type      string          `json:"type" yaml:"type"`
	DynamoDB  DynamoDBConfig  `json:"dynamodb" yaml:"dynamodb"`
	Memcached MemcachedConfig `json:"memcached" yaml:"memcached"`
	Memory    MemoryConfig    `json:"memory" yaml:"memory"`
	Redis     RedisConfig     `json:"redis" yaml:"redis"`
}

Config is the all encompassing configuration struct for all cache types.

func NewConfig

func NewConfig() Config

NewConfig returns a configuration struct fully populated with default values.

func (*Config) UnmarshalJSON

func (c *Config) UnmarshalJSON(bytes []byte) error

UnmarshalJSON ensures that when parsing configs that are in a map or slice the default values are still applied.

func (*Config) UnmarshalYAML

func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML ensures that when parsing configs that are in a map or slice the default values are still applied.

type DynamoDB added in v0.28.0

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

DynamoDB is a DynamoDB based cache implementation.

func (*DynamoDB) Add added in v0.28.0

func (d *DynamoDB) Add(key string, value []byte) error

Add attempts to set the value of a key only if the key does not already exist and returns an error if the key already exists.

func (*DynamoDB) Delete added in v0.28.0

func (d *DynamoDB) Delete(key string) error

Delete attempts to remove a key.

func (*DynamoDB) Get added in v0.28.0

func (d *DynamoDB) Get(key string) ([]byte, error)

Get attempts to locate and return a cached value by its key, returns an error if the key does not exist.

func (*DynamoDB) Set added in v0.28.0

func (d *DynamoDB) Set(key string, value []byte) error

Set attempts to set the value of a key.

func (*DynamoDB) SetMulti added in v0.35.0

func (d *DynamoDB) SetMulti(items map[string][]byte) error

SetMulti attempts to set the value of multiple keys, if any keys fail to be set an error is returned.

type DynamoDBConfig added in v0.28.0

type DynamoDBConfig struct {
	ConsistentRead bool   `json:"consistent_read" yaml:"consistent_read"`
	DataKey        string `json:"data_key" yaml:"data_key"`
	HashKey        string `json:"hash_key" yaml:"hash_key"`
	Table          string `json:"table" yaml:"table"`
	TTL            string `json:"ttl" yaml:"ttl"`
	TTLKey         string `json:"ttl_key" yaml:"ttl_key"`
	retries.Config `json:",inline" yaml:",inline"`
	// contains filtered or unexported fields
}

DynamoDBConfig contains config fields for the DynamoDB cache type.

func NewDynamoDBConfig added in v0.28.0

func NewDynamoDBConfig() DynamoDBConfig

NewDynamoDBConfig creates a MemoryConfig populated with default values.

type Memcached

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

Memcached is a cache that connects to memcached servers.

func (*Memcached) Add

func (m *Memcached) Add(key string, value []byte) error

Add attempts to set the value of a key only if the key does not already exist and returns an error if the key already exists or if the operation fails.

func (*Memcached) Delete

func (m *Memcached) Delete(key string) error

Delete attempts to remove a key.

func (*Memcached) Get

func (m *Memcached) Get(key string) ([]byte, error)

Get attempts to locate and return a cached value by its key, returns an error if the key does not exist or if the operation failed.

func (*Memcached) Set

func (m *Memcached) Set(key string, value []byte) error

Set attempts to set the value of a key.

func (*Memcached) SetMulti added in v0.35.0

func (m *Memcached) SetMulti(items map[string][]byte) error

SetMulti attempts to set the value of multiple keys, returns an error if any keys fail.

type MemcachedConfig

type MemcachedConfig struct {
	Addresses     []string `json:"addresses" yaml:"addresses"`
	Prefix        string   `json:"prefix" yaml:"prefix"`
	TTL           int32    `json:"ttl" yaml:"ttl"`
	Retries       int      `json:"retries" yaml:"retries"`
	RetryPeriodMS int      `json:"retry_period_ms" yaml:"retry_period_ms"`
}

MemcachedConfig is a config struct for a memcached connection.

func NewMemcachedConfig

func NewMemcachedConfig() MemcachedConfig

NewMemcachedConfig returns a MemcachedConfig with default values.

type Memory

type Memory struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Memory is a memory based cache implementation.

func (*Memory) Add

func (m *Memory) Add(key string, value []byte) error

Add attempts to set the value of a key only if the key does not already exist and returns an error if the key already exists.

func (*Memory) Delete

func (m *Memory) Delete(key string) error

Delete attempts to remove a key.

func (*Memory) Get

func (m *Memory) Get(key string) ([]byte, error)

Get attempts to locate and return a cached value by its key, returns an error if the key does not exist.

func (*Memory) Set

func (m *Memory) Set(key string, value []byte) error

Set attempts to set the value of a key.

func (*Memory) SetMulti added in v0.35.0

func (m *Memory) SetMulti(items map[string][]byte) error

SetMulti attempts to set the value of multiple keys, returns an error if any keys fail.

type MemoryConfig

type MemoryConfig struct {
	TTL                 int `json:"ttl" yaml:"ttl"`
	CompactionIntervalS int `json:"compaction_interval_s" yaml:"compaction_interval_s"`
}

MemoryConfig contains config fields for the Memory cache type.

func NewMemoryConfig

func NewMemoryConfig() MemoryConfig

NewMemoryConfig creates a MemoryConfig populated with default values.

type Redis added in v0.34.7

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

Redis is a cache that connects to redis servers.

func (*Redis) Add added in v0.34.7

func (r *Redis) Add(key string, value []byte) error

Add attempts to set the value of a key only if the key does not already exist and returns an error if the key already exists or if the operation fails.

func (*Redis) Delete added in v0.34.7

func (r *Redis) Delete(key string) error

Delete attempts to remove a key.

func (*Redis) Get added in v0.34.7

func (r *Redis) Get(key string) ([]byte, error)

Get attempts to locate and return a cached value by its key, returns an error if the key does not exist or if the operation failed.

func (*Redis) Set added in v0.34.7

func (r *Redis) Set(key string, value []byte) error

Set attempts to set the value of a key.

func (*Redis) SetMulti added in v0.35.0

func (r *Redis) SetMulti(items map[string][]byte) error

SetMulti attempts to set the value of multiple keys, returns an error if any keys fail.

type RedisConfig added in v0.34.7

type RedisConfig struct {
	URL           string `json:"url" yaml:"url"`
	Prefix        string `json:"prefix" yaml:"prefix"`
	Expiration    string `json:"expiration" yaml:"expiration"`
	Retries       int    `json:"retries" yaml:"retries"`
	RetryPeriodMS int    `json:"retry_period_ms" yaml:"retry_period_ms"`
}

RedisConfig is a config struct for a redis connection.

func NewRedisConfig added in v0.34.7

func NewRedisConfig() RedisConfig

NewRedisConfig returns a RedisConfig with default values.

type TypeSpec

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

TypeSpec is a constructor and a usage description for each cache type.

Jump to

Keyboard shortcuts

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