cache

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

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) *Cache[K, V]

NewCache 创建一个新的综合缓存 defaultExpiration: 默认的过期时间 cleanupInterval: 清理过期项的间隔时间

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]) BLPop

func (c *Cache[K, V]) BLPop(key K, timeout time.Duration) (V, bool)

BLPop 阻塞版本的LPop,不支持真正的阻塞,但可以模拟轮询 timeout为等待时间,0表示无限等待

func (*Cache[K, V]) BRPop

func (c *Cache[K, V]) BRPop(key K, timeout time.Duration) (V, bool)

BRPop 阻塞版本的RPop,不支持真正的阻塞,但可以模拟轮询 timeout为等待时间,0表示无限等待

func (*Cache[K, V]) BRPopLPush

func (c *Cache[K, V]) BRPopLPush(source K, destination K, timeout time.Duration) (V, bool)

BRPopLPush 阻塞版本的RPoplPush timeout为等待时间,0表示无限等待

func (*Cache[K, V]) BeginTransaction

func (c *Cache[K, V]) BeginTransaction() *Transaction[K, V]

BeginTransaction 开始一个新事务

func (*Cache[K, V]) Clear

func (c *Cache[K, V]) Clear()

Clear 清空缓存

func (*Cache[K, V]) Close

func (c *Cache[K, V]) Close()

Close 停止清理过期项和自动持久化的goroutine

func (*Cache[K, V]) Count

func (c *Cache[K, V]) Count() int

Count 返回缓存中的普通项目数量

func (*Cache[K, V]) Delete

func (c *Cache[K, V]) Delete(key K)

Delete 删除缓存项

func (*Cache[K, V]) DeleteExpired

func (c *Cache[K, V]) DeleteExpired()

DeleteExpired 删除所有过期的项

func (*Cache[K, V]) DeleteList

func (c *Cache[K, V]) DeleteList(key K)

DeleteList 删除列表

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]) Flush

func (c *Cache[K, V]) Flush() error

Flush 清除所有数据并删除持久化文件

func (*Cache[K, V]) ForEach

func (c *Cache[K, V]) ForEach(fn func(key K, value V) bool)

ForEach 遍历所有未过期的缓存项并对每一项执行指定的函数

func (*Cache[K, V]) ForEachList

func (c *Cache[K, V]) ForEachList(fn func(key K, list []V) bool)

ForEachList 遍历所有未过期的列表

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(key K) (V, bool)

Get 获取缓存项

func (*Cache[K, V]) GetListTTL

func (c *Cache[K, V]) GetListTTL(key K) (time.Duration, bool)

GetListTTL 获取列表键的剩余生存时间

func (*Cache[K, V]) GetStats

func (c *Cache[K, V]) GetStats() Stats

GetStats 获取缓存统计信息

func (*Cache[K, V]) GetTTL

func (c *Cache[K, V]) GetTTL(key K) (time.Duration, bool)

GetTTL 获取键的剩余生存时间

func (*Cache[K, V]) GetWithTTL

func (c *Cache[K, V]) GetWithTTL(key K) (V, time.Duration, bool)

GetWithTTL 获取缓存项及其剩余生存时间

func (*Cache[K, V]) Has

func (c *Cache[K, V]) Has(key K) bool

Has 检查键是否存在且未过期

func (*Cache[K, V]) HasList

func (c *Cache[K, V]) HasList(key K) bool

HasList 检查列表键是否存在且未过期

func (*Cache[K, V]) Increment

func (c *Cache[K, V]) Increment(key K, increment any) (any, error)

Increment 对数值类型进行增加操作

func (*Cache[K, V]) Keys

func (c *Cache[K, V]) Keys() []K

Keys 返回所有的普通缓存键

func (*Cache[K, V]) LIndex

func (c *Cache[K, V]) LIndex(key K, index int) (V, bool)

LIndex 通过索引获取列表中的元素

func (*Cache[K, V]) LInsert

