Documentation
¶
Index ¶
- Constants
- type Cache
- func (c *Cache) ClearAll() error
- func (c *Cache) Del(key string) error
- func (c *Cache) Exists(key string) (bool, error)
- func (c *Cache) GetKeys(pattern string) ([]string, error)
- func (c *Cache) GetStats() (map[string]interface{}, error)
- func (c *Cache) GetString(key string, defaultValue ...string) (string, error)
- func (c *Cache) GetStruct(key string, value interface{}, defaultValue ...interface{}) error
- func (c *Cache) Set(key string, value interface{}, expire ...time.Duration) error
- type CacheOption
- type Client
- type Instance
- func (i *Instance) Decr(key string) (int64, error)
- func (i *Instance) Del(key ...string) error
- func (i *Instance) Eval(script string, keys []string, args ...interface{}) (interface{}, error)
- func (i *Instance) Exists(key string) (bool, error)
- func (i *Instance) Expire(key string, expiration time.Duration) (bool, error)
- func (i *Instance) GetClient() *redis.Client
- func (i *Instance) GetString(key string, args ...string) (string, error)
- func (i *Instance) GetStruct(key string, value interface{}, args ...interface{}) error
- func (i *Instance) HExists(key, field string) (bool, error)
- func (i *Instance) HGet(key, field string) (string, error)
- func (i *Instance) HGetAll(key string) (map[string]string, error)
- func (i *Instance) HSet(key, field string, value interface{}) error
- func (i *Instance) Incr(key string) (int64, error)
- func (i *Instance) Info() (string, error)
- func (i *Instance) Keys(pattern string) ([]string, error)
- func (i *Instance) LLen(key string) (int64, error)
- func (i *Instance) LPop(key string) (string, error)
- func (i *Instance) LPush(key string, values ...interface{}) error
- func (i *Instance) LRange(key string, start, stop int64) ([]string, error)
- func (i *Instance) Ping() (string, error)
- func (i *Instance) Publish(channel string, message interface{}) error
- func (i *Instance) RPop(key string) (string, error)
- func (i *Instance) RPush(key string, values ...interface{}) error
- func (i *Instance) SAdd(key string, members ...interface{}) (int64, error)
- func (i *Instance) SIsMember(key string, member interface{}) (bool, error)
- func (i *Instance) SMembers(key string) ([]string, error)
- func (i *Instance) SRem(key string, members ...interface{}) (int64, error)
- func (i *Instance) Set(key string, value interface{}, args ...time.Duration) error
- func (i *Instance) Subscribe(channels ...string) (*redis.PubSub, error)
- func (i *Instance) TTL(key string) (time.Duration, error)
- type StatusCmd
- type StringCmd
Constants ¶
View Source
const Nil = redis.Nil
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶ added in v0.10.2
type Cache struct {
Prefix string `json:"prefix" default:"jcbase"`
Expire time.Duration `json:"expire" default:"0"`
// contains filtered or unexported fields
}
Cache 缓存管理器结构体 提供基于Redis的缓存操作,支持键前缀、过期时间等配置
func NewCache ¶
func NewCache(redis *Instance, opts ...CacheOption) (*Cache, error)
NewCache 创建一个新的缓存管理器实例 参数:
- redis: Redis实例,不能为空
- opts: 可选的配置选项
返回:
- *Cache: 缓存管理器实例
- error: 创建失败时的错误信息
func (*Cache) Exists ¶ added in v0.10.2
Exists 检查缓存键是否存在 参数:
- key: 要检查的缓存键
返回:
- bool: 键存在返回true,否则返回false
- error: 检查失败时的错误信息
func (*Cache) GetKeys ¶ added in v0.10.2
GetKeys 获取所有匹配模式的缓存键 参数:
- pattern: 键模式,支持通配符
返回:
- []string: 匹配的键列表
- error: 获取失败时的错误信息
func (*Cache) GetStats ¶ added in v0.10.2
GetStats 获取缓存统计信息 返回:
- map[string]interface{}: 统计信息
- error: 获取失败时的错误信息
func (*Cache) GetString ¶ added in v0.10.2
GetString 获取字符串类型的缓存值 参数:
- key: 缓存键
- defaultValue: 可选的默认值
返回:
- string: 缓存值或默认值
- error: 获取失败时的错误信息
type CacheOption ¶ added in v0.10.2
type CacheOption func(*Cache)
CacheOption 缓存配置选项函数
func WithExpire ¶ added in v0.10.2
func WithExpire(expire time.Duration) CacheOption
WithExpire 设置默认过期时间
type Instance ¶
func (*Instance) Del ¶
Del 删除键值。
参数:
- key (必需): 要删除的键值。
返回值:
- error: 如果发生错误则返回相应的错误信息。
示例:
err := Del(key)
if err != nil {
// 处理错误
}
func (*Instance) Exists ¶
Exists 检查键值是否存在。
参数:
- key (必需): 要检查的键值。
返回值:
- bool: 如果键值存在则返回 true,否则返回 false。
- error: 如果发生错误则返回相应的错误信息。
示例:
exists, err := Exists(key)
if err != nil {
// 处理错误
}
func (*Instance) GetString ¶
GetString 根据键值,返回字符串值。 如果键不存在或发生错误,则返回默认值(如果提供)。
参数:
- key (必需): 用于查找数据的字符串键值。
- defaultValue (可选): 可选的默认值(字符串类型)。
返回值:
- value: 获取到的字符串值,或者是默认值。
- err: 如果发生错误则返回相应的错误信息。
示例:
value, err := GetString(key)
if err != nil {
// 处理错误
} else {
// 使用 value
}
value, err := GetString(key, "default")
if err != nil {
// 处理错误
} else {
// 使用 value
}
func (*Instance) GetStruct ¶
GetStruct 根据键值获取自定义结构体类型或 map 类型。 args 参数列表,支持以下格式:
- 提供 key 和 value(指向目标结构体的指针或 map 的指针):GetStruct(key, &value)
- 提供 key, value 和 defaultValue(默认值的任意类型):GetStruct(key, &value, defaultValue)
参数:
- key (必需): 用于查找数据的字符串键值。
- value (必需): 指向目标结构体的指针或 map 的指针,用于存储获取到的数据。
- defaultValue (可选): 当获取数据失败时的默认值。
返回值:
- error: 如果发生错误则返回相应的错误信息。
示例:
var value MyStruct
defaultValue := MyStruct{Field: "default"}
err := GetStruct(key, &value, defaultValue)
if err != nil {
// 处理错误
} else {
// 使用 value
}
func (*Instance) Keys ¶
Keys 获取键值列表。
参数:
- pattern (必需): 匹配的键值模式。
返回值:
- []string: 匹配到的键值列表。
- error: 如果发生错误则返回相应的错误信息。
示例:
keys, err := Keys(pattern)
if err != nil {
// 处理错误
}
func (*Instance) Set ¶
Set 设置键值。
参数:
- key (必需): 要设置的键值。
- value (必需): 要设置的值,将被转换为 JSON 格式保存。
- expire (可选): 数据的过期时间,如果未设置则数据将永不过期。
返回值:
- error: 如果发生错误则返回相应的错误信息。
示例:
err := Set(key, value, time.Hour)
if err != nil {
// 处理错误
}
Click to show internal directories.
Click to hide internal directories.