cache

package
v0.0.0-...-139d814 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2024 License: AGPL-3.0 Imports: 31 Imported by: 2

Documentation

Index

Constants

View Source
const (
	CacheDisabled int64 = -1 // 禁用缓存
	CacheFresh    int64 = -2 // 强制缓存新数据
)

Variables

View Source
var (
	TTL               = x.TTL
	Query             = x.Query
	DisableCacheUsage = x.DisableCacheUsage
	UseFreshData      = x.UseFreshData
	Disabled          = DisableCacheUsage(true) // 禁用缓存
	Fresh             = UseFreshData(true)      // 强制缓存新数据
	Noop              = func(o *x.Options) {}
)
View Source
var DBCacher = NewDBCacher()
View Source
var ErrFailedToAcquireLock = errors.New("failed to acquire lock")

Functions

func AddOptionParser

func AddOptionParser(adapter string, parser func(cache.Options) (cache.Options, error))

func AddTTL

func AddTTL(a x.GetOption, ttl int64) x.GetOption

func AdminRefreshable

func AdminRefreshable(ctx echo.Context, customer *dbschema.OfficialCustomer, ttl x.GetOption) x.GetOption

func Cache

func Cache(ctx context.Context, args ...string) cache.Cache

func CacheNew

func CacheNew(ctx context.Context, opts cache.Options, keys ...string) error

func CloseCache

func CloseCache()

func Decr

func Decr(ctx context.Context, key string, args ...string) error

Decr 自减

func Delete

func Delete(ctx context.Context, key string, args ...string) error

Delete 删除缓存

func Flush

func Flush(ctx context.Context, args ...string) error

Flush 删除所有缓存数据

func ForgetLock

func ForgetLock(key string, types ...LockType)

func GenOptions

func GenOptions(ctx echo.Context, cacheSeconds int64) []x.GetOption

func Get

func Get(ctx context.Context, key string, recv interface{}, args ...string) error

Get 获取缓存

func GetTTLByNumber

func GetTTLByNumber(ttl int64, b x.GetOption) x.GetOption

func Incr

func Incr(ctx context.Context, key string, args ...string) error

Incr 自增

func IsDbAccount

func IsDbAccount(v string) bool

func IsExist

func IsExist(ctx context.Context, key string, args ...string) (bool, error)

IsExist 是否存在缓存

func IsNotExist

func IsNotExist(err error) bool

func NewDBCacher

func NewDBCacher() *dbCacher

func Put

func Put(ctx context.Context, key string, val interface{}, timeout int64, args ...string) error

Put 设置缓存

func RedisClient

func RedisClient() *redis.Client

func RedisMutex

func RedisMutex(key string, options ...redsync.Option) *redsync.Mutex

RedisMutex 分布式锁 example: mutex := RedisMutex(`goods_1`) err = mutex.Lock(ctx)

if err != nil {
	panic(err)
}

mutex.Unlock(ctx)

func RedisOptions

func RedisOptions() *redis.Options

func RedsyncClient

func RedsyncClient() *redsync.Redsync

func RegisterTryLocker

func RegisterTryLocker(t LockType, fn TryLocker)

func SetDefaultLockType

func SetDefaultLockType(lockType LockType)

func SingleflightDo

func SingleflightDo(key string, fn func() (interface{}, error)) (v interface{}, err error, shared bool)

func SingleflightDoChan

func SingleflightDoChan(key string, fn func() (interface{}, error)) <-chan SinglefightResult

func SingleflightForget

func SingleflightForget(key string)

func TTLIf

func TTLIf(condition bool, a x.GetOption, b x.GetOption) x.GetOption

func TTLIfCallback

func TTLIfCallback(condition func() bool, a x.GetOption, b x.GetOption) x.GetOption

func XFunc

func XFunc(ctx context.Context, key string, recv interface{}, fn func() error, options ...x.GetOption) error

XFunc 获取缓存,如果不存在则执行函数获取数据并缓存【自动避免缓存穿透】

func XQuery

func XQuery(ctx context.Context, key string, recv interface{}, query x.Querier, options ...x.GetOption) error

XQuery 获取缓存,如果不存在则执行函数获取数据并缓存【自动避免缓存穿透】

Types

type LockType

type LockType int32
const (
	LockTypeMemory LockType = iota
	LockTypeRedis
)

func DefaultLockType

func DefaultLockType() LockType

type QueryFunc

type QueryFunc = x.QueryFunc

type ReloadableOptions

type ReloadableOptions struct {
	*cache.Options
}

func NewReloadableOptions

func NewReloadableOptions() *ReloadableOptions

func (*ReloadableOptions) IsValid

func (o *ReloadableOptions) IsValid() bool

func (*ReloadableOptions) Reload

func (o *ReloadableOptions) Reload() error

type SinglefightResult

type SinglefightResult = singleflight.Result

type Singleflighter

type Singleflighter interface {
	Do(key string, fn func() (interface{}, error)) (v interface{}, err error, shared bool)
	DoChan(key string, fn func() (interface{}, error)) <-chan SinglefightResult
	Forget(key string)
}

func NewSingleflight

func NewSingleflight(mu TryLocker) Singleflighter

func Singleflight

func Singleflight(types ...LockType) Singleflighter

type TryLocker

type TryLocker interface {
	Lock(key string) (unlock UnlockFunc, err error)
	TryLock(key string) (unlock UnlockFunc, err error)
	TryLockWithTimeout(key string, timeout time.Duration) (unlock UnlockFunc, err error)
	TryLockWithContext(key string, ctx context.Context) (unlock UnlockFunc, err error)
	Forget(key string)
}

func GetLocker

func GetLocker(types ...LockType) TryLocker

type UnlockFunc

type UnlockFunc func() error

func Lock

func Lock(key string, types ...LockType) (unlock UnlockFunc, err error)

func TryLock

func TryLock(key string, types ...LockType) (unlock UnlockFunc, err error)

func TryLockWithContext

func TryLockWithContext(key string, ctx context.Context, types ...LockType) (unlock UnlockFunc, err error)

func TryLockWithTimeout

func TryLockWithTimeout(key string, timeout time.Duration, types ...LockType) (unlock UnlockFunc, err error)

Jump to

Keyboard shortcuts

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