cache

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: MIT Imports: 7 Imported by: 4

Documentation

Index

Constants

View Source
const (
	CacheKindRedis = "redis"
)

Variables

View Source
var (
	ErrDriverNameMiss = errors.New("cache: driverName is empty")
	ErrCacheMiss      = errors.New("cache: key is missing")
)

Functions

func DefaultMarshalFunc added in v0.3.0

func DefaultMarshalFunc(value any) ([]byte, error)

func DefaultUnmarshalFunc added in v0.3.0

func DefaultUnmarshalFunc(b []byte, value any) error

func Del

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

func Get

func Get(ctx context.Context, key string, v any, opts ...Option) error

func Has

func Has(ctx context.Context, key string) bool

func IsNotFound

func IsNotFound(err error) bool

func RegisterCache

func RegisterCache(name string, cache Cache) error

RegisterCache registers a cache driver. It is not safe for concurrent use.

func Set

func Set(ctx context.Context, key string, v any, opts ...Option) error

func SetDefault

func SetDefault(driver string) error

SetDefault sets the default driver to use the static functions.

func UnRegisterCache added in v0.4.3

func UnRegisterCache(name string)

UnRegisterCache unregisters a cache driver. It is not safe for concurrent use.

Types

type Cache

type Cache interface {
	// Get gets the value from cache and unmarshal it to v. Make sure the value is a pointer and zero.
	Get(ctx context.Context, key string, value any, opts ...Option) error
	// Set sets the value to cache.
	Set(ctx context.Context, key string, value any, opts ...Option) error
	// Has reports whether the value for the given key exists.
	Has(ctx context.Context, key string) bool
	// Del deletes the value for the given key.
	Del(ctx context.Context, key string) error
	// IsNotFound detect the error weather not found from cache
	IsNotFound(err error) bool
}

Cache is the interface for cache.

func GetCache

func GetCache(driver string) (Cache, error)

GetCache return a registered Cache driver.

type MarshalFunc added in v0.3.0

type MarshalFunc func(any) ([]byte, error)

type Option added in v0.3.0

type Option func(*Options)

func WithGetter added in v0.3.0

func WithGetter(getter func(ctx context.Context, key string) (any, error)) Option

WithGetter sets the cache getter.

func WithGroup added in v0.3.0

func WithGroup() Option

WithGroup sets the cache Group.

func WithRaw added in v0.3.0

func WithRaw() Option

WithRaw sets the cache Raw.

func WithSetNX added in v0.3.0

func WithSetNX() Option

WithSetNX sets the cache SetNX.

func WithSetXX added in v0.3.0

func WithSetXX() Option

WithSetXX sets the cache SetXX.

func WithSkip added in v0.3.0

func WithSkip(skip SkipMode) Option

WithSkip sets the cache Skip.

func WithTTL added in v0.3.0

func WithTTL(ttl time.Duration) Option

WithTTL sets the cache expiration time.

type Options added in v0.3.0

type Options struct {
	// TTL is the cache expiration time.
	TTL time.Duration
	// Getter returns value to be cached.call getter to get a value(such as query database) and set cache using c.expiry
	Getter func(ctx context.Context, key string) (any, error)
	// SetXX only sets the key if it already exists.
	SetXX bool
	// SetNX only sets the key if it does not already exist.
	SetNX bool
	// SkipFlags indicator skip level.
	Skip SkipMode
	// Raw indicates whether to skip serialization. default is false to keep coroutine safe.
	// Caches accessed across processes are serialized, that flag Generally used for memory cache.
	//
	// false that means serialize value to Item.V. if true, Item.V is raw value but support by implemented Cache.
	// has implemented Cache: lfu cache
	Raw bool
	// Group indicates whether to singleflight.
	Group bool
}

func ApplyOptions added in v0.3.0

func ApplyOptions(opts ...Option) *Options

func (*Options) Expiration added in v0.3.0

func (o *Options) Expiration() time.Duration

type SkipMode added in v0.3.0

type SkipMode int

SkipMode controls the cache load which level from a combined cache .

const (
	SkipLocal SkipMode = 1 << iota
	SkipRemote
	// SkipCache skip cache load,means load from source
	SkipCache = SkipLocal | SkipRemote
)

func (SkipMode) Any added in v0.3.0

func (f SkipMode) Any() bool

Any returns true if the skip annotation was set.

func (SkipMode) Is added in v0.3.0

func (f SkipMode) Is(mode SkipMode) bool

Is checks if the skip annotation has a specific flag.

type Stats added in v0.3.0

type Stats struct {
	Hits   uint64
	Misses uint64
}

Stats is the redis cache analyzer.

func (*Stats) AddHit added in v0.3.0

func (s *Stats) AddHit()

func (*Stats) AddMiss added in v0.3.0

func (s *Stats) AddMiss()

type UnmarshalFunc added in v0.3.0

type UnmarshalFunc func([]byte, any) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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