Documentation
¶
Index ¶
- Constants
- type CacheProvider
- type Expiration
- func NewExpiration(baseExpireTime, randomRangeTime time.Duration) *Expiration
- func NewExpirationFromHour(baseExpireTime, randomRangeTime int64) *Expiration
- func NewExpirationFromMillisecond(baseExpireTime, randomRangeTime int64) *Expiration
- func NewExpirationFromMinute(baseExpireTime, randomRangeTime int64) *Expiration
- func NewExpirationFromSecond(baseExpireTime, randomRangeTime int64) *Expiration
- type KeyOperation
- func (keyOp *KeyOperation) Create(value any) (bool, error)
- func (keyOp *KeyOperation) Get(value any) error
- func (keyOp *KeyOperation) MustCreate(value any) bool
- func (keyOp *KeyOperation) MustGet(value any)
- func (keyOp *KeyOperation) MustRemove() bool
- func (keyOp *KeyOperation) MustSet(value any)
- func (keyOp *KeyOperation) Remove() (bool, error)
- func (keyOp *KeyOperation) Set(value any) error
- func (keyOp *KeyOperation) TryGet(value any) (bool, error)
- type L2CacheProvider
- func (p *L2CacheProvider) Create(key string, value any, t time.Duration) (result bool, err error)
- func (p *L2CacheProvider) Get(key string, value any) error
- func (p *L2CacheProvider) Increase(key string) (int64, error)
- func (p *L2CacheProvider) IncreaseOrCreate(key string, increment int64, t time.Duration) (int64, error)
- func (p *L2CacheProvider) MustCreate(key string, value any, t time.Duration) bool
- func (p *L2CacheProvider) MustGet(key string, value any)
- func (p *L2CacheProvider) MustIncrease(key string) int64
- func (p *L2CacheProvider) MustIncreaseOrCreate(key string, increment int64, t time.Duration) int64
- func (p *L2CacheProvider) MustRemove(key string) bool
- func (p *L2CacheProvider) MustSet(key string, value any, t time.Duration)
- func (p *L2CacheProvider) Remove(key string) (bool, error)
- func (p *L2CacheProvider) Set(key string, value any, t time.Duration) error
- func (p *L2CacheProvider) TryGet(key string, value any) (result bool, err error)
- type MemoryCacheProvider
- func (cp *MemoryCacheProvider) Create(key string, value any, t time.Duration) (bool, error)
- func (cp *MemoryCacheProvider) Get(key string, value any) error
- func (cp *MemoryCacheProvider) Increase(key string) (int64, error)
- func (cp *MemoryCacheProvider) IncreaseOrCreate(key string, increment int64, t time.Duration) (int64, error)
- func (cp *MemoryCacheProvider) MustCreate(key string, value any, t time.Duration) bool
- func (cp *MemoryCacheProvider) MustGet(key string, value any)
- func (cp *MemoryCacheProvider) MustIncrease(key string) int64
- func (cp *MemoryCacheProvider) MustIncreaseOrCreate(key string, increment int64, t time.Duration) int64
- func (cp *MemoryCacheProvider) MustRemove(key string) bool
- func (cp *MemoryCacheProvider) MustSet(key string, value any, t time.Duration)
- func (cp *MemoryCacheProvider) Remove(key string) (bool, error)
- func (cp *MemoryCacheProvider) Set(key string, value any, t time.Duration) error
- func (cp *MemoryCacheProvider) TryGet(key string, value any) (bool, error)
- type Operation
- type RedisCacheProvider
- func (cli *RedisCacheProvider) Create(key string, value any, t time.Duration) (bool, error)
- func (cli *RedisCacheProvider) Get(key string, value any) error
- func (cli *RedisCacheProvider) Increase(key string) (int64, error)
- func (cli *RedisCacheProvider) IncreaseOrCreate(key string, increment int64, t time.Duration) (int64, error)
- func (cli *RedisCacheProvider) MustCreate(key string, value any, t time.Duration) bool
- func (cli *RedisCacheProvider) MustGet(key string, value any)
- func (cli *RedisCacheProvider) MustIncrease(key string) int64
- func (cli *RedisCacheProvider) MustIncreaseOrCreate(key string, increment int64, t time.Duration) int64
- func (cli *RedisCacheProvider) MustRemove(key string) bool
- func (cli *RedisCacheProvider) MustSet(key string, value any, t time.Duration)
- func (cli *RedisCacheProvider) Remove(key string) (bool, error)
- func (cli *RedisCacheProvider) Set(key string, value any, t time.Duration) error
- func (cli *RedisCacheProvider) TryGet(key string, value any) (bool, error)
- type UnixTime
Examples ¶
Constants ¶
const NoExpiration time.Duration = 0 // 缓存不过期。
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CacheProvider ¶
type CacheProvider interface {
// Get 获取指定缓存值。
// @key: cache key
// @value: receive value
//
// return: 如果key存在,value被更新成对应值, 反之value值不做改变。
Get(key string, value any) error
// MustGet 是 Get 的 panic 版。
MustGet(key string, value any)
// TryGet 尝试获取指定缓存。
// @key: cache key
// @value: receive value
//
// return: 若key存在,value被更新成对应值,返回true,
// 反之value值不做改变,返回false。
TryGet(key string, value any) (bool, error)
// Create 仅当缓存键不存在时,创建缓存。
// @key: cache key
// @value: receive value
// @t: 过期时长, 0表不过期
// return: true表示创建了缓存;false说明缓存已经存在了。
Create(key string, value any, t time.Duration) (bool, error)
// MustCreate 是 Create 的 panic 版。
MustCreate(key string, value any, t time.Duration) bool
// Set 设置或者更新缓存,
// @key: cache key
// @value: receive value
// @t: 过期时长, 0表不过期
Set(key string, value any, t time.Duration) error
// MustSet 是 Set 的 panic 版。
MustSet(key string, value any, t time.Duration)
// Remove 移除指定缓存,
// @key: cache key
// return: true成功移除,false缓存不存在。
Remove(key string) (bool, error)
// MustRemove 是 Remove 的 panic 版。
MustRemove(key string) bool
// Increase 为已存在的指定缓存的值(必须是整数)增加1。
// @key: cache key
// return: 符合条件返回增加后的值,反之返回默认值,以及对应的error.
Increase(key string) (int64, error)
// MustIncrease 是 Increase 的 panic 版。
MustIncrease(key string) int64
// Increase 为指定缓存的值增加一个增量(负数==减法),如果不存在则创建该缓存。
// @key: cache key
// @increment: 增量,如果key不存在,则当成新缓存的value
// @t: 过期时长, 0表不过期
// return: 返回增加后的值。
IncreaseOrCreate(key string, increment int64, t time.Duration) (int64, error)
// MustIncreaseOrCreate 是 IncreaseOrCreate 的 panic 版。
MustIncreaseOrCreate(key string, increment int64, t time.Duration) int64
}
CacheProvider 提供一套缓存语义.
type Expiration ¶
type Expiration struct {
// contains filtered or unexported fields
}
Expiration 缓存过期时间。
var CacheExpirationZero *Expiration = NewExpiration(0, 0) // 缓存不过期。
func NewExpiration ¶
func NewExpiration(baseExpireTime, randomRangeTime time.Duration) *Expiration
NewExpiration 新建缓存过期时间。
@baseExpireTime: 基准过期时长,0表不过期 @randomRangeTime: 随机过期市场,0表不随机,否则baseExpireTime将增加[-randomRangeTime, +randomRangeTime]
func NewExpirationFromHour ¶
func NewExpirationFromHour(baseExpireTime, randomRangeTime int64) *Expiration
NewExpirationFromHour 以小时为单位创建缓存过期时间。
func NewExpirationFromMillisecond ¶
func NewExpirationFromMillisecond(baseExpireTime, randomRangeTime int64) *Expiration
NewExpirationFromMillisecond 以毫秒为单位创建缓存过期时间。
func NewExpirationFromMinute ¶
func NewExpirationFromMinute(baseExpireTime, randomRangeTime int64) *Expiration
NewExpirationFromMinute 以分钟为单位创建缓存过期时间。
func NewExpirationFromSecond ¶
func NewExpirationFromSecond(baseExpireTime, randomRangeTime int64) *Expiration
NewExpirationFromSecond 以秒为单位创建缓存过期时间。
func (Expiration) BaseExpireTime ¶
func (c Expiration) BaseExpireTime() time.Duration
func (*Expiration) NextExpireTime ¶
func (c *Expiration) NextExpireTime() time.Duration
NextExpireTime 获取一个新的过期时间,如果存在随机量的话,返回值已经过随机量计算。
func (Expiration) RandomRangeTime ¶
func (c Expiration) RandomRangeTime() time.Duration
type KeyOperation ¶
type KeyOperation struct {
// 缓存key。
Key string
// contains filtered or unexported fields
}
Example ¶
// 创建一个缓存操作对象。
// 指定key的组成: go:cache:test<_flag1><_flag2>
// 指定缓存提供器:内存缓存,切一分钟清理一次过期key
// 指定缓存的过期时间为,10分钟 再加上 2分钟 的随机量 上下波动
//
// 正常情况下该变量声明成全局变量,不需要重复声明。
cacheOp := NewOperation(
"go", "cache:test", 2,
NewMemoryCacheProvider(1*time.Minute),
NewExpirationFromMinute(10, 2))
// 获取 key 操作对象
// 这个对象中包含了组装好的完整换缓存key,
// go:cache:test_unixTime_1,
// cacheOp 指定了两个flag,这边就必须传两个参数,多、少都不行。
key := cacheOp.Key(time.Now(), true)
// 获取完整缓存key
// log.Println(key.Key)
// output: go:cache:test_1625123485000_1
key.Set("hellow word!")
var value string
key.Get(&value)
log.Println(value)
// output: hellow word!
// 支持基础类型互转,存进去一个 int8 用 int 去接。
key.Set(int(8))
var intV int
key.Get(&intV)
log.Println(intV)
Output: 8
func (*KeyOperation) Create ¶
func (keyOp *KeyOperation) Create(value any) (bool, error)
Create 仅当缓存键不存在时,创建缓存, t 过期时长, 0 表不过期。 return: true表示创建了缓存;false说明缓存已经存在了。
func (*KeyOperation) Get ¶
func (keyOp *KeyOperation) Get(value any) error
Get 获取指定缓存值, 如果key存在,value被更新成对应值, 反之value值不做改变。
func (*KeyOperation) MustCreate ¶
func (keyOp *KeyOperation) MustCreate(value any) bool
MustCreate 是 Create 的 panic 版。
func (*KeyOperation) MustGet ¶
func (keyOp *KeyOperation) MustGet(value any)
MustGet 是 Get 的 panic 版。
func (*KeyOperation) MustRemove ¶
func (keyOp *KeyOperation) MustRemove() bool
MustRemove 是 Remove 的 panic 版。
func (*KeyOperation) MustSet ¶
func (keyOp *KeyOperation) MustSet(value any)
MustSet 是 Set 的 panic 版。
func (*KeyOperation) Remove ¶
func (keyOp *KeyOperation) Remove() (bool, error)
Remove 移除指定缓存, return: true成功移除,false缓存不存在。
func (*KeyOperation) Set ¶
func (keyOp *KeyOperation) Set(value any) error
Set 设置或者更新缓存, t 过期时长, 0 表不过期。
func (*KeyOperation) TryGet ¶
func (keyOp *KeyOperation) TryGet(value any) (bool, error)
TryGet 尝试获取指定缓存, 若key存在,value被更新成对应值,返回true, 反之value值不做改变,返回false。
type L2CacheProvider ¶ added in v1.0.1
type L2CacheProvider struct {
// contains filtered or unexported fields
}
L2CacheProvider 实现简单的两级缓存,不支持Increase, 两个层次的缓存使用相同的缓存key, 所以两个层级的缓存需要使用不同的缓存提供器,防止相互覆盖。
当一级获取不到,将从二级获取(一般来说,一级回收间隔更短), 并且根据二级缓存更新一级缓存。
实际上可以看成以 Level 2 为主,Level 1 为辅助,提高访问性能。
func NewL2CacheProvider ¶
func NewL2CacheProvider(l1, l2 CacheProvider, expireTime *Expiration) *L2CacheProvider
func (*L2CacheProvider) Get ¶ added in v1.0.1
func (p *L2CacheProvider) Get(key string, value any) error
implement ICacheProvider.Get
func (*L2CacheProvider) Increase ¶ added in v1.0.1
func (p *L2CacheProvider) Increase(key string) (int64, error)
implement ICacheProvider.Increase, not implemented, will panic
func (*L2CacheProvider) IncreaseOrCreate ¶ added in v1.0.1
func (p *L2CacheProvider) IncreaseOrCreate(key string, increment int64, t time.Duration) (int64, error)
implement ICacheProvider.IncreaseOrCreate, not implemented, will panic
func (*L2CacheProvider) MustCreate ¶ added in v1.0.1
func (p *L2CacheProvider) MustCreate(key string, value any, t time.Duration) bool
implement ICacheProvider.MustCreate
func (*L2CacheProvider) MustGet ¶ added in v1.0.1
func (p *L2CacheProvider) MustGet(key string, value any)
implement ICacheProvider.MustGet
func (*L2CacheProvider) MustIncrease ¶ added in v1.0.1
func (p *L2CacheProvider) MustIncrease(key string) int64
implement ICacheProvider.MustIncrease, not implemented, will panic
func (*L2CacheProvider) MustIncreaseOrCreate ¶ added in v1.0.1
implement ICacheProvider.MustIncreaseOrCreate, not implemented, will panic
func (*L2CacheProvider) MustRemove ¶ added in v1.0.1
func (p *L2CacheProvider) MustRemove(key string) bool
implement ICacheProvider.MustRemove
func (*L2CacheProvider) MustSet ¶ added in v1.0.1
func (p *L2CacheProvider) MustSet(key string, value any, t time.Duration)
implement ICacheProvider.MustSet
func (*L2CacheProvider) Remove ¶ added in v1.0.1
func (p *L2CacheProvider) Remove(key string) (bool, error)
implement ICacheProvider.Remove
type MemoryCacheProvider ¶
type MemoryCacheProvider struct {
// contains filtered or unexported fields
}
内存 类型的缓存提供器, 数据的组织方式,基础类型直接使用
func NewMemoryCacheProvider ¶
func NewMemoryCacheProvider(cleanupInterval time.Duration) *MemoryCacheProvider
NewMemoryCacheProvider
func (*MemoryCacheProvider) Get ¶
func (cp *MemoryCacheProvider) Get(key string, value any) error
implement ICacheProvider.Get
func (*MemoryCacheProvider) Increase ¶
func (cp *MemoryCacheProvider) Increase(key string) (int64, error)
implement ICacheProvider.Increase
func (*MemoryCacheProvider) IncreaseOrCreate ¶
func (cp *MemoryCacheProvider) IncreaseOrCreate(key string, increment int64, t time.Duration) (int64, error)
implement ICacheProvider.IncreaseOrCreate
func (*MemoryCacheProvider) MustCreate ¶
func (cp *MemoryCacheProvider) MustCreate(key string, value any, t time.Duration) bool
implement ICacheProvider.MustCreate
func (*MemoryCacheProvider) MustGet ¶
func (cp *MemoryCacheProvider) MustGet(key string, value any)
implement ICacheProvider.MustGet
func (*MemoryCacheProvider) MustIncrease ¶
func (cp *MemoryCacheProvider) MustIncrease(key string) int64
implement ICacheProvider.MustIncrease
func (*MemoryCacheProvider) MustIncreaseOrCreate ¶
func (cp *MemoryCacheProvider) MustIncreaseOrCreate(key string, increment int64, t time.Duration) int64
implement ICacheProvider.MustIncreaseOrCreate
func (*MemoryCacheProvider) MustRemove ¶
func (cp *MemoryCacheProvider) MustRemove(key string) bool
implement ICacheProvider.MustRemove
func (*MemoryCacheProvider) MustSet ¶
func (cp *MemoryCacheProvider) MustSet(key string, value any, t time.Duration)
implement ICacheProvider.MustSet
func (*MemoryCacheProvider) Remove ¶
func (cp *MemoryCacheProvider) Remove(key string) (bool, error)
implement ICacheProvider.Remove
type Operation ¶
type Operation struct {
// contains filtered or unexported fields
}
Operation 缓存操作对象。
func NewOperation ¶
func NewOperation(cacheNamespace, keyPrefix string, uniqueFlagLen int, cacheProvider CacheProvider, expireTime *Expiration) *Operation
NewOperation 创建一个缓存操作对象, 缓存key分三段 <CacheNamespace>:<Prefix>[:unique flag] expireTime : 过期时长, nil或者CacheExpirationZero 表不过期。 uniqueFlagLen : 指定用来拼接[:unique flag]部分的元素个数。
func (*Operation) Key ¶
func (c *Operation) Key(keys ...interface{}) *KeyOperation
Key 获取指定key的缓存操作对象。
type RedisCacheProvider ¶
type RedisCacheProvider struct {
// contains filtered or unexported fields
}
Redis 类型的缓存提供器。 数据的组织方式,基础类型直接使用
func NewRedisCacheProvider ¶
func NewRedisCacheProvider(cli redis.Cmdable) *RedisCacheProvider
func (*RedisCacheProvider) Get ¶
func (cli *RedisCacheProvider) Get(key string, value any) error
implement ICacheProvider.Get
func (*RedisCacheProvider) Increase ¶
func (cli *RedisCacheProvider) Increase(key string) (int64, error)
implement ICacheProvider.Increase
func (*RedisCacheProvider) IncreaseOrCreate ¶
func (cli *RedisCacheProvider) IncreaseOrCreate(key string, increment int64, t time.Duration) (int64, error)
implement ICacheProvider.IncreaseOrCreate
func (*RedisCacheProvider) MustCreate ¶
func (cli *RedisCacheProvider) MustCreate(key string, value any, t time.Duration) bool
implement ICacheProvider.MustCreate
func (*RedisCacheProvider) MustGet ¶
func (cli *RedisCacheProvider) MustGet(key string, value any)
implement ICacheProvider.MustGet
func (*RedisCacheProvider) MustIncrease ¶
func (cli *RedisCacheProvider) MustIncrease(key string) int64
implement ICacheProvider.MustIncrease
func (*RedisCacheProvider) MustIncreaseOrCreate ¶
func (cli *RedisCacheProvider) MustIncreaseOrCreate(key string, increment int64, t time.Duration) int64
implement ICacheProvider.MustIncreaseOrCreate
func (*RedisCacheProvider) MustRemove ¶
func (cli *RedisCacheProvider) MustRemove(key string) bool
implement ICacheProvider.MustRemove
func (*RedisCacheProvider) MustSet ¶
func (cli *RedisCacheProvider) MustSet(key string, value any, t time.Duration)
implement ICacheProvider.MustSet
func (*RedisCacheProvider) Remove ¶
func (cli *RedisCacheProvider) Remove(key string) (bool, error)
implement ICacheProvider.Remove
type UnixTime ¶
UnixTime 毫秒级时间戳。
func (UnixTime) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*UnixTime) UnmarshalJSON ¶
UnmarshalJSON implements json.UnmarshalJSON.