cache

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

README

Gousing Cache

介绍

开箱即用的 Cache 工具,支持 Memory 和 Redis,统一的 API 快速设置及读取,支持 TAG 标签,支持全局默认过期时间,单 KEY 过期时间,提供 PluginDo 初始化回调插件(singleflight 防击穿) 和 PluginSet 泛型化集合插件……

安装及使用
go get gitee.com/gousing/cache
import  "gitee.com/gousing/cache"
全局默认缓存实例
	// 全局默认缓存实例,使用默认Store, 默认为 StoreMemory
	// Set使用全局默认过期时间
	cache.Set("key1", "StoreMemory")
	// SetExpire针对单个Key设置过期时间
	cache.SetExpire("key2", "valueExpire", time.Second)

	// 使用Memory实例
	cache.Store(cache.StoreMemory).Set("key1", "StoreMemory")
	// 或
	cache.Memory().Set("key1", "StoreMemory")
	// 使用Redis实例
	// 为了测试,清空Redis缓存
	cache.Redis().Clear()
	cache.Store(cache.StoreRedis).Set("key1", "StoreRedis")
	// 或
	cache.Redis().Set("key1", "StoreRedis")

	fmt.Println(`cache.Memory().Size()`, cache.Memory().Size()) // 2
	fmt.Println(`cache.Redis().Size()`, cache.Redis().Size())   // 1

	// 缓存读取
	fmt.Println(`cache.GetString("key1")`, cache.GetString("key1"))                                                   // "StoreMemory"
	fmt.Println(`cache.Store(cache.StoreMemory).GetString("key1")`, cache.Store(cache.StoreMemory).GetString("key1")) // "StoreMemory"
	fmt.Println(`cache.Store(cache.StoreRedis).GetString("key1")`, cache.Store(cache.StoreRedis).GetString("key1"))   // "StoreRedis"

	// TAG标签
	cache.Tags("tag1").Set("keyTag-1-1", 11)
	cache.Tags("tag1").Set("keyTag-1-2", 12)
	cache.Tags("tag2").Set("keyTag-2", 20)

	fmt.Println(`cache.Tags("tag1").Size()`, cache.Tags("tag1").Size()) // 2
	fmt.Println(`cache.Tags("tag2").Size()`, cache.Tags("tag2").Size()) // 1

	cache.Tags("tag1").Clear()                                          // Del tag1 all keys: keyTag-1-1,keyTag-1-2
	fmt.Println(`cache.Tags("tag1").Size()`, cache.Tags("tag1").Size()) // 0

	cache.Tags("tag2").Clear()                                          // Del tag2 all keys: keyTag-2
	fmt.Println(`cache.Tags("tag2").Size()`, cache.Tags("tag2").Size()) // 0


	// 自定义全局默认缓存仓库实例
	// 注意:请在项目初始化时进行自定义,仅需自定义一次,后续整个项目直接使用即可
	// SetDefaultStoreType 设置默认缓存仓库的驱动类型 StoreType
	cache.SetDefaultStoreType(cache.StoreType)
	// GetDefaultStoreType 获取默认缓存仓库的驱动类型 StoreType
	cache.GetDefaultStoreType() cache.StoreType 
	// GetDefault 获取默认缓存实例 StoreIO
	//   - 如果默认实例未配置(未初始化),将自动尝试使用默认参数进行初始化
	//   - 用户可通过 `SetDefaultStoreType` 设置默认缓存实例的 StoreType 驱动类型(默认为 StoreMemory)
	//   - 用户可通过 `SetDefaultMemory` 设置自定义默认 StoreMemory 缓存实例
	//   - 用户可通过 `SetDefaultRedis` 设置自定义默认 StoreRedis 缓存实例
	cache.GetDefault() CacheIO
	// GetDefaultMemory 获取全局默认 StoreMemory 缓存实例
	//   - 用户可通过 `SetDefaultMemory` 设置自定义默认 StoreMemory 缓存实例
	//   - 如果默认实例未配置(未初始化),会尝试使用默认参数自动延迟初始化
	cache.GetDefaultMemory() CacheIO 
	// SetDefaultMemory 设置全局默认StoreMemory缓存实例
	cache.SetDefaultMemory(io CacheIO) 
	// GetDefaultRedis 全局StoreRedis缓存
	//   - 用户可通过 `SetDefaultRedis` 设置自定义默认 StoreRedis 缓存实例
	//   - 如果默认实例未配置(未初始化),会尝试使用默认参数自动延迟初始化
	cache.GetDefaultRedis() CacheIO
	// SetDefaultRedis 设置全局默认StoreRedis缓存实例
	cache.SetDefaultRedis(io CacheIO)

	// PluginDo 插件
	//   Key 缓存键名
	//   Do 缓存创建回调, 不存在或过期后回调创建缓存
	//   Read 读取缓存, 返回缓存值及错误
	//   Expire 可选, 过期时间 ( 默认为0, 表示使用缓存Store默认过期时间[无过期时间] )
	//   ForgetTime 可选, 缓存防击穿选项, 每个PluginDo实例可以自定义, 默认为 0,即singleflight默认方式。
	// 	 - singleflight 默认只能尝试一次,阻塞请求,直到执行完毕。
	//   - 如设置为 100ms, 表示100ms内的请求最多发起1次请求,每秒最大并发:10 RPS
	//   - 如设置为 200ms,表示200ms内的请求最多发起1次请求,每秒最大并发:5 RPS
	//   - 如设置为 500ms,表示500ms内的请求最多发起1次请求,每秒最大并发:2 RPS
	//   - 如设置为 1000ms,表示1000ms内的请求最多发起1次请求,每秒最大并发:1 RPS
		valRuning := 0
		doRuning := cache.PluginDo[int]().
			Key("usePluginDo").
			Do(func() (int, error) {
				valRuning++
				return valRuning, nil
			}).
			Expire(time.Second * 2)
		// 注意:IO将最小过期时间统一为 1s => time.Second
		// 注意:由于StoreMemory(FreeCache) 的过期时间是一个近似值,实际过期时间会在 (X-1, X] 秒之间,其中 X 是你设置的过期时间。
		// 我们测试的过期时间是 2s,所以实际过期时间会在 [1, 2] 秒之间,所以这里测试时,我们最多只能测试到 <2s 的过期时间。)
		var wg sync.WaitGroup
		wg.Add(11)
		// 未过期测试
		for range 10 {
			go func() {
				cerRuning, _:= doRuning.Read()
				fmt.Println(`未过期 cerRuning`,cerRuning)//1
				wg.Done()
			}()
		}
		// 已过期测试
		go func() {
			time.Sleep(time.Second*2 + time.Millisecond*100)
			cerRuning, _ := doRuning.Read()
			fmt.Println(`已过期 cerRuning`,cerRuning)//2
			wg.Done()
		}()
		wg.Wait()

	// 更多接口参见 IO Interface:
	// CacheIO interface
	// TagsIO interface