func (c *Cache[K, V]) LInsert(key K, before bool, pivot V, value V, equals func(a, b V) bool) int

LInsert 在列表的指定位置插入元素 before == true表示在pivot之前插入 before == false表示在pivot之后插入

func (*Cache[K, V]) LLen

func (c *Cache[K, V]) LLen(key K) int

LLen 获取列表长度

func (*Cache[K, V]) LPop

func (c *Cache[K, V]) LPop(key K) (V, bool)

LPop 移除并返回列表头部的元素

func (*Cache[K, V]) LPush

func (c *Cache[K, V]) LPush(key K, values ...V) int

LPush 将一个或多个值插入到列表头部

func (*Cache[K, V]) LRange

func (c *Cache[K, V]) LRange(key K, start, stop int) []V

LRange 获取列表指定范围内的元素

func (*Cache[K, V]) LRem

func (c *Cache[K, V]) LRem(key K, count int, value V, equals func(a, b V) bool) int

LRem 移除列表中与参数value相等的元素 count > 0: 从头往尾移除count个值为value的元素 count < 0: 从尾往头移除count个值为value的元素 count = 0: 移除所有值为value的元素

func (*Cache[K, V]) LSet

func (c *Cache[K, V]) LSet(key K, index int, value V) bool

LSet 通过索引来设置列表元素的值

func (*Cache[K, V]) LTrim

func (c *Cache[K, V]) LTrim(key K, start, stop int) bool

LTrim 对一个列表进行修剪,只保留指定区间内的元素

func (*Cache[K, V]) ListCount

func (c *Cache[K, V]) ListCount() int

ListCount 返回列表数量

func (*Cache[K, V]) ListKeys

func (c *Cache[K, V]) ListKeys() []K

ListKeys 返回所有的列表键

func (*Cache[K, V]) Load

func (c *Cache[K, V]) Load() error

Load 从文件加载缓存

func (*Cache[K, V]) LoadOrInit

func (c *Cache[K, V]) LoadOrInit() error

LoadOrInit 从文件加载缓存,如果文件不存在则初始化一个新的缓存

func (*Cache[K, V]) RPop

func (c *Cache[K, V]) RPop(key K) (V, bool)

RPop 移除并返回列表尾部的元素

func (*Cache[K, V]) RPoplPush

func (c *Cache[K, V]) RPoplPush(source K, destination K) (V, bool)

RPoplPush 移除列表的最后一个元素,并将该元素添加到另一个列表的头部并返回它

func (*Cache[K, V]) RPush

func (c *Cache[K, V]) RPush(key K, values ...V) int

RPush 将一个或多个值插入到列表尾部

func (*Cache[K, V]) Save

func (c *Cache[K, V]) Save() error

Save 保存缓存到文件

func (*Cache[K, V]) Set

func (c *Cache[K, V]) Set(key K, value V, duration ...time.Duration)

Set 设置缓存项,可选过期时间。不传递过期时间时使用默认过期时间

func (*Cache[K, V]) SetList

func (c *Cache[K, V]) SetList(key K, duration ...time.Duration)

SetList 设置列表的过期时间,不传递过期时间时使用默认过期时间

func (*Cache[K, V]) WithPersistence

func (c *Cache[K, V]) WithPersistence(persistPath string, autoPersistInterval time.Duration) *Cache[K, V]

WithPersistence 配置持久化选项

type CacheItem

type CacheItem[T any] struct {
	Value      T
	Expiration int64 // Unix时间戳,表示过期时间
}

CacheItem 表示缓存中的一个项目

type ListItem

type ListItem[T any] struct {
	Value T
}

ListItem 表示列表中的一个项目

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 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]) Commit

func (t *Transaction[K, V]) Commit()

Commit 提交事务

func (*Transaction[K, V]) Delete

func (t *Transaction[K, V]) Delete(key K)

Delete 在事务中删除缓存项

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 在事务中设置列表过期时间

Jump to

Keyboard shortcuts

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