cache

package
v4.0.0-alpha.7 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxSize = 10000

DefaultMaxSize 默认最大缓存条目数

Variables

View Source
var ErrKeyNotFound = errors.New("key not found")

ErrKeyNotFound 键不存在错误

Functions

func Clear

func Clear()

Clear 清空所有缓存

func CloseMemory

func CloseMemory()

CloseMemory 关闭内存缓存

func Decr

func Decr(key string) int64

Decr 自减计数器

func Del

func Del(keys ...string) error

Del 删除缓存

func Delete

func Delete(key string) error

Delete 删除缓存(别名)

func Exists

func Exists(key string) bool

Exists 检查键是否存在

func Expire

func Expire(key string, expiration time.Duration) error

Expire 设置过期时间

func Get

func Get(key string) (any, error)

Get 获取缓存值

func GetInt

func GetInt(key string) (int64, error)

GetInt 获取整数

func GetString

func GetString(key string) (string, error)

GetString 获取字符串缓存

func HDel

func HDel(key string, fields ...string) error

HDel 删除Hash字段

func HGet

func HGet(key string, field string) (string, error)

HGet 获取Hash字段

func HGetAll

func HGetAll(key string) (map[string]string, error)

HGetAll 获取所有Hash字段

func HSet

func HSet(key string, field string, value any) error

HSet 设置Hash字段

func Incr

func Incr(key string) int64

Incr 自增计数器

func IncrBy

func IncrBy(key string, delta int64) int64

IncrBy 自增指定值

func InitMemory

func InitMemory()

InitMemory 初始化内存缓存

func InitMemoryWithSize

func InitMemoryWithSize(maxSize int)

InitMemoryWithSize 初始化指定最大容量的内存缓存

func IsMemoryCache

func IsMemoryCache() bool

IsMemoryCache 是否使用内存缓存

func IsRedisCache

func IsRedisCache() bool

IsRedisCache 是否使用Redis缓存

func Keys

func Keys(pattern string) ([]string, error)

Keys 获取匹配的键(支持简单的 * 通配符)

func Len

func Len() int

Len 返回当前缓存条目数(用于测试和监控)

func SAdd

func SAdd(key string, members ...any) error

SAdd 集合添加

func SCard

func SCard(key string) (int64, error)

SCard 获取集合大小

func SClear

func SClear(key string) error

SClear 清空集合

func SMembers

func SMembers(key string) ([]string, error)

SMembers 获取集合成员

func Set

func Set(key string, value any, expiration time.Duration) error

Set 设置缓存(保持兼容原有签名)

func SetInt

func SetInt(key string, value int64, expiration time.Duration) error

SetInt 设置整数

func StartCleanup

func StartCleanup()

StartCleanup 启动定期清理(如果尚未启动)

func Stop

func Stop()

Stop 停止定期清理

Types

type CacheType

type CacheType string

CacheType 缓存类型

const (
	CacheTypeMemory CacheType = "memory"
	CacheTypeRedis  CacheType = "redis"
)

func GetCacheType

func GetCacheType() CacheType

GetCacheType 获取当前缓存类型

type MemoryCache

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

MemoryCache 内存缓存实现(支持 TTL、LRU 淘汰、定期清理)

Jump to

Keyboard shortcuts

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