自定义实例
cache.NewMemory(options cache.MemoryOptions) (CacheIO,error)
memCache,err :=cache.NewMemory(cache.MemoryOptions{
	// CacheSize MemoryCache 占用内存大小, 单位MB,  默认 100M,最小10M
	//  - MemoryCache底层由freecache驱动, 每个freecache有256个segment,因此每个缓存[entry]大小不能超过 freecache内存大小的1/1024
	CacheSize: 256,
	// DefaultExpire 默认过期时间(配置单位为[秒]) 0 表示不过期(实例生命周期内/长久存储)
	DefaultExpire:0,
})
cache.NewRedis(options cache.RedisOptions) (CacheIO,error)
// RedisOptions Redis缓存配置信息
type RedisOptions struct {
	// Client Redis服务器链接选项,使用 UniversalClient 统一客户端,UniversalClient是对 Client 、 ClusterClient 、 FailoverClient 客户端的包装。
	// 	- 根据不同的选项,客户端的类型如下:
	// 	- 如果指定了 MasterName 选项,则返回 FailoverClient 哨兵客户端。
	// 	- 如果 Addrs 是 2 个以上的地址,则返回 ClusterClient 集群客户端。
	// 	- 其他情况,返回 Client 单节点客户端。
	//  - github.com/redis/go-redis/v9
	// 	- https://redis.uptrace.dev/zh/guide/universal.html
	// 	- RedisUniversalOptions is an alias for [redis.UniversalOptions]
	Client RedisUniversalOptions
	// DefaultExpire 默认过期时间(配置单位为[秒]) 0 表示不过期(实例生命周期内/长久存储) 注意:最小过期时间为1s time.Second
	// FreeCache 的过期时间是一个近似值,实际过期时间会在 (X-1, X] 秒之间,其中 X 是你设置的过期时间。
	DefaultExpire int
}
redisCache,err :=cache.NewRedis(cache.RedisOptions{
		// 单个主机或集群配置
		// 例如:[]string{"192.168.1.10:6379"}
		Client: cache.RedisUniversalOptions{Addrs: []string{"localhost:6379"}},
		DefaultExpire:0,
},0),

// 注意:IO将最小过期时间统一为 1s => time.Second
// 注意:由于StoreMemory(FreeCache) 的过期时间是一个近似值,实际过期时间会在 (X-1, X] 秒之间,其中 X 是你设置的过期时间。
// 我们测试的过期时间是 2s,所以实际过期时间会在 (1, 2] 秒之间,所以这里测试时,我们最多只能测试到 1s 的过期时间。)
memCache.Set("key1", "value1")
memCache.SetExpire("key2", "value2",time.Second)
memCache.GetString("key1") // "value1"
memCache.GetString("key2") // "value2"
time.Sleep(time.Second*2)
memCache.GetString("key1") // "value1"
memCache.GetString("key2") // ""
CacheIO 接口
type CacheIO interface {
	//Has 判断缓存是否存在
	Has(key string) bool
	//TTL 获取缓存过期时间 (返回0 表示不过期(实例生命周期内/长久存储) 注意:最小过期时间为1s time.Second)
	TTL(key string) (time.Duration, error)
	//Get 读取缓存
	Get(key string) (val []byte, err error)
	GetString(key string) string
	GetStringD(key string, defaultVal string) string
	GetInt(key string) int
	GetIntD(key string, defaultVal int) int
	GetInt8(key string) int8
	GetInt8D(key string, defaultVal int8) int8
	GetInt16(key string) int16
	GetInt16D(key string, defaultVal int16) int16
	GetInt32(key string) int32
	GetInt32D(key string, defaultVal int32) int32
	GetInt64(key string) int64
	GetInt64D(key string, defaultVal int64) int64
	GetUint(key string) uint
	GetUintD(key string, defaultVal uint) uint
	GetUint8(key string) uint8
	GetUint8D(key string, defaultVal uint8) uint8
	GetUint16(key string) uint16
	GetUint16D(key string, defaultVal uint16) uint16
	GetUint32(key string) uint32
	GetUint32D(key string, defaultVal uint32) uint32
	GetUint64(key string) uint64
	GetUint64D(key string, defaultVal uint64) uint64
	GetFloat32(key string) float32
	GetFloat32D(key string, defaultVal float32) float32
	GetFloat64(key string) float64
	GetFloat64D(key string, defaultVal float64) float64
	GetBool(key string) bool
	GetBoolD(key string, defaultVal bool) bool
	GetTime(key string) time.Time
	GetTimeD(key string, defaultVal time.Time) time.Time
	GetDuration(key string) time.Duration
	GetDurationD(key string, defaultVal time.Duration) time.Duration
	GetTimeMonth(key string) time.Month
	GetTimeMonthD(key string, defaultVal time.Month) time.Month
	GetTimeWeekday(key string) time.Weekday
	GetTimeWeekdayD(key string, defaultVal time.Weekday) time.Weekday
	GetIP(key string) net.IP
	GetIPD(key string, defaultVal net.IP) net.IP
	// Scan 方式获取缓存对象,适用于 Slice/Map/Struct 缓存对象, 反序列化扫描后放入引用地址变量
	Scan(key string, refVal any) error
	// Set 设置缓存(默认过期时间)
	Set(key string, val interface{}) error
	// SetExpire 设置缓存(标记过期时间,为0表示永不过期) , 注意:最小过期时间为 1s => time.Second
	SetExpire(key string, val interface{}, expire time.Duration) error
	// SetNotExist 设置缓存(标记过期时间,为0表示永不过期),不存在则设置
	SetNotExist(key string, val interface{}, expire time.Duration) error
	// ChangeInt 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减) 标记过期时间,为0表示永不过期
	//   - expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间
	ChangeInt(key string, step int, expire time.Duration) (newVal int, err error)
	// ChangeInt32 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减) 标记过期时间,为0表示永不过期
	//   - expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间
	ChangeInt32(key string, step int32, expire time.Duration) (newVal int32, err error)
	// ChangeInt64 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减) 标记过期时间,为0表示永不过期
	//   - expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间
	ChangeInt64(key string, step int64, expire time.Duration) (newVal int64, err error)
	// ChangeFloat32 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减) 标记过期时间,为0表示永不过期
	//   - expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间
	ChangeFloat32(key string, step float32, expire time.Duration) (newVal float32, err error)
	// ChangeFloat64 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减) 标记过期时间,为0表示永不过期
	//   - expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间
	ChangeFloat64(key string, step float64, expire time.Duration) (newVal float64, err error)

	//Del 删除缓存
	Del(keys ...string) error
	//Clear 清空全部缓存
	Clear() error
	//Size 获取已缓存数据的条目
	Size() int64
	//StoreType 获取缓存库的类型
	StoreType() StoreType
	//Tags 指定缓存标签
	Tags(tags ...string) TagsIO
	//Stats 获取性能指标统计信息
	Stats() StoreStats
}

