Categorygithub.com/zhanglp0129/redis_cache
modulepackage
1.0.6
Repository: https://github.com/zhanglp0129/redis_cache.git
Documentation: pkg.go.dev

# 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数据结构,使用不同的方式修改缓存
  • 能主动刷新缓存时间

# Functions

CacheIncrBy 如果只缓存了一个整数,可以修改缓存,让这个整数增加value.
CacheIncrByCtx 如果只缓存了一个整数,可以修改缓存,让这个整数增加value.
CacheIncrByToPipe 将缓存的整数增加value。会把操作加入pipe,不会执行。一次只能添加一个.
CacheIncrByToPipeCtx 将缓存的整数增加value。会把操作加入pipe,不会执行。一次只能添加一个.
DeleteCache 删除缓存.
DeleteCacheCtx 删除缓存.
DeleteCacheToPipe 将删除缓存的操作加入pipe,不会执行.
DeleteCacheToPipeCtx 将删除缓存的操作加入pipe,不会执行.
FlushCacheTime 在命中缓存时刷新缓存时间,默认为false.
QueryWithCache 带着缓存查询 model,传出参数,数据模型 query,查询函数,缓存未命中时调用 return,是否命中缓存.
WithCacheTime 指定缓存时间,默认为 30 min.
WithCacheTimeDiff 指定缓存时间上下偏差,默认为 5 min.
WithContext 指定使用redis时的context,默认为background.
WriteCache 是否在缓存未命中时,写入缓存,默认为true.

# Variables

DefaultCacheTime 默认缓存时间,30min.
DefaultCacheTimeDiff 默认缓存时间的上下偏差,5min.

# Structs

No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author