Documentation
¶
Index ¶
- Variables
- type Cache
- func (c *Cache) Close() error
- func (c *Cache) Del(key string) error
- func (c *Cache) Exists(key string) bool
- func (c *Cache) Get(key string) ([]byte, error)
- func (c *Cache) GetS(key string) (string, error)
- func (c *Cache) Set(key string, value []byte) error
- func (c *Cache) SetS(key string, value string) error
- func (c *Cache) XDecr(key string) (int64, error)
- func (c *Cache) XDecrBy(key string, decrement int64) (int64, error)
- func (c *Cache) XExpire(key string, expires time.Duration) error
- func (c *Cache) XExpireAt(key string, tm time.Time) error
- func (c *Cache) XExpireSec(key string, seconds int64) error
- func (c *Cache) XGet(key string) ([]byte, error)
- func (c *Cache) XGetS(key string) (string, error)
- func (c *Cache) XIncr(key string) (int64, error)
- func (c *Cache) XIncrBy(key string, increment int64) (int64, error)
- func (c *Cache) XSet(key string, value []byte) error
- func (c *Cache) XSetEx(key string, value []byte, expires time.Duration) error
- func (c *Cache) XSetExS(key string, value string, expires time.Duration) error
- func (c *Cache) XSetExSec(key string, value []byte, seconds int64) error
- func (c *Cache) XSetExSecS(key string, value string, seconds int64) error
- func (c *Cache) XSetS(key string, value string) error
- func (c *Cache) XTTL(key string) (int64, error)
- type CacheType
- type Option
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache 结构体封装了对 bigcache 的操作
func NewCache ¶
NewCache 返回一个新的 Cache 实例。 minute 参数指定默认的缓存生命周期(分钟) opts 可选参数用于自定义缓存配置
Example:
// 创建一个默认10分钟过期的缓存 cache, err := NewCache(10) // 创建一个自定义配置的缓存 cache, err := NewCache(10, WithLifeWindow(5*time.Minute), WithCleanWindow(1*time.Minute), )
func (*Cache) Close ¶ added in v0.0.2
Close 关闭缓存并释放资源 在程序结束时调用此方法以确保资源被正确释放
Example:
cache, err := NewCache(10) if err != nil { // 处理错误 } defer cache.Close()
func (*Cache) XDecr ¶ added in v0.2.0
XDecr 将键存储的数字值减1
Example:
newVal, err := cache.XDecr("counter")
func (*Cache) XDecrBy ¶ added in v0.2.0
XDecrBy 将键存储的数字值减少指定的减量
Example:
// 减少5 newVal, err := cache.XDecrBy("counter", 5)
func (*Cache) XExpire ¶ added in v0.2.0
XExpire 设置键的过期时间(Duration类型)
Example:
// 设置5分钟后过期 err := cache.XExpire("key", 5*time.Minute)
func (*Cache) XExpireAt ¶ added in v0.2.0
XExpireAt 设置键在指定时间点过期
Example:
// 设置在1小时后过期 err := cache.XExpireAt("key", time.Now().Add(time.Hour)) // 设置在明天零点过期 tomorrow := time.Now().Add(24*time.Hour) expireTime := time.Date( tomorrow.Year(), tomorrow.Month(), tomorrow.Day(), 0, 0, 0, 0, tomorrow.Location(), ) err := cache.XExpireAt("key", expireTime)
func (*Cache) XExpireSec ¶ added in v0.2.0
XExpireSec 设置键的过期时间(秒数)
func (*Cache) XGet ¶ added in v0.2.0
XGet 获取 gob 序列化存储的数据 如果数据已过期,会自动删除并返回 ErrKeyNotFound
Example:
data, err := cache.XGet("key") if err == ErrKeyNotFound { // 处理键不存在的情况 }
func (*Cache) XGetS ¶ added in v0.2.0
XGetS 获取 gob 序列化存储的字符串数据
Example:
str, err := cache.XGetS("key") if err != nil { // 处理错误 }
func (*Cache) XIncr ¶ added in v0.2.0
XIncr 将键存储的数字值加1 如果键不存在,会创建并设置值为1
Example:
newVal, err := cache.XIncr("counter") // newVal 是增加后的新值
func (*Cache) XIncrBy ¶ added in v0.2.0
XIncrBy 将键存储的数字值增加指定的增量
Example:
// 增加5 newVal, err := cache.XIncrBy("counter", 5) // 减少3(通过负数实现) newVal, err := cache.XIncrBy("counter", -3)
func (*Cache) XSet ¶ added in v0.2.0
XSet 使用 gob 序列化存储数据,数据永不过期
Example:
err := cache.XSet("key", []byte("value"))
func (*Cache) XSetEx ¶ added in v0.2.0
XSetEx 使用 Duration 类型设置过期时间存储数据
Example:
// 存储10分钟后过期的数据 err := cache.XSetEx("key", []byte("value"), 10*time.Minute)
func (*Cache) XSetExS ¶ added in v0.2.0
XSetExS 使用 Duration 类型设置过期时间存储字符串数据
Example:
// 存储1小时后过期的字符串 err := cache.XSetExS("key", "value", time.Hour)
func (*Cache) XSetExSecS ¶ added in v0.2.0
XSetExSecS 使用秒数设置过期时间存储字符串数据
func (*Cache) XSetS ¶ added in v0.2.0
XSetS 使用 gob 序列化存储字符串数据,数据永不过期
Example:
err := cache.XSetS("key", "value")
type Option ¶ added in v0.0.3
func WithCleanWindow ¶ added in v0.0.3
WithCleanWindow 设置缓存的清理频率。 在此时间段内,过期的条目将从缓存中删除。 这有助于保持最佳性能和内存使用,确保过期条目不会滞留在缓存中。
func WithContext ¶ added in v0.0.3
WithContext 允许为 BigCache 实例设置自定义上下文。 该上下文可用于控制缓存的生命周期,允许进行取消和超时管理。
func WithLifeWindow ¶ added in v0.0.3
WithLifeWindow 设置缓存中条目的有效期。 超过此时间后,条目将自动从缓存中删除。 这有助于管理内存使用,并确保不会提供过时的数据。