vcache

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DataEmpty = ""
)

Variables

This section is empty.

Functions

func CheckDataEmpty

func CheckDataEmpty(data string) bool

func Get

func Get(ctx context.Context, key interface{}) (*vvar.Var, error)

Get retrieves and returns the associated value of given `key`. It returns nil if it does not exist, or its value is nil, or it's expired. If you would like to check if the `key` exists in the cache, it's better using function Contains.

func GetOrSet

func GetOrSet(ctx context.Context, key interface{}, value interface{}, duration time.Duration) (*vvar.Var, error)

GetOrSet retrieves and returns the value of `key`, or sets `key`-`value` pair and returns `value` if `key` does not exist in the cache. The key-value pair expires after `duration`.

It does not expire if `duration` == 0. It deletes the `key` if `duration` < 0 or given `value` is nil, but it does nothing if `value` is a function and the function result is nil.

func Set

func Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error

Set sets cache with `key`-`value` pair, which is expired after `duration`.

It does not expire if `duration` == 0. It deletes the keys of `data` if `duration` < 0 or given `value` is nil.

func Update

func Update(ctx context.Context, key interface{}, value interface{}) (oldValue *vvar.Var, exist bool, err error)

Update updates the value of `key` without changing its expiration and returns the old value. The returned value `exist` is false if the `key` does not exist in the cache.

It deletes the `key` if given `value` is nil. It does nothing if `key` does not exist in the cache.

Types

type ILocalCache

type ILocalCache interface {
	Get(ctx context.Context, key interface{}) (*vvar.Var, error)
	Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error
}

func NewLocalCache

func NewLocalCache() ILocalCache

type IRedisCache

type IRedisCache interface {
	Del(ctx context.Context, keys ...string) (int64, error)
	ExpireNX(ctx context.Context, key string, duration time.Duration) (bool, error)
	Get(ctx context.Context, key string) (string, error)
	HDel(ctx context.Context, key string, fields ...string) (int64, error)
	HGet(ctx context.Context, key string, field string) (string, error)
	HGetAll(ctx context.Context, key string) (map[string]string, error)
	HSet(ctx context.Context, key string, values ...interface{}) (int64, error)
	Lock(ctx context.Context, lockName string, duration time.Duration) (res bool, err error)
	SAdd(ctx context.Context, key string, members ...interface{}) (int64, error)
	SPop(ctx context.Context, key string) (string, error)
	Set(ctx context.Context, key string, value interface{}, duration time.Duration) (string, error)
	SetNX(ctx context.Context, key string, value interface{}, duration time.Duration) (bool, error)
	Unlock(ctx context.Context, lockName string) (int64, error)
}

func NewRedisCache

func NewRedisCache(p RedisCacheParam) IRedisCache

type LocalCache

type LocalCache struct {
}

func (*LocalCache) Get

func (r *LocalCache) Get(ctx context.Context, key interface{}) (*vvar.Var, error)

func (*LocalCache) Set

func (r *LocalCache) Set(ctx context.Context, key interface{}, value interface{}, duration time.Duration) error

type RedisCache

type RedisCache struct {
	Redis *vredis.Redis
}

func (*RedisCache) Del

func (r *RedisCache) Del(ctx context.Context, keys ...string) (int64, error)

func (*RedisCache) ExpireNX

func (r *RedisCache) ExpireNX(ctx context.Context, key string, duration time.Duration) (bool, error)

func (*RedisCache) Get

func (r *RedisCache) Get(ctx context.Context, key string) (string, error)

func (*RedisCache) HDel

func (r *RedisCache) HDel(ctx context.Context, key string, fields ...string) (int64, error)

func (*RedisCache) HGet

func (r *RedisCache) HGet(ctx context.Context, key string, field string) (string, error)

func (*RedisCache) HGetAll

func (r *RedisCache) HGetAll(ctx context.Context, key string) (map[string]string, error)

func (*RedisCache) HSet

func (r *RedisCache) HSet(ctx context.Context, key string, values ...interface{}) (int64, error)

func (*RedisCache) Lock

func (r *RedisCache) Lock(ctx context.Context, lockName string, duration time.Duration) (res bool, err error)

func (*RedisCache) SAdd

func (r *RedisCache) SAdd(ctx context.Context, key string, members ...interface{}) (int64, error)

func (*RedisCache) SPop

func (r *RedisCache) SPop(ctx context.Context, key string) (string, error)

func (*RedisCache) Set

func (r *RedisCache) Set(ctx context.Context, key string, value interface{}, duration time.Duration) (string, error)

func (*RedisCache) SetNX

func (r *RedisCache) SetNX(ctx context.Context, key string, value interface{}, duration time.Duration) (bool, error)

func (*RedisCache) Unlock

func (r *RedisCache) Unlock(ctx context.Context, lockName string) (int64, error)

type RedisCacheParam

type RedisCacheParam struct {
	vfx.In

	Redis *vredis.Redis
}

Jump to

Keyboard shortcuts

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