type TagsIO interface {
	// Set 指定Tags下设置缓存(默认过期时间)
	Set(key string, val interface{}) error
	// SetExpire 指定Tags下设置缓存(标记过期时间,为0表示永不过期)
	SetExpire(key string, val interface{}, expire time.Duration) error
	// SetNotExist 指定Tags下设置缓存(标记过期时间,为0表示永不过期),不存在则设置
	SetNotExist(key string, val interface{}, expire time.Duration) error
	//Del 删除指定Tags下的缓存
	Del(keys ...string) error
	//Clear 清空指定Tags下的全部缓存
	Clear() error
	//Size 获取指定Tags下缓存数据的条目
	Size() int64
}

Gousing 通用选项
自定义 Logger
// Cache 日志默认使用Golang标准库slog.Default()全局日志记录日志
// cache.SetLogger(*slog.Logger)
myLogger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
cache.SetLogger(myLogger)
自定义 Jsoner
// Cache 序列化默认使用兼容标准库的json-iterator
// jsoniter.ConfigCompatibleWithStandardLibrary
type JsonerAPI interface {
	// Marshal adapts to json/encoding Marshal API
	// Refer to https://godoc.org/encoding/json#Marshal for more information
	Marshal(v any) ([]byte, error)
	// Unmarshal adapts to json/encoding Unmarshal API
	// Refer to https://godoc.org/encoding/json#Unmarshal for more information
	Unmarshal(data []byte, v any) error
	// Valid reports whether data is a valid JSON encoding
	Valid(v []byte) bool
}
// SetJsoner 自定义 Json 操作库,为nil则不设置
var myJsonerApi JsonerAPI
cache.SetJsoner(myJsonerApi)
// UseStandardLibrary 切换Json库为 Json StandardLibrary 标准库
cache.UseStandardLibrary() 
// UseJsonIterator 切换Json库为 Json Iterator 库
cache.UseJsonIterator() 
感谢
  1. Cache Memory 使用 freecache 驱动 github.com/coocood/freecache
  2. Cache Redis 使用 go-redis(v9) 驱动 github.com/redis/go-redis
Benchmark

统一便捷的 IO 接口封装后性能损失极低:

goos: windows
goarch: amd64
pkg: gitee.com/gousing/cache
cpu: 12th Gen Intel(R) Core(TM) i5-12400F
go.exe test -benchmem -benchtime=100000x -run=^$ -bench ^Benchmark gitee.com/gousing/cache
Benchmark Memory
BenchmarkMemory - - - -
BenchmarkMemory/Memory_SetGet(1:9)-12 100000 53.96 ns/op 1 B/op 1 allocs/op
BenchmarkMemory/Memory_SetGet(1:9)Expire-12 100000 60.62 ns/op 1 B/op 1 allocs/op
BenchmarkMemory/Memory_Tags_SetGet(1:9)-12 100000 80.09 ns/op 2 B/op 1 allocs/op
BenchmarkMemory/Memory_ChangeInc-12 100000 364.4 ns/op 31 B/op 3 allocs/op
BenchmarkMemory/Memory_ChangeDec-12 100000 244.1 ns/op 31 B/op 3 allocs/op
BenchmarkMemory/Memory_Scan_Struct-12 100000 222.1 ns/op 48 B/op 4 allocs/op
BenchmarkMemory/Memory_Scan_SilceInt-12 100000 198.6 ns/op 24 B/op 1 allocs/op
BenchmarkMemory/Memory_Scan_SilceAny-12 100000 396.0 ns/op 160 B/op 6 allocs/op
BenchmarkMemory/Memory_Scan_MapString-12 100000 362.6 ns/op 117 B/op 9 allocs/op
BenchmarkMemory/Memory_Scan_MapAny-12 100000 414.5 ns/op 128 B/op 9 allocs/op
BenchmarkMemory/Memory_Parallel-12 100000 165.7 ns/op 1 B/op 1 allocs/op
Benchmark Redis
BenchmarkRedis - - - -
BenchmarkRedis/Redis_SetGet(1:9)-12 100000 29120 ns/op 173 B/op 5 allocs/op
BenchmarkRedis/Redis_SetGet(1:9)Expire-12 100000 29250 ns/op 173 B/op 5 allocs/op
BenchmarkRedis/Redis_Tags_SetGet(1:9)-12 100000 35396 ns/op 219 B/op 6 allocs/op
BenchmarkRedis/Redis_ChangeInc-12 100000 59271 ns/op 344 B/op 8 allocs/op
BenchmarkRedis/Redis_ChangeDec-12 100000 59539 ns/op 344 B/op 8 allocs/op
BenchmarkRedis/Redis_Scan_Struct-12 100000 30282 ns/op 229 B/op 8 allocs/op
BenchmarkRedis/Redis_Scan_SilceInt-12 100000 30104 ns/op 188 B/op 5 allocs/op
BenchmarkRedis/Redis_Scan_SilceAny-12 100000 30339 ns/op 320 B/op 10 allocs/op
BenchmarkRedis/Redis_Scan_MapString-12 100000 30640 ns/op 296 B/op 13 allocs/op
BenchmarkRedis/Redis_Scan_MapAny-12 100000 30263 ns/op 309 B/op 13 allocs/op
BenchmarkRedis/Redis_Parallel-12 100000 14062 ns/op 218 B/op 5 allocs/op
Benchmark Plugin
BenchmarkPlugin - - - -
BenchmarkPluginDo/Memory_Do_Struct-12 100000 354.0 ns/op 176 B/op 7 allocs/op
BenchmarkPluginDo/Memory_Do_StructPtr-12 100000 411.6 ns/op 184 B/op 8 allocs/op
BenchmarkPluginDo/Redis_Do_Struct-12 100000 58663 ns/op 461 B/op 14 allocs/op
BenchmarkPluginDo/Redis_Do_StructPtr-12 100000 61128 ns/op 469 B/op 15 allocs/op
BenchmarkPluginSet/Memory:Push-12 100000 225.7 ns/op 45 B/op 3 allocs/op
BenchmarkPluginSet/Memory:Has-12 100000 24.89 ns/op 0 B/op 0 allocs/op
BenchmarkPluginSet/Redis:Push-12 100000 31322 ns/op 288 B/op 9 allocs/op
BenchmarkPluginSet/Redis:Has-12 100000 24.00 ns/op 0 B/op 0 allocs/op

Documentation

Index

Constants

View Source
const (
	MemoryCacheSizeMin     = 10 //单位 MB
	MemoryCacheSizeDefault = 100
)

Variables

View Source
var (
	ErrStoreInstanceIsNil = errors.New("cache: store instance is nil")
	ErrStoreTypeNotExist  = errors.New("cache: store type does not exist")
	ErrKeyCanNotEmpty     = errors.New("cache: key can not empty")
	ErrKeyNotExist        = errors.New("cache: key does not exist")
	ErrValNotExist        = errors.New("cache: val does not exist")
	ErrTagCanNotEmpty     = errors.New("cache: tag can not empty")
	ErrTagNotExist        = errors.New("cache: tag does not exist")
	ErrTagKeyNotExist     = errors.New("cache: tag/key does not exist")
	ErrValIsNilPointer    = errors.New("cache: val can not a nil pointer")
	ErrValNotMarshal      = errors.New("cache: val does not Marshal")
	ErrValNotUnmarshal    = errors.New("cache: val does not Unmarshal")
)

Functions

func ChangeFloat32

