Documentation
¶
Index ¶
- Constants
- type Cache
- func New[K comparable, V any](config Config) *Cache[K, V]
- func NewCache[K comparable, V any](defaultExpiration, cleanupInterval time.Duration, options ...int) *Cache[K, V]
- func NewCacheWithPersistence[K comparable, V any](defaultExpiration, cleanupInterval time.Duration, persistPath string, ...) *Cache[K, V]
- func (c *Cache[K, V]) AddToListWithLimit(key K, value V, maxSize int)
- func (c *Cache[K, V]) BLPop(key K, timeout time.Duration) (V, bool)
- func (c *Cache[K, V]) BRPop(key K, timeout time.Duration) (V, bool)
- func (c *Cache[K, V]) BRPopLPush(source K, destination K, timeout time.Duration) (V, bool)
- func (c *Cache[K, V]) BeginTransaction() *Transaction[K, V]
- func (c *Cache[K, V]) Clear()
- func (c *Cache[K, V]) Close()
- func (c *Cache[K, V]) Count() int
- func (c *Cache[K, V]) Delete(key K)
- func (c *Cache[K, V]) DeleteExpired()
- func (c *Cache[K, V]) DeleteList(key K)
- func (c *Cache[K, V]) DisableAutoPersist()
- func (c *Cache[K, V]) EnableAutoPersist()
- func (c *Cache[K, V]) Flush() error
- func (c *Cache[K, V]) ForEach(fn func(key K, value V) bool)
- func (c *Cache[K, V]) ForEachList(fn func(key K, list []V) bool)
- func (c *Cache[K, V]) Get(key K) (V, bool)
- func (c *Cache[K, V]) GetListTTL(key K) (time.Duration, bool)
- func (c *Cache[K, V]) GetStats() Stats
- func (c *Cache[K, V]) GetTTL(key K) (time.Duration, bool)
- func (c *Cache[K, V]) GetWithTTL(key K) (V, time.Duration, bool)
- func (c *Cache[K, V]) Has(key K) bool
- func (c *Cache[K, V]) HasList(key K) bool
- func (c *Cache[K, V]) Increment(key K, increment any) (any, error)
- func (c *Cache[K, V]) Keys() []K
- func (c *Cache[K, V]) LIndex(key K, index int) (V, bool)
- func (c *Cache[K, V]) LInsert(key K, before bool, pivot V, value V, equals func(a, b V) bool) int
- func (c *Cache[K, V]) LLen(key K) int
- func (c *Cache[K, V]) LPop(key K) (V, bool)
- func (c *Cache[K, V]) LPush(key K, values ...V) int
- func (c *Cache[K, V]) LRange(key K, start, stop int) []V
- func (c *Cache[K, V]) LRem(key K, count int, value V, equals func(a, b V) bool) int
- func (c *Cache[K, V]) LSet(key K, index int, value V) bool
- func (c *Cache[K, V]) LTrim(key K, start, stop int) bool
- func (c *Cache[K, V]) ListCount() int
- func (c *Cache[K, V]) ListKeys() []K
- func (c *Cache[K, V]) Load() error
- func (c *Cache[K, V]) LoadOrInit() error
- func (c *Cache[K, V]) RPop(key K) (V, bool)
- func (c *Cache[K, V]) RPoplPush(source K, destination K) (V, bool)
- func (c *Cache[K, V]) RPush(key K, values ...V) int
- func (c *Cache[K, V]) Save() error
- func (c *Cache[K, V]) Set(key K, value V, duration ...time.Duration)
- func (c *Cache[K, V]) SetList(key K, duration ...time.Duration)
- func (c *Cache[K, V]) WithPersistence(persistPath string, autoPersistInterval time.Duration) *Cache[K, V]
- type CacheItem
- type Config
- type ListItem
- type PersistenceData
- type Shard
- type Stats
- type Transaction
- func (t *Transaction[K, V]) Commit()
- func (t *Transaction[K, V]) Delete(key K)
- func (t *Transaction[K, V]) DeleteList(key K)
- func (t *Transaction[K, V]) LPush(key K, values ...V)
- func (t *Transaction[K, V]) Rollback()
- func (t *Transaction[K, V]) Set(key K, value V, duration time.Duration)
- func (t *Transaction[K, V]) SetListExpiration(key K, duration time.Duration)
Constants ¶
const ( // DefaultHistorySize 默认历史记录大小 DefaultHistorySize = 50 // DefaultBufferSize 默认缓存缓冲区大小 DefaultBufferSize = 512 // DefaultShardCount 默认分片数量,设为CPU数量的2倍提高并发性 DefaultShardCount = 16 // DefaultCleanupInterval 默认清理过期数据的间隔 DefaultCleanupInterval = 5 * time.Minute // DefaultItemsPerCleanup 每次清理处理的最大项目数,避免长时间锁定 DefaultItemsPerCleanup = 1000 )
添加分片常量和条件变量相关定义
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
Cache 是一个综合的缓存实现,支持内存缓存和列表缓存
func NewCache ¶
func NewCache[K comparable, V any](defaultExpiration, cleanupInterval time.Duration, options ...int) *Cache[K, V]
NewCache 创建一个新的综合缓存 defaultExpiration: 默认的过期时间 cleanupInterval: 清理过期项的间隔时间 可选的shardCount: 分片数量,默认为CPU核心数*2
func NewCacheWithPersistence ¶
func NewCacheWithPersistence[K comparable, V any](defaultExpiration, cleanupInterval time.Duration, persistPath string, autoPersistInterval time.Duration) *Cache[K, V]
NewCacheWithPersistence 初始化带持久化的全局缓存
func (*Cache[K, V]) AddToListWithLimit ¶ added in v0.1.5
AddToListWithLimit 将元素添加到列表,并在超出限制时修剪列表
func (*Cache[K, V]) BRPopLPush ¶
BRPopLPush 阻塞版本的RPoplPush,使用通知机制代替轮询 timeout为等待时间,0表示无限等待
func (*Cache[K, V]) BeginTransaction ¶
func (c *Cache[K, V]) BeginTransaction() *Transaction[K, V]
BeginTransaction 开始一个新事务
func (*Cache[K, V]) DeleteExpired ¶
func (c *Cache[K, V]) DeleteExpired()
DeleteExpired 删除所有过期的项 采用分片清理策略减少锁争用,每次只清理部分数据
func (*Cache[K, V]) DisableAutoPersist ¶
func (c *Cache[K, V]) DisableAutoPersist()
DisableAutoPersist 禁用自动持久化
func (*Cache[K, V]) EnableAutoPersist ¶
func (c *Cache[K, V]) EnableAutoPersist()
EnableAutoPersist 启用自动持久化
func (*Cache[K, V]) ForEachList ¶
ForEachList 遍历所有未过期的列表
func (*Cache[K, V]) GetListTTL ¶
GetListTTL 获取列表键的剩余生存时间
func (*Cache[K, V]) GetWithTTL ¶
GetWithTTL 获取缓存项及其剩余生存时间
func (*Cache[K, V]) LInsert ¶
LInsert 在列表的指定位置插入元素 before == true表示在pivot之前插入 before == false表示在pivot之后插入
func (*Cache[K, V]) LRem ¶
LRem 移除列表中与参数value相等的元素 count > 0: 从头往尾移除count个值为value的元素 count < 0: 从尾往头移除count个值为value的元素 count = 0: 移除所有值为value的元素
func (*Cache[K, V]) LoadOrInit ¶
LoadOrInit 从文件加载缓存,如果文件不存在则初始化一个新的缓存
type Config ¶ added in v0.1.5
type Config struct {
// TTL 默认过期时间
TTL time.Duration
// CleanupInterval 清理过期项的间隔
CleanupInterval time.Duration
// ShardCount 分片数量(可选)
ShardCount int
// PersistPath 持久化文件路径(可选)
PersistPath string
// AutoPersistInterval 自动持久化间隔(可选)
AutoPersistInterval time.Duration
}
Config 缓存配置
type PersistenceData ¶
type PersistenceData[K comparable, V any] struct { Items map[K]CacheItem[V] ListItems map[K][]ListItem[V] Expiration map[K]int64 DefaultExpiration time.Duration }
PersistenceData 持久化数据结构
type Shard ¶ added in v0.1.5
type Shard[K comparable, V any] struct { // contains filtered or unexported fields }
Shard 表示缓存的一个分片
type Stats ¶
type Stats struct {
ItemsCount int `json:"itemsCount"` // 普通缓存项数量
ListsCount int `json:"listsCount"` // 列表数量
HitCount uint64 `json:"hitCount"` // 命中次数
MissCount uint64 `json:"missCount"` // 未命中次数
LastSaveTime time.Time `json:"lastSaveTime"` // 最后一次保存时间
LastLoadTime time.Time `json:"lastLoadTime"` // 最后一次加载时间
CreationTime time.Time `json:"creationTime"` // 创建时间
MemoryUsage uint64 `json:"memoryUsage"` // 预估内存使用(字节)
ExpiredCount uint64 `json:"expiredCount"` // 过期项目计数
DeletedCount uint64 `json:"deletedCount"` // 删除项目计数
PersistPath string `json:"persistPath"` // 持久化路径
IsAutoPersist bool `json:"isAutoPersist"` // 是否开启自动持久化
SaveInterval time.Duration `json:"saveInterval"` // 保存间隔
}
Stats 缓存统计信息
type Transaction ¶
type Transaction[K comparable, V any] struct { // contains filtered or unexported fields }
Transaction 表示一个缓存事务
func (*Transaction[K, V]) DeleteList ¶
func (t *Transaction[K, V]) DeleteList(key K)
DeleteList 在事务中删除列表
func (*Transaction[K, V]) LPush ¶
func (t *Transaction[K, V]) LPush(key K, values ...V)
LPush 在事务中将值插入列表头部
func (*Transaction[K, V]) Rollback ¶
func (t *Transaction[K, V]) Rollback()
Rollback 回滚事务(什么也不做,因为事务只在Commit时才会应用)
func (*Transaction[K, V]) Set ¶
func (t *Transaction[K, V]) Set(key K, value V, duration time.Duration)
Set 在事务中设置缓存项
func (*Transaction[K, V]) SetListExpiration ¶
func (t *Transaction[K, V]) SetListExpiration(key K, duration time.Duration)
SetListExpiration 在事务中设置列表过期时间