cache

package
v0.0.0-...-d5d5ef8 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCleanTask

func AddCleanTask(task func() error, keys ...string)

AddCleanTask adds a clean task on given keys.

func AddCleanTaskx

func AddCleanTaskx(task func() error, key string, fields ...string)

AddCleanTaskx adds a clean task on given key and fields.

func TotalWeights

func TotalWeights(c []NodeConf) int

TotalWeights returns the total weights of given nodes.

Types

type Cache

type Cache interface {
	Exists(key string) (bool, error)
	ExistsCtx(ctx context.Context, key string) (bool, error)
	Hexists(key string, field string) (bool, error)
	HexistsCtx(ctx context.Context, key string, field string) (bool, error)
	// Del deletes cached values with keys.
	Del(keys ...string) error
	// DelCtx deletes cached values with keys.
	DelCtx(ctx context.Context, keys ...string) error
	// Delx deletes cached values with key and fields.
	Delx(key string, fields ...string) error
	// DelxCtx deletes cached values with key and fields.
	DelxCtx(ctx context.Context, key string, fields ...string) error
	// Get gets the cache with key and fills into v.
	Get(key string, val any) error
	// GetCtx gets the cache with key and fills into v.
	GetCtx(ctx context.Context, key string, val any) error
	// IsNotFound checks if the given error is the defined errNotFound.
	IsNotFound(err error) bool
	// Set sets the cache with key and v, using c.expiry.
	Set(key string, val any) error
	// SetCtx sets the cache with key and v, using c.expiry.
	SetCtx(ctx context.Context, key string, val any) error
	// Set sets the cache with key and v, using c.expiry.
	HSet(key string, fields string, val any) error
	// SetCtx sets the cache with key and v, using c.expiry.
	HsetCtx(ctx context.Context, key string, fields string, val any) error
	// Get gets the cache with key and fills into v.
	HGet(key string, fields string, val any) error
	// GetCtx gets the cache with key and fills into v.
	HGetCtx(ctx context.Context, key string, fields string, val any) error
	// SetWithExpire sets the cache with key and v, using given expire.
	SetWithExpire(key string, val any, expire time.Duration) error
	// SetWithExpireCtx sets the cache with key and v, using given expire.
	SetWithExpireCtx(ctx context.Context, key string, val any, expire time.Duration) error
	// Take takes the result from cache first, if not found,
	// query from DB and set cache using c.expiry, then return the result.
	Take(val any, key string, query func(val any) error) error
	// Takex takes the result from cache first, key and field, if not found,
	// query from DB and set cache using c.expiry, then return the result.
	Takex(val any, key, field string, query func(val any) error) error
	// TakeCtx takes the result from cache first, if not found,
	// query from DB and set cache using c.expiry, then return the result.
	TakeCtx(ctx context.Context, val any, key string, query func(val any) error) error
	// TakexCtx takes the result from cache first, key and field, if not found,
	// query from DB and set cache using c.expiry, then return the result.
	TakexCtx(ctx context.Context, val any, key, field string, query func(val any) error) error
	// TakeWithExpire takes the result from cache first, if not found,
	// query from DB and set cache using given expire, then return the result.
	TakeWithExpire(val any, key string, query func(val any, expire time.Duration) error) error
	// TakeWithExpireCtx takes the result from cache first, if not found,
	// query from DB and set cache using given expire, then return the result.
	TakeWithExpireCtx(ctx context.Context, val any, key string,
		query func(val any, expire time.Duration) error) error
	// TakeWithExpire2 takes the result from cache first, if not found,
	// query from DB and set cache using given expire, then return the result.
	TakeWithExpire2(val any, key string, field string, query func(val any) error) error
	// TakeWithExpire2Ctx takes the result from cache first, if not found,
	// query from DB and set cache using given expire, then return the result.
	TakeWithExpire2Ctx(ctx context.Context, val any, key string, field string, query func(val any) error) error
	// TakeAllOne takes the result from cache first, key and fields, if not found,
	// query from DB and set cache using c.expiry, then return the result.
	TakeAllOne(val any, key string, query func(val any, leftFields ...string) error) error

	// TakeAllOneCtx takes the result from cache first by key and struct tag fields, if not found,
	// query from DB and set cache using c.expiry, then return the result.
	TakeAllOneCtx(ctx context.Context, val any, key string,
		query func(val any, leftFields ...string) error) error

	// Expire is the implementation of redis expire command.
	Expire(key string, expire time.Duration) error
	// ExpireCtx is the implementation of redis expire command.
	ExpireCtx(ctx context.Context, key string, expire time.Duration) error
}

Cache interface is used to define the cache implementation.

func New

func New(c ClusterConf, barrier syncx.SingleFlight, st *Stat, errNotFound error,
	opts ...Option) Cache

New returns a Cache.

func NewNode

func NewNode(rds *redis.Redis, barrier syncx.SingleFlight, st *Stat,
	errNotFound error, opts ...Option) Cache

NewNode returns a cacheNode. rds is the underlying redis node or cluster. barrier is the barrier that maybe shared with other cache nodes on cache cluster. st is used to stat the cache. errNotFound defines the error that returned on cache not found. opts are the options that customize the cacheNode.

type CacheConf

type CacheConf = ClusterConf

CacheConf is an alias of ClusterConf.

type ClusterConf

type ClusterConf []NodeConf

A ClusterConf is the config of a redis cluster that used as cache.

type NodeConf

type NodeConf struct {
	redis.RedisConf
	Weight int `json:",default=100"`
}

A NodeConf is the config of a redis node that used as cache.

type Option

type Option func(o *Options)

Option defines the method to customize an Options.

func WithExpiry

func WithExpiry(expiry time.Duration) Option

WithExpiry returns a func to customize an Options with given expiry.

func WithNotFoundExpiry

func WithNotFoundExpiry(expiry time.Duration) Option

WithNotFoundExpiry returns a func to customize an Options with given not found expiry.

type Options

type Options struct {
	Expiry         time.Duration
	NotFoundExpiry time.Duration
}

An Options is used to store the cache options.

type Stat

type Stat struct {

	// export the fields to let the unit tests working,
	// reside in internal package, doesn't matter.
	Total   uint64
	Hit     uint64
	Miss    uint64
	DbFails uint64
	// contains filtered or unexported fields
}

A Stat is used to stat the cache.

func NewStat

func NewStat(name string) *Stat

NewStat returns a Stat.

func (*Stat) IncrementDbFails

func (s *Stat) IncrementDbFails()

IncrementDbFails increments the db fail count.

func (*Stat) IncrementHit

func (s *Stat) IncrementHit()

IncrementHit increments the hit count.

func (*Stat) IncrementMiss

func (s *Stat) IncrementMiss()

IncrementMiss increments the miss count.

func (*Stat) IncrementTotal

func (s *Stat) IncrementTotal()

IncrementTotal increments the total count.

Jump to

Keyboard shortcuts

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