func ChangeFloat32(key string, step float32, expire time.Duration) (float32, error)

ChangeFloat32 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)

  • expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间

func ChangeFloat64

func ChangeFloat64(key string, step float64, expire time.Duration) (float64, error)

ChangeFloat64 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)

  • expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间

func ChangeInt

func ChangeInt(key string, step int, expire time.Duration) (int, error)

ChangeInt 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)

  • expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间

func ChangeInt32

func ChangeInt32(key string, step int32, expire time.Duration) (int32, error)

ChangeInt32 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)

  • expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间

func ChangeInt64

func ChangeInt64(key string, step int64, expire time.Duration) (int64, error)

ChangeInt64 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)

  • expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间

func Clear

func Clear() error

Clear 清空全部缓存

func Del

func Del(keys ...string) error

Delete 删除缓存

func Get

func Get(key string) ([]byte, error)

Get returns the value for the given key, ie: ([]byte, nil). If the value does not exists it returns (nil, error)

func GetBool

func GetBool(key string) bool

GetBool 读取 BOOL

func GetBoolD

func GetBoolD(key string, defaultVal bool) bool

GetBoolDefault 读取 BOOL If the value does not exists it return defaultVal

func GetDuration

func GetDuration(key string) time.Duration

GetDuration time.Duration => int64

func GetDurationD

func GetDurationD(key string, defaultVal time.Duration) time.Duration

func GetFloat32

func GetFloat32(key string) float32

GetFloat32 读取 FLOAT32

func GetFloat32D

func GetFloat32D(key string, defaultVal float32) float32

GetFloat32Default 读取 FLOAT32 If the value does not exists it return defaultVal

func GetFloat64

func GetFloat64(key string) float64

GetFloat64 读取 FLOAT64

func GetFloat64D

func GetFloat64D(key string, defaultVal float64) float64

GetFloat64Default 读取 FLOAT64 If the value does not exists it return defaultVal

func GetIP

func GetIP(key string) net.IP

GetIP

func GetIPD

func GetIPD(key string, defaultVal net.IP) net.IP

GetIPDefault net.IP(nil)

func GetInt

func GetInt(key string) int

GetInt 读取 INT

func GetInt16

func GetInt16(key string) int16

GetInt16 读取 INT16

func GetInt16D

func GetInt16D(key string, defaultVal int16) int16

GetInt16Default 读取 INT16 If the value does not exists it return defaultVal

func GetInt32

func GetInt32(key string) int32

GetInt32 读取 INT32

func GetInt32D

func GetInt32D(key string, defaultVal int32) int32

GetInt32Default 读取 INT32 If the value does not exists it return defaultVal

func GetInt64

func GetInt64(key string) int64

GetInt64 读取 INT64

func GetInt64D

func GetInt64D(key string, defaultVal int64) int64

GetInt64Default 读取 INT64 If the value does not exists it return defaultVal

func GetInt8

func GetInt8(key string) int8

GetInt8 读取 INT8

func GetInt8D

func GetInt8D(key string, defaultVal int8) int8

GetInt8Default 读取 INT8 If the value does not exists it return defaultVal

func GetIntD

func GetIntD(key string, defaultVal int) int

GetIntDefault 读取 INT If the value does not exists it return defaultVal

func GetString

func GetString(key string) string

GetString 读取 String

func GetStringD

func GetStringD(key string, defaultVal string) string

GetStringDefault 读取 String If the value does not exists it return defaultVal

func GetTime

func GetTime(key string) time.Time

GetTime

func GetTimeD

func GetTimeD(key string, defaultVal time.Time) time.Time

func GetTimeMonth

func GetTimeMonth(key string) time.Month

func GetTimeMonthD

func GetTimeMonthD(key string, defaultVal time.Month) time.Month

func GetTimeWeekday

func GetTimeWeekday(key string) time.Weekday

func GetTimeWeekdayD

func GetTimeWeekdayD(key string, defaultVal time.Weekday) time.Weekday

func GetUint

func GetUint(key string) uint

GetUint 读取 UINT

func GetUint16

func GetUint16(key string) uint16

GetUint16 读取 UINT8

func GetUint16D

func GetUint16D(key string, defaultVal uint16) uint16

GetUint16Default 读取 UINT8 If the value does not exists it return defaultVal

func GetUint32

func GetUint32(key string) uint32

GetUint32 读取 UINT32

func GetUint32D

func GetUint32D(key string, defaultVal uint32) uint32

GetUint32Default 读取 UINT32 If the value does not exists it return defaultVal

func GetUint64

func GetUint64(key string) uint64

GetUint64 读取 UINT64

func GetUint64D

func GetUint64D(key string, defaultVal uint64) uint64

GetUint64Default 读取 UINT64 If the value does not exists it return defaultVal

func GetUint8

func GetUint8(key string) uint8

GetUint8 读取 UINT8

func GetUint8D

func GetUint8D(key string, defaultVal uint8) uint8

GetUint8Default 读取 UINT8 If the value does not exists it return defaultVal

func GetUintD

func GetUintD(key string, defaultVal uint) uint

GetUintDefault 读取 UINT If the value does not exists it return defaultVal

func Has

func Has(key string) bool

Has 判断缓存是否存在

func PluginDo

func PluginDo[T any]() *pluginDo[T]

PluginDo 获取一个缓存自动初始化回调缓存包装接口 ( 使用默认 DefaultStore 实例 )

func PluginDoMemory added in v1.1.0

func PluginDoMemory[T any]() *pluginDo[T]

PluginDoMemory 获取一个缓存自动初始化回调缓存包装接口 ( 使用默认 StoreMemory 实例 )

func PluginDoRedis added in v1.1.0

func PluginDoRedis[T any]() *pluginDo[T]

PluginDoRedis 获取一个缓存自动初始化回调缓存包装接口 ( 使用默认 StoreRedis 实例 )

func PluginDoWith added in v1.1.0

func PluginDoWith[T any](store CacheIO) *pluginDo[T]

PluginDoWith 获取一个缓存自动初始化回调缓存包装接口 ( 使用指定 Store CacheIO 实例 )

func PluginSet

func PluginSet[T comparable]() *pluginSet[T]

PluginSet 获取一个缓存泛型化集合插件实例 ( 使用默认 Store 实例 )

func PluginSetMemory added in v1.1.0

func PluginSetMemory[T comparable]() *pluginSet[T]

PluginSetMemory 获取一个缓存泛型化集合插件实例 ( 使用默认 StoreMemory 实例 )

func PluginSetRedis added in v1.1.0

func PluginSetRedis[T comparable]() *pluginSet[T]

PluginSetRedis 获取一个缓存泛型化集合插件实例 ( 使用默认 StoreRedis 实例 )

func PluginSetWith added in v1.1.0

func PluginSetWith[T comparable](store CacheIO) *pluginSet[T]

PluginSetWith 获取一个缓存泛型化集合插件实例 ( 使用指定 Store CacheIO 实例 )

func Scan

func Scan(key string, refVal any) error

