redis_cache

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2024 License: MIT Imports: 7 Imported by: 0

README

RedisCache

Golang使用redis缓存,将数据转换为json,并存入redis

使用

  1. 安装依赖
go get -u github.com/zhanglp0129/redis_cache
  1. 带着缓存查询
// 创建redis客户端连接
rdb := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
})
// redis key
key := "test_key"
// model可以是任意类型
model := ""

// 带着缓存查询,需要传入查询函数,在缓存未命中时调用
hit, err := QueryWithCache(rdb, key, &model, func() (*string, error) {
    data := "test_data"
    return &data, nil
})

// 打印结果
fmt.Println("是否命中缓存:", hit)
fmt.Println("结果:", model)
  1. 删除缓存
rdb := redis.NewClient(&redis.Options{
    Addr: "127.0.0.1:6379",
})
key := "test_key"

err := DeleteCache(rdb, key)

TODO

  • 文档和注释改为英文,同时优化文档
  • 带着缓存查询时,能够加锁,防止缓存多次写入
  • 添加布隆过滤器的实现
  • 能针对不同的数据类型,使用不同的数据结构缓存
  • 针对不同的redis数据结构,使用不同的方式修改缓存
  • 能主动刷新缓存时间

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultCacheTime 默认缓存时间,30min
	DefaultCacheTime = 30 * time.Minute
	// DefaultCacheTimeDiff 默认缓存时间的上下偏差,5min
	DefaultCacheTimeDiff = 5 * time.Minute
)

Functions

func CacheIncrBy added in v1.0.4

func CacheIncrBy(rdb redis.UniversalClient, key string, value int64) error

CacheIncrBy 如果只缓存了一个整数,可以修改缓存,让这个整数增加value

func CacheIncrByCtx added in v1.0.6

func CacheIncrByCtx(ctx context.Context, rdb redis.UniversalClient, key string, value int64) error

CacheIncrByCtx 如果只缓存了一个整数,可以修改缓存,让这个整数增加value

func CacheIncrByToPipe added in v1.0.4

func CacheIncrByToPipe(pipe redis.Pipeliner, key string, value int64)

CacheIncrByToPipe 将缓存的整数增加value。会把操作加入pipe,不会执行。一次只能添加一个

func CacheIncrByToPipeCtx added in v1.0.6

func CacheIncrByToPipeCtx(ctx context.Context, pipe redis.Pipeliner, key string, value int64)

CacheIncrByToPipeCtx 将缓存的整数增加value。会把操作加入pipe,不会执行。一次只能添加一个

func DeleteCache

func DeleteCache(rdb redis.UniversalClient, key string) error

DeleteCache 删除缓存

func DeleteCacheCtx added in v1.0.6

func DeleteCacheCtx(ctx context.Context, rdb redis.UniversalClient, key string) error

DeleteCacheCtx 删除缓存

func DeleteCacheToPipe added in v1.0.2

func DeleteCacheToPipe(pipe redis.Pipeliner, keys ...string)

DeleteCacheToPipe 将删除缓存的操作加入pipe,不会执行

func DeleteCacheToPipeCtx added in v1.0.6

func DeleteCacheToPipeCtx(ctx context.Context, pipe redis.Pipeliner, keys ...string)

DeleteCacheToPipeCtx 将删除缓存的操作加入pipe,不会执行

func QueryWithCache

func QueryWithCache[T any](rdb redis.UniversalClient, key string, model *T, query QueryFunc[*T], options ...Option) (bool, error)

QueryWithCache 带着缓存查询 model,传出参数,数据模型 query,查询函数,缓存未命中时调用 return,是否命中缓存

Types

type CacheConfig

type CacheConfig struct {
	// contains filtered or unexported fields
}

type Option

type Option func(c *CacheConfig)

func FlushCacheTime added in v1.0.6

func FlushCacheTime(flush bool) Option

FlushCacheTime 在命中缓存时刷新缓存时间,默认为false

func WithCacheTime

func WithCacheTime(cacheTime time.Duration) Option

WithCacheTime 指定缓存时间,默认为 30 min

func WithCacheTimeDiff added in v1.0.6

func WithCacheTimeDiff(diff time.Duration) Option

WithCacheTimeDiff 指定缓存时间上下偏差,默认为 5 min

func WithContext

func WithContext(ctx context.Context) Option

WithContext 指定使用redis时的context,默认为background

func WriteCache added in v1.0.3

func WriteCache(write bool) Option

WriteCache 是否在缓存未命中时,写入缓存,默认为true

type QueryFunc

type QueryFunc[T any] func() (T, error)

Jump to

Keyboard shortcuts

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