cache

package
v0.0.0-...-e417875 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Memory the cache name of memory
	Memory = "memory"
	// Redis the cache name of redis
	Redis = "redis"
	// RedisSentinel the cache name of redis sentinel
	RedisSentinel = "redis+sentinel"
)

Variables

View Source
var (
	// ErrNotFound error returns when the key value not found in the cache
	ErrNotFound = errors.New("key not found")
)

Functions

func FetchOrSave

func FetchOrSave(ctx context.Context, c Cache, key string, value interface{}, builder func() (interface{}, error), expiration ...time.Duration) error

FetchOrSave retrieves the value for the key if present in the cache. Otherwise, it saves the value from the builder and retrieves the value for the key again.

func Initialize

func Initialize(typ, addr string) error

Initialize initialize the default cache from the addr

func NewContext

func NewContext(ctx context.Context, c Cache) context.Context

NewContext returns new context with cache

func Register

func Register(typ string, factory func(opts Options) (Cache, error))

Register register cache factory for type

Types

type Cache

type Cache interface {
	// Contains returns true if key exists
	Contains(ctx context.Context, key string) bool

	// Delete delete item from cache by key
	Delete(ctx context.Context, key string) error

	// Fetch retrieve the cached key value
	Fetch(ctx context.Context, key string, value interface{}) error

	// Ping ping the cache
	Ping(ctx context.Context) error

	// Save cache the value by key
	Save(ctx context.Context, key string, value interface{}, expiration ...time.Duration) error

	// Scan scans the keys matched by match string
	// NOTICE: memory cache does not support use wildcard, compared by strings.Contains
	Scan(ctx context.Context, match string) (Iterator, error)
}

Cache cache interface

func Default

func Default() Cache

Default returns the default cache

func FromContext

func FromContext(ctx context.Context) (Cache, bool)

FromContext returns cache from context

func LayerCache

func LayerCache() Cache

LayerCache is the global cache instance for cache layer.

func New

func New(typ string, opt ...Option) (Cache, error)

New returns cache from addr

type Codec

type Codec interface {
	// Encode returns the encoded byte array of v.
	Encode(v interface{}) ([]byte, error)

	// Decode analyzes the encoded data and stores the result into the v.
	Decode(data []byte, v interface{}) error
}

Codec codec interface for cache

func DefaultCodec

func DefaultCodec() Codec

DefaultCodec returns default codec.

type Iterator

type Iterator interface {
	Next(ctx context.Context) bool
	Val() string
}

Iterator returns the ScanIterator

type Option

type Option func(*Options)

Option function to set the options of the cache

func Address

func Address(addr string) Option

Address sets the address

func Expiration

func Expiration(d time.Duration) Option

Expiration sets the default expiration

func Prefix

func Prefix(prefix string) Option

Prefix sets the prefix

type Options

type Options struct {
	Address    string        // the address of the cache
	Codec      Codec         // the codec for the cache
	Expiration time.Duration // the default expiration for the cache
	Prefix     string        // the prefix for all the keys in the cache
}

Options options of the cache

func (*Options) Key

func (opts *Options) Key(key string) string

Key returns the real cache key

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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