Scan 方式获取缓存对象,适用于 Slice/Map/Struct 缓存对象, 反序列化扫描后放入引用地址变量 注意: 不建议直接获取任何非基本类型的缓存进行断言转换,Cache 驱动为Memory时工作正常,如为Redis则无法转换

func Set

func Set(key string, val any) error

Set 设置缓存(实例默认过期时间)

func SetDefaultMemory added in v1.1.0

func SetDefaultMemory(io CacheIO)

SetDefaultMemory 设置全局默认StoreMemory缓存实例

func SetDefaultRedis added in v1.1.0

func SetDefaultRedis(io CacheIO)

SetDefaultRedis 设置全局默认StoreRedis缓存实例

func SetDefaultStoreType added in v1.1.0

func SetDefaultStoreType(t StoreType)

SetDefaultStoreType 设置默认缓存仓库的驱动类型 StoreType

func SetExpire

func SetExpire(key string, val any, expire time.Duration) error

SetExpire 设置缓存(标记过期时间)

func SetJsoner

func SetJsoner(lib JsonerAPI)

SetJsoner 自定义 Json 操作库,为nil则不设置

func SetLogger

func SetLogger(log *slog.Logger)

func SetNotExist

func SetNotExist(key string, val any, expire time.Duration) error

SetNotExist 设置缓存(标记过期时间,为0表示永不过期),不存在则设置

func Size

func Size() int64

Size 获取已缓存数据条目的数量

func UseJsonIterator added in v1.1.0

func UseJsonIterator()

UseJsonIterator 切换Json库为 Json Iterator 库

func UseStandardLibrary added in v1.1.0

func UseStandardLibrary()

UseStandardLibrary 切换Json库为 Json StandardLibrary 标准库

Types

type CacheIO

type CacheIO interface {
	//Has 判断缓存是否存在
	Has(key string) bool
	//TTL 获取缓存过期时间 (返回0 表示不过期(实例生命周期内/长久存储) 注意:最小过期时间为1s time.Second)
	TTL(key string) (time.Duration, error)
	//Get 读取缓存
	Get(key string) (val []byte, err error)
	GetString(key string) string
	GetStringD(key string, defaultVal string) string
	GetInt(key string) int
	GetIntD(key string, defaultVal int) int
	GetInt8(key string) int8
	GetInt8D(key string, defaultVal int8) int8
	GetInt16(key string) int16
	GetInt16D(key string, defaultVal int16) int16
	GetInt32(key string) int32
	GetInt32D(key string, defaultVal int32) int32
	GetInt64(key string) int64
	GetInt64D(key string, defaultVal int64) int64
	GetUint(key string) uint
	GetUintD(key string, defaultVal uint) uint
	GetUint8(key string) uint8
	GetUint8D(key string, defaultVal uint8) uint8
	GetUint16(key string) uint16
	GetUint16D(key string, defaultVal uint16) uint16
	GetUint32(key string) uint32
	GetUint32D(key string, defaultVal uint32) uint32
	GetUint64(key string) uint64
	GetUint64D(key string, defaultVal uint64) uint64
	GetFloat32(key string) float32
	GetFloat32D(key string, defaultVal float32) float32
	GetFloat64(key string) float64
	GetFloat64D(key string, defaultVal float64) float64
	GetBool(key string) bool
	GetBoolD(key string, defaultVal bool) bool
	GetTime(key string) time.Time
	GetTimeD(key string, defaultVal time.Time) time.Time
	GetDuration(key string) time.Duration
	GetDurationD(key string, defaultVal time.Duration) time.Duration
	GetTimeMonth(key string) time.Month
	GetTimeMonthD(key string, defaultVal time.Month) time.Month
	GetTimeWeekday(key string) time.Weekday
	GetTimeWeekdayD(key string, defaultVal time.Weekday) time.Weekday
	GetIP(key string) net.IP
	GetIPD(key string, defaultVal net.IP) net.IP
	// Scan 方式获取缓存对象,适用于 Slice/Map/Struct 缓存对象, 反序列化扫描后放入引用地址变量
	Scan(key string, refVal any) error

	// Set 设置缓存(默认过期时间)
	Set(key string, val any) error
	// SetExpire 设置缓存(标记过期时间,为0表示永不过期) , 注意:最小过期时间为 1s => time.Second
	SetExpire(key string, val any, expire time.Duration) error
	// SetNotExist 设置缓存(标记过期时间,为0表示永不过期),不存在则设置
	SetNotExist(key string, val any, expire time.Duration) error
	// ChangeInt 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减) 标记过期时间,为0表示永不过期
	//   - expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间
	ChangeInt(key string, step int, expire time.Duration) (newVal int, err error)
	// ChangeInt32 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减) 标记过期时间,为0表示永不过期
	//   - expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间
	ChangeInt32(key string, step int32, expire time.Duration) (newVal int32, err error)
	// ChangeInt64 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减) 标记过期时间,为0表示永不过期
	//   - expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间
	ChangeInt64(key string, step int64, expire time.Duration) (newVal int64, err error)
	// ChangeFloat32 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减) 标记过期时间,为0表示永不过期
	//   - expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间
	ChangeFloat32(key string, step float32, expire time.Duration) (newVal float32, err error)
	// ChangeFloat64 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减) 标记过期时间,为0表示永不过期
	//   - expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间
	ChangeFloat64(key string, step float64, expire time.Duration) (newVal float64, err error)

	//Del 删除缓存
	Del(keys ...string) error
	//Clear 清空全部缓存
	Clear() error
	//Size 获取已缓存数据的条目
	Size() int64
	//StoreType 获取缓存库的类型
	StoreType() StoreType
	//Tags 指定缓存标签
	Tags(tags ...string) TagsIO
	//Stats 获取性能指标统计信息
	Stats() StoreStats
	// 判断当且Cache是否为全局默认实例
	IsGlobal() bool
	// contains filtered or unexported methods
}

func GetDefault added in v1.1.0

func GetDefault() CacheIO

GetDefault 获取默认缓存实例 StoreIO

  • 如果默认实例未配置(未初始化),将自动尝试使用默认参数进行初始化
  • 用户可通过 `SetDefaultStoreType` 设置默认缓存实例的 StoreType 驱动类型(默认为 StoreMemory)
  • 用户可通过 `SetDefaultMemory` 设置自定义默认 StoreMemory 缓存实例
  • 用户可通过 `SetDefaultRedis` 设置自定义默认 StoreRedis 缓存实例

func GetDefaultMemory added in v1.1.0

func GetDefaultMemory() CacheIO

GetDefaultMemory 获取全局默认 StoreMemory 缓存实例

  • 用户可通过 `SetDefaultMemory` 设置自定义默认 StoreMemory 缓存实例
  • 如果默认实例未配置(未初始化),会尝试使用默认参数自动延迟初始化

func GetDefaultRedis added in v1.1.0

func GetDefaultRedis() CacheIO

GetDefaultRedis 全局StoreRedis缓存

  • 用户可通过 `SetDefaultRedis` 设置自定义默认 StoreRedis 缓存实例
  • 如果默认实例未配置(未初始化),会尝试使用默认参数自动延迟初始化

type JsonerAPI

