cache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2022 License: Apache-2.0 Imports: 9 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 (
	// ErrorNotFound error returns the key value not found in the cache
	ErrorNotFound = 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(t, addr string) error

func NewContext

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

func Register

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

Types

type Cache

type Cache interface {
	Contain(ctx context.Context, key string) bool
	Delete(ctx context.Context, key string) error
	Fetch(ctx context.Context, key string, value interface{}) error
	Ping(ctx context.Context) error
	Save(ctx context.Context, key string, value interface{}, expiration ...time.Duration) error
	Keys(ctx context.Context, prefixes ...string) ([]string, error)
}

func Default

func Default() Cache

func FromContext

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

func New

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

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 interface for cache

func DefaultCodec

func DefaultCodec() Codec

type Option

type Option func(*Options)

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
	Codec      Codec
	Expiration time.Duration
	Prefix     string
}

func (*Options) Key

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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