cache

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2024 License: MIT Imports: 5 Imported by: 2

Documentation

Overview

Package cache is an interface for distributed data cache.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned when a key doesn't exist
	ErrNotFound = errors.New("not found")
	// DefaultCache is the memory cache.
	DefaultCache Cache

	Flag = pflag.NewFlagSet("cache", pflag.ExitOnError)
)

Functions

func Del

func Del(ctx context.Context, key string, opts ...DelOption) error

Del removes the record with the corresponding key from the DefaultCache.

func List

func List(ctx context.Context, opts ...ListOption) ([]string, error)

List returns any keys from the DefaultCache

func Put

func Put(ctx context.Context, r *Record, opts ...PutOption) error

Put writes a record to the DefaultCache,

Types

type Cache

type Cache interface {
	// Init initialises the cache. It must perform any required setup on the backing storage
	// implementation and check that it is ready for use, returning any errors.
	Init(...Option) error
	// Options allows you to view the current options.
	Options() Options
	// Get takes a single key name and optional GetOptions. It returns matching []*Record or an error.
	Get(ctx context.Context, key string, opts ...GetOption) ([]*Record, error)
	// Put writes a record to the cache, and returns an error if the record was not written.
	Put(ctx context.Context, r *Record, opts ...PutOption) error
	// Del removes the record with the corresponding key from the cache.
	Del(ctx context.Context, key string, opts ...DelOption) error
	// List returns any keys that match, or an empty list with no error if none matched.
	List(ctx context.Context, opts ...ListOption) ([]string, error)
	// Close the cache
	Close() error
	// String returns the name of the implementation.
	String() string
}

Cache is a data cache interface

type DelOption

type DelOption func(d *DelOptions)

DelOption sets values in DelOptions

func DelFrom

func DelFrom(database, table string) DelOption

DelFrom the database and table

type DelOptions

type DelOptions struct {
	Database, Table string
}

DelOptions configures an individual Del operation

type GetOption

type GetOption func(r *GetOptions)

GetOption sets values in GetOptions

func GetFrom

func GetFrom(database, table string) GetOption

GetFrom the database and table

func GetLimit

func GetLimit(l uint) GetOption

GetLimit limits the number of responses to l

func GetOffset

func GetOffset(o uint) GetOption

GetOffset starts returning responses from o. Use in conjunction with Limit for pagination

func GetPrefix

func GetPrefix() GetOption

GetPrefix returns all records that are prefixed with key

func GetSuffix

func GetSuffix() GetOption

GetSuffix returns all records that have the suffix key

type GetOptions

type GetOptions struct {
	Database, Table string
	// Prefix returns all records that are prefixed with key
	Prefix bool
	// Suffix returns all records that have the suffix key
	Suffix bool
	// Limit limits the number of returned records
	Limit uint
	// Offset when combined with Limit supports pagination
	Offset uint
}

GetOptions configures an individual Get operation

type ListOption

type ListOption func(l *ListOptions)

ListOption sets values in ListOptions

func ListFrom

func ListFrom(database, table string) ListOption

ListFrom the database and table

func ListLimit

func ListLimit(l uint) ListOption

ListLimit limits the number of returned keys to l

func ListOffset

func ListOffset(o uint) ListOption

ListOffset starts returning responses from o. Use in conjunction with Limit for pagination.

func ListPrefix

func ListPrefix(p string) ListOption

ListPrefix returns all keys that are prefixed with key

func ListSuffix

func ListSuffix(s string) ListOption

ListSuffix returns all keys that end with key

type ListOptions

type ListOptions struct {
	// List from the following
	Database, Table string
	// Prefix returns all keys that are prefixed with key
	Prefix string
	// Suffix returns all keys that end with key
	Suffix string
	// Limit limits the number of returned keys
	Limit uint
	// Offset when combined with Limit supports pagination
	Offset uint
}

ListOptions configures an individual List operation

type Option

type Option func(o *Options)

Option sets values in Options

func Database

func Database(db string) Option

Database allows multiple isolated stores to be kept in one backend, if supported.

func Nodes

func Nodes(a ...string) Option

Nodes contains the addresses or other connection information of the backing storage. For example, an etcd implementation would contain the nodes of the cluster. A SQL implementation could contain one or more connection strings.

func Table

func Table(t string) Option

Table is analagous to a table in database backends or a key prefix in KV backends

func WithClient

func WithClient(c client.Client) Option

WithClient sets the stores client to use for RPC

func WithContext

func WithContext(c context.Context) Option

WithContext sets the stores context, for any extra configuration

type Options

type Options struct {
	// Nodes contains the addresses or other connection information of the backing storage.
	// For example, an etcd implementation would contain the nodes of the cluster.
	// A SQL implementation could contain one or more connection strings.
	Nodes []string
	// Database allows multiple isolated stores to be kept in one backend, if supported.
	Database string
	// Table is analagous to a table in database backends or a key prefix in KV backends
	Table string
	// Context should contain all implementation specific options, using context.WithValue.
	Context context.Context
	// Client to use for RPC
	Client client.Client
}

Options contains configuration for the Cache

type PutOption

type PutOption func(w *PutOptions)

PutOption sets values in PutOptions

func PutExpiry

func PutExpiry(t time.Time) PutOption

PutExpiry is the time the record expires

func PutTTL

func PutTTL(d time.Duration) PutOption

PutTTL is the time the record expires

func PutTo

func PutTo(database, table string) PutOption

PutTo the database and table

type PutOptions

type PutOptions struct {
	Database, Table string
	// Expiry is the time the record expires
	Expiry time.Time
	// TTL is the time until the record expires
	TTL time.Duration
}

PutOptions configures an individual Put operation If Expiry and TTL are set TTL takes precedence

type Record

type Record struct {
	// The key to cache the record
	Key string `json:"key"`
	// The value within the record
	Value []byte `json:"value"`
	// Any associated metadata for indexing
	Metadata map[string]interface{} `json:"metadata"`
	// Time to expire a record: TODO: change to timestamp
	Expiry time.Duration `json:"expiry,omitempty"`
}

Record is an item cached or retrieved from a Cache

func Get

func Get(ctx context.Context, key string, opts ...GetOption) ([]*Record, error)

Get takes a single key to DefaultCache

Directories

Path Synopsis
Package memory is a in-memory cache cache
Package memory is a in-memory cache cache

Jump to

Keyboard shortcuts

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