type JsonerAPI interface {
	// Marshal adapts to json/encoding Marshal API
	// Refer to https://godoc.org/encoding/json#Marshal for more information
	Marshal(v any) ([]byte, error)
	// Unmarshal adapts to json/encoding Unmarshal API
	// Refer to https://godoc.org/encoding/json#Unmarshal for more information
	Unmarshal(data []byte, v any) error
	// Valid reports whether data is a valid JSON encoding
	Valid(v []byte) bool
}

type MemoryCache

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

func NewMemory

func NewMemory(options MemoryOptions) (*MemoryCache, error)

func (*MemoryCache) ChangeFloat32

func (s *MemoryCache) ChangeFloat32(key string, step float32, expire time.Duration) (float32, error)

ChangeFloat32 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)

  • expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间

func (*MemoryCache) ChangeFloat64

func (s *MemoryCache) ChangeFloat64(key string, step float64, expire time.Duration) (float64, error)

ChangeFloat64 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)

  • expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间

func (*MemoryCache) ChangeInt

func (s *MemoryCache) ChangeInt(key string, step int, expire time.Duration) (int, error)

ChangeInt 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)

  • expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间

func (*MemoryCache) ChangeInt32

func (s *MemoryCache) ChangeInt32(key string, step int32, expire time.Duration) (int32, error)

ChangeInt32 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)

  • expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间

func (*MemoryCache) ChangeInt64

func (s *MemoryCache) ChangeInt64(key string, step int64, expire time.Duration) (int64, error)

ChangeInt64 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)

  • expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间

func (*MemoryCache) Clear

func (s *MemoryCache) Clear() error

Clear 清空全部缓存

func (*MemoryCache) Del

func (s *MemoryCache) Del(keys ...string) error

Delete 删除缓存

func (*MemoryCache) Get

func (s *MemoryCache) Get(key string) ([]byte, error)

Get returns the value for the given key, ie: ([]byte, nil). If the value does not exists it returns (nil, error)

func (*MemoryCache) GetBool

func (s *MemoryCache) GetBool(key string) bool

GetBool 读取 BOOL

func (*MemoryCache) GetBoolD

func (s *MemoryCache) GetBoolD(key string, defaultVal bool) bool

GetBoolDefault 读取 BOOL If the value does not exists it return defaultVal

func (*MemoryCache) GetDuration

func (s *MemoryCache) GetDuration(key string) time.Duration

GetDuration time.Duration => int64

func (*MemoryCache) GetDurationD

func (s *MemoryCache) GetDurationD(key string, defaultVal time.Duration) time.Duration

GetDurationD

func (*MemoryCache) GetFloat32

func (s *MemoryCache) GetFloat32(key string) float32

GetFloat32 读取 FLOAT32

func (*MemoryCache) GetFloat32D

func (s *MemoryCache) GetFloat32D(key string, defaultVal float32) float32

GetFloat32Default 读取 FLOAT32 If the value does not exists it return defaultVal

func (*MemoryCache) GetFloat64

func (s *MemoryCache) GetFloat64(key string) float64

GetFloat64 读取 FLOAT64

func (*MemoryCache) GetFloat64D

func (s *MemoryCache) GetFloat64D(key string, defaultVal float64) float64

GetFloat64Default 读取 FLOAT64 If the value does not exists it return defaultVal

func (*MemoryCache) GetIP

func (s *MemoryCache) GetIP(key string) net.IP

GetIP

func (*MemoryCache) GetIPD

func (s *MemoryCache) GetIPD(key string, defaultVal net.IP) net.IP

GetIPD net.IP(nil)

func (*MemoryCache) GetInt

func (s *MemoryCache) GetInt(key string) int

GetInt 读取 INT

func (*MemoryCache) GetInt16

func (s *MemoryCache) GetInt16(key string) int16

GetInt16 读取 INT16

func (*MemoryCache) GetInt16D

func (s *MemoryCache) GetInt16D(key string, defaultVal int16) int16

GetInt16Default 读取 INT16 If the value does not exists it return defaultVal

func (*MemoryCache) GetInt32

func (s *MemoryCache) GetInt32(key string) int32

GetInt32 读取 INT32

func (*MemoryCache) GetInt32D

func (s *MemoryCache) GetInt32D(key string, defaultVal int32) int32

GetInt32Default 读取 INT32 If the value does not exists it return defaultVal

func (*MemoryCache) GetInt64

func (s *MemoryCache) GetInt64(key string) int64

GetInt64 读取 INT64

func (*MemoryCache) GetInt64D

func (s *MemoryCache) GetInt64D(key string, defaultVal int64) int64

GetInt64Default 读取 INT64 If the value does not exists it return defaultVal

func (*MemoryCache) GetInt8

func (s *MemoryCache) GetInt8(key string) int8

GetInt8 读取 INT8

func (*MemoryCache) GetInt8D

func (s *MemoryCache) GetInt8D(key string, defaultVal int8) int8

GetInt8Default 读取 INT8 If the value does not exists it return defaultVal

func (*MemoryCache) GetIntD

func (s *MemoryCache) GetIntD(key string, defaultVal int) int

GetIntDefault 读取 INT If the value does not exists it return defaultVal

func (*MemoryCache) GetString

func (s *MemoryCache) GetString(key string) string

GetString 读取 String

func (*MemoryCache) GetStringD

func (s *MemoryCache) GetStringD(key string, defaultVal string) string

GetStringDefault 读取 String If the value does not exists it return defaultVal

func (*MemoryCache) GetTime

func (s *MemoryCache) GetTime(key string) time.Time

GetTime

func (*MemoryCache) GetTimeD

func (s *MemoryCache) GetTimeD(key string, defaultVal time.Time) time.Time

func (*MemoryCache) GetTimeMonth

func (s *MemoryCache) GetTimeMonth(key string) time.Month

GetTimeMonth time.Month => int

func (*MemoryCache) GetTimeMonthD

func (s *MemoryCache) GetTimeMonthD(key string, defaultVal time.Month) time.Month

GetTimeMonthD

func (*MemoryCache) GetTimeWeekday

func (s *MemoryCache) GetTimeWeekday(key string) time.Weekday

GetTimeWeekday

func (*MemoryCache) GetTimeWeekdayD

func (s *MemoryCache) GetTimeWeekdayD(key string, defaultVal time.Weekday) time.Weekday

GetTimeWeekdayD

func (*MemoryCache) GetUint

func (s *MemoryCache) GetUint(key string) uint

GetUint 读取 UINT

func (*MemoryCache) GetUint16

func (s *MemoryCache) GetUint16(key string) uint16

GetUint16 读取 UINT8

func (*MemoryCache) GetUint16D

func (s *MemoryCache) GetUint16D(key string, defaultVal uint16) uint16

GetUint16Default 读取 UINT8 If the value does not exists it return defaultVal

func (*MemoryCache) GetUint32

func (s *MemoryCache) GetUint32(key string) uint32

GetUint32 读取 UINT32

func (*MemoryCache) GetUint32D

func (s *MemoryCache) GetUint32D(key string, defaultVal uint32) uint32

GetUint32Default 读取 UINT32 If the value does not exists it return defaultVal

func (*MemoryCache) GetUint64

func (s *MemoryCache) GetUint64(key string) uint64

GetUint64 读取 UINT64

func (*MemoryCache) GetUint64D

func (s *MemoryCache) GetUint64D(key string, defaultVal uint64) uint64

GetUint64Default 读取 UINT64 If the value does not exists it return defaultVal

func (*MemoryCache) GetUint8

func (s *MemoryCache) GetUint8(key string) uint8

GetUint8 读取 UINT8

func (*MemoryCache) GetUint8D

func (s *MemoryCache) GetUint8D(key string, defaultVal uint8) uint8

GetUint8Default 读取 UINT8 If the value does not exists it return defaultVal

func (*MemoryCache) GetUintD

func (s *MemoryCache) GetUintD(key string, defaultVal uint) uint

GetUintDefault 读取 UINT If the value does not exists it return defaultVal

func (*MemoryCache) Has

func (s *MemoryCache) Has(key string) bool

Has 判断缓存是否存在

func (*MemoryCache) IsGlobal added in v1.1.0

func (s *MemoryCache) IsGlobal() bool

判断当且Cache是否为全局默认实例

func (*MemoryCache) Scan

func (s *MemoryCache) Scan(key string, refVal any) error

Scan 方式获取缓存对象,适用于 Slice/Map/Struct 缓存对象, 反序列化扫描后放入引用地址变量 注意: 不建议直接获取任何非基本类型的缓存进行断言转换,Cache 驱动为Memory时工作正常,如为Redis则无法转换

func (*MemoryCache) Set

func (s *MemoryCache) Set(key string, val any) error

Set 设置缓存(实例默认过期时间)

func (*MemoryCache) SetExpire

func (s *MemoryCache) SetExpire(key string, val any, expire time.Duration) error

SetExpire 设置缓存(标记过期时间)

func (*MemoryCache) SetNotExist

func (s *MemoryCache) SetNotExist(key string, val any, expire time.Duration) error

SetNotExist 设置缓存(标记过期时间,为0表示永不过期),不存在则设置

func (*MemoryCache) Size

func (s *MemoryCache) Size() int64

Size 获取已缓存数据条目的数量

func (*MemoryCache) Stats

func (s *MemoryCache) Stats() StoreStats

Stats 获取性能指标统计信息

func (*MemoryCache) StoreType

func (s *MemoryCache) StoreType() StoreType

func (*MemoryCache) TTL

func (s *MemoryCache) TTL(key string) (time.Duration, error)

TTL 获取缓存过期时间 (返回0 表示不过期(实例生命周期内/长久存储) 注意:最小过期时间为1s time.Second)

func (*MemoryCache) Tags

func (s *MemoryCache) Tags(tags ...string) TagsIO

Tags 入口

type MemoryOptions

type MemoryOptions struct {
	// CacheSize MemoryCache 占用内存大小, 单位MB,  默认 100M,最小10M
	//  - MemoryCache底层由freecache驱动, 每个freecache有256个segment,因此每个缓存[entry]大小不能超过 freecache内存大小的1/1024
	CacheSize int
	// DefaultExpire 默认过期时间(配置单位为[秒]) 0 表示不过期(实例生命周期内/长久存储) 注意:最小过期时间为1s time.Second
	// FreeCache 的过期时间是一个近似值,实际过期时间会在 (X-1, X] 秒之间,其中 X 是你设置的过期时间。
	DefaultExpire int
}

type RedisCache

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

func NewRedis

func NewRedis(options RedisOptions) (*RedisCache, error)

NewRedis

  • options RedisOptions

func (*RedisCache) ChangeFloat32

func (s *RedisCache) ChangeFloat32(key string, step float32, expire time.Duration) (float32, error)

ChangeFloat32 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)

  • expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间

func (*RedisCache) ChangeFloat64

func (s *RedisCache) ChangeFloat64(key string, step float64, expire time.Duration) (float64, error)

ChangeFloat64 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)

  • expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间

func (*RedisCache) ChangeInt

func (s *RedisCache) ChangeInt(key string, step int, expire time.Duration) (int, error)

ChangeInt 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)

  • expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间

func (*RedisCache) ChangeInt32

func (s *RedisCache) ChangeInt32(key string, step int32, expire time.Duration) (int32, error)

ChangeInt32 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)

  • expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间

func (*RedisCache) ChangeInt64

func (s *RedisCache) ChangeInt64(key string, step int64, expire time.Duration) (int64, error)

ChangeInt64 自增自减更新缓存(步进值为正数表示自增,步进值为负数表示自减)

  • expire 过期时间,如key存在则继续沿用原有过期时间,如key不存在则存储并设置过期时间

func (*RedisCache) Clear

func (s *RedisCache) Clear() error

Clear 清空全部缓存

func (*RedisCache) Del

func (s *RedisCache) Del(keys ...string) error

Delete 删除缓存

func (*RedisCache) Get

func (s *RedisCache) Get(key string) ([]byte, error)

Get returns the value for the given key, ie: ([]byte, nil). If the value does not exists it returns (nil, error)

func (*RedisCache) GetBool

func (s *RedisCache) GetBool(key string) bool

GetBool

func (*RedisCache) GetBoolD

func (s *RedisCache) GetBoolD(key string, defaultVal bool) bool

func (*RedisCache) GetDuration

func (s *RedisCache) GetDuration(key string) time.Duration

GetDuration time.Duration => int64

func (*RedisCache) GetDurationD

func (s *RedisCache) GetDurationD(key string, defaultVal time.Duration) time.Duration

func (*RedisCache) GetFloat32

func (s *RedisCache) GetFloat32(key string) float32

GetFloat32

func (*RedisCache) GetFloat32D

func (s *RedisCache) GetFloat32D(key string, defaultVal float32) float32

func (*RedisCache) GetFloat64

func (s *RedisCache) GetFloat64(key string) float64

GetFloat64

func (*RedisCache) GetFloat64D

func (s *RedisCache) GetFloat64D(key string, defaultVal float64) float64

func (*RedisCache) GetIP

func (s *RedisCache) GetIP(key string) net.IP

GetIP net.IP(nil)

func (*RedisCache) GetIPD

func (s *RedisCache) GetIPD(key string, defaultVal net.IP) net.IP

GetIPDefault

func (*RedisCache) GetInt

func (s *RedisCache) GetInt(key string) int

GetInt

func (*RedisCache) GetInt16

func (s *RedisCache) GetInt16(key string) int16

GetInt16

func (*RedisCache) GetInt16D

func (s *RedisCache) GetInt16D(key string, defaultVal int16) int16

func (*RedisCache) GetInt32

func (s *RedisCache) GetInt32(key string) int32

GetInt32

func (*RedisCache) GetInt32D

func (s *RedisCache) GetInt32D(key string, defaultVal int32) int32

func (*RedisCache) GetInt64

func (s *RedisCache) GetInt64(key string) int64

GetInt64

func (*RedisCache) GetInt64D

func (s *RedisCache) GetInt64D(key string, defaultVal int64) int64

func (*RedisCache) GetInt8

func (s *RedisCache) GetInt8(key string) int8

GetInt8

func (*RedisCache) GetInt8D

func (s *RedisCache) GetInt8D(key string, defaultVal int8) int8

func (*RedisCache) GetIntD

func (s *RedisCache) GetIntD(key string, defaultVal int) int

func (*RedisCache) GetString

func (s *RedisCache) GetString(key string) string

GetString

func (*RedisCache) GetStringD

func (s *RedisCache) GetStringD(key string, defaultVal string) string

func (*RedisCache) GetTime

func (s *RedisCache) GetTime(key string) time.Time

GetTime

func (*RedisCache) GetTimeD

func (s *RedisCache) GetTimeD(key string, defaultVal time.Time) time.Time

func (*RedisCache) GetTimeMonth

func (s *RedisCache) GetTimeMonth(key string) time.Month

GetTimeMonth time.Month => int

func (*RedisCache) GetTimeMonthD

func (s *RedisCache) GetTimeMonthD(key string, defaultVal time.Month) time.Month

GetTimeMonthD

func (*RedisCache) GetTimeWeekday

func (s *RedisCache) GetTimeWeekday(key string) time.Weekday

GetTimeWeekday

func (*RedisCache) GetTimeWeekdayD

func (s *RedisCache) GetTimeWeekdayD(key string, defaultVal time.Weekday) time.Weekday

GetTimeWeekdayD

func (*RedisCache) GetUint

func (s *RedisCache) GetUint(key string) uint

GetUint

func (*RedisCache) GetUint16

func (s *RedisCache) GetUint16(key string) uint16

GetUint16

func (*RedisCache) GetUint16D

func (s *RedisCache) GetUint16D(key string, defaultVal uint16) uint16

func (*RedisCache) GetUint32

func (s *RedisCache) GetUint32(key string) uint32

GetUint32

func (*RedisCache) GetUint32D

func (s *RedisCache) GetUint32D(key string, defaultVal uint32) uint32

func (*RedisCache) GetUint64

func (s *RedisCache) GetUint64(key string) uint64

GetUint64

func (*RedisCache) GetUint64D

func (s *RedisCache) GetUint64D(key string, defaultVal uint64) uint64

func (*RedisCache) GetUint8

func (s *RedisCache) GetUint8(key string) uint8

GetUint8

func (*RedisCache) GetUint8D

func (s *RedisCache) GetUint8D(key string, defaultVal uint8) uint8

func (*RedisCache) GetUintD

func (s *RedisCache) GetUintD(key string, defaultVal uint) uint

func (*RedisCache) Has

func (s *RedisCache) Has(key string) bool

Has 判断缓存是否存在

func (*RedisCache) IsGlobal added in v1.1.0

func (s *RedisCache) IsGlobal() bool

判断当且Cache是否为全局默认实例

func (*RedisCache) Scan

func (s *RedisCache) Scan(key string, refVal any) error

Scan 方式获取缓存对象,适用于 Slice/Map/Struct 缓存对象, 反序列化扫描后放入引用地址变量

func (*RedisCache) Set

func (s *RedisCache) Set(key string, val any) error

Set 设置缓存

func (*RedisCache) SetExpire

func (s *RedisCache) SetExpire(key string, val any, expire time.Duration) error

SetExpire 设置缓存(标记过期时间)

func (*RedisCache) SetNotExist

func (s *RedisCache) SetNotExist(key string, val any, expire time.Duration) error

SetNotExist 设置缓存(标记过期时间,为0表示永不过期),不存在则设置

func (*RedisCache) Size

func (s *RedisCache) Size() int64

Size 获取已缓存数据的条目

func (*RedisCache) Stats

func (s *RedisCache) Stats() StoreStats

Stats 获取性能指标统计信息

func (*RedisCache) StoreType

func (s *RedisCache) StoreType() StoreType

func (*RedisCache) TTL

func (s *RedisCache) TTL(key string) (time.Duration, error)

TTL 获取缓存过期时间 (返回0 表示不过期(实例生命周期内/长久存储) 注意:最小过期时间为1s time.Second)

func (*RedisCache) Tags

func (s *RedisCache) Tags(tags ...string) TagsIO

Tags 入口

type RedisOptions

type RedisOptions struct {
	// Client Redis服务器链接选项,使用 UniversalClient 统一客户端,UniversalClient是对 Client 、 ClusterClient 、 FailoverClient 客户端的包装。
	// 	- 根据不同的选项,客户端的类型如下:
	// 	- 如果指定了 MasterName 选项,则返回 FailoverClient 哨兵客户端。
	// 	- 如果 Addrs 是 2 个以上的地址,则返回 ClusterClient 集群客户端。
	// 	- 其他情况,返回 Client 单节点客户端。
	//  - github.com/redis/go-redis/v9
	// 	- https://redis.uptrace.dev/zh/guide/universal.html
	// 	- RedisUniversalOptions is an alias for [redis.UniversalOptions]
	Client RedisUniversalOptions
	// DefaultExpire 默认过期时间(配置单位为[秒]) 0 表示不过期(实例生命周期内/长久存储) 注意:最小过期时间为1s time.Second
	// FreeCache 的过期时间是一个近似值,实际过期时间会在 (X-1, X] 秒之间,其中 X 是你设置的过期时间。
	DefaultExpire int
}

RedisOptions Redis缓存配置信息

type RedisUniversalOptions added in v1.1.0

type RedisUniversalOptions = redis.UniversalOptions

type StoreStats

type StoreStats struct {
	StoreType StoreType
	Entry     int64   // 已缓存数据条目
	Hit       int64   // 命中数据条目
	Miss      int64   // 丢失数据条目
	HitRate   float64 // 命中率
}

func Stats

func Stats() StoreStats

Stats 获取已缓存数据条目的数量

type StoreType

type StoreType = string
const (
	StoreMemory StoreType = "Memory"
	StoreRedis  StoreType = "Redis"
)

func GetDefaultStoreType added in v1.1.0

func GetDefaultStoreType() StoreType

GetDefaultStoreType 获取默认缓存仓库的驱动类型 StoreType

type TagsIO

type TagsIO interface {
	// Set 指定Tags下设置缓存(默认过期时间)
	Set(key string, val any) error
	// SetExpire 指定Tags下设置缓存(标记过期时间,为0表示永不过期) , 注意:最小过期时间为1s time.Second
	SetExpire(key string, val any, expire time.Duration) error
	// SetNotExist 指定Tags下设置缓存(标记过期时间,为0表示永不过期),不存在则设置 , 注意:最小过期时间为1s time.Second
	SetNotExist(key string, val any, expire time.Duration) error
	//Del 删除指定Tags下的缓存
	Del(keys ...string) error
	//Clear 清空指定Tags下的全部缓存
	Clear() error
	//Size 获取指定Tags下缓存数据的条目
	Size() int64
}

func Tags

func Tags(tags ...string) TagsIO

默认缓存仓库包装代理 TagsIO / CacheIO Tags 入口

Jump to

Keyboard shortcuts

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