redis

package
v0.10.5 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2025 License: MIT Imports: 11 Imported by: 1

Documentation

Index

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) ClearAll added in v0.10.2

func (c *Cache) ClearAll() error

ClearAll 清除所有当前前缀的缓存 返回:

  • error: 清除失败时的错误信息

func (*Cache) Del added in v0.10.2

func (c *Cache) Del(key string) error

Del 删除缓存键 参数:

  • key: 要删除的缓存键

返回:

  • error: 删除失败时的错误信息

func (*Cache) Exists added in v0.10.2

func (c *Cache) Exists(key string) (bool, error)

Exists 检查缓存键是否存在 参数:

  • key: 要检查的缓存键

返回:

  • bool: 键存在返回true,否则返回false
  • error: 检查失败时的错误信息

func (*Cache) GetKeys added in v0.10.2

func (c *Cache) GetKeys(pattern string) ([]string, error)

GetKeys 获取所有匹配模式的缓存键 参数:

  • pattern: 键模式,支持通配符

返回:

  • []string: 匹配的键列表
  • error: 获取失败时的错误信息

func (*Cache) GetStats added in v0.10.2

func (c *Cache) GetStats() (map[string]interface{}, error)

GetStats 获取缓存统计信息 返回:

  • map[string]interface{}: 统计信息
  • error: 获取失败时的错误信息

func (*Cache) GetString added in v0.10.2

func (c *Cache) GetString(key string, defaultValue ...string) (string, error)

GetString 获取字符串类型的缓存值 参数:

  • key: 缓存键
  • defaultValue: 可选的默认值

返回:

  • string: 缓存值或默认值
  • error: 获取失败时的错误信息

func (*Cache) GetStruct added in v0.10.2

func (c *Cache) GetStruct(key string, value interface{}, defaultValue ...interface{}) error

GetStruct 获取结构体类型的缓存值 参数:

  • key: 缓存键
  • value: 指向目标结构体的指针
  • defaultValue: 可选的默认值

返回:

  • error: 获取失败时的错误信息

func (*Cache) Set added in v0.10.2

func (c *Cache) Set(key string, value interface{}, expire ...time.Duration) error

Set 设置缓存键值 参数:

  • key: 缓存键
  • value: 缓存值,会被序列化为JSON
  • expire: 过期时间,如果为0则使用默认过期时间

返回:

  • 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 设置默认过期时间

func WithPrefix added in v0.10.2

func WithPrefix(prefix string) CacheOption

WithPrefix 设置缓存键前缀

type Client

type Client = redis.Client

type Instance

type Instance struct {
	Context context.Context
	Conf    jcbaseGo.RedisStruct
	Client  *redis.Client
}

func New

func New(conf jcbaseGo.RedisStruct) *Instance

New 获取新的redis连接

func (*Instance) Decr

func (i *Instance) Decr(key string) (int64, error)

Decr 减少计数器的值。

func (*Instance) Del

func (i *Instance) Del(key ...string) error

Del 删除键值。

参数:

  • key (必需): 要删除的键值。

返回值:

  • error: 如果发生错误则返回相应的错误信息。

示例:

err := Del(key)
if err != nil {
    // 处理错误
}

func (*Instance) Eval

func (i *Instance) Eval(script string, keys []string, args ...interface{}) (interface{}, error)

Eval 执行 Lua 脚本。

func (*Instance) Exists

func (i *Instance) Exists(key string) (bool, error)

Exists 检查键值是否存在。

参数:

  • key (必需): 要检查的键值。

返回值:

  • bool: 如果键值存在则返回 true,否则返回 false。
  • error: 如果发生错误则返回相应的错误信息。

示例:

exists, err := Exists(key)
if err != nil {
    // 处理错误
}

func (*Instance) Expire

func (i *Instance) Expire(key string, expiration time.Duration) (bool, error)

Expire 设置键的过期时间。

func (*Instance) GetClient

func (i *Instance) GetClient() *redis.Client

GetClient 获取redis client

func (*Instance) GetString

func (i *Instance) GetString(key string, args ...string) (string, error)

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

func (i *Instance) GetStruct(key string, value interface{}, args ...interface{}) error

GetStruct 根据键值获取自定义结构体类型或 map 类型。 args 参数列表,支持以下格式:

  1. 提供 key 和 value(指向目标结构体的指针或 map 的指针):GetStruct(key, &value)
  2. 提供 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) HExists

func (i *Instance) HExists(key, field string) (bool, error)

HExists 检查哈希表中是否存在指定字段。

func (*Instance) HGet

func (i *Instance) HGet(key, field string) (string, error)

HGet 获取哈希表中指定字段的值。

func (*Instance) HGetAll

func (i *Instance) HGetAll(key string) (map[string]string, error)

HGetAll 获取哈希表中所有字段和值。

func (*Instance) HSet

func (i *Instance) HSet(key, field string, value interface{}) error

HSet 设置哈希表中的字段值。

func (*Instance) Incr

func (i *Instance) Incr(key string) (int64, error)

Incr 增加计数器的值。

func (*Instance) Info

func (i *Instance) Info() (string, error)

Info 获取 Redis 服务器的信息。

func (*Instance) Keys

func (i *Instance) Keys(pattern string) ([]string, error)

Keys 获取键值列表。

参数:

  • pattern (必需): 匹配的键值模式。

返回值:

  • []string: 匹配到的键值列表。
  • error: 如果发生错误则返回相应的错误信息。

示例:

keys, err := Keys(pattern)
if err != nil {
    // 处理错误
}

func (*Instance) LLen

func (i *Instance) LLen(key string) (int64, error)

LLen 获取列表的长度。

func (*Instance) LPop

func (i *Instance) LPop(key string) (string, error)

LPop 从列表左端弹出一个元素。

func (*Instance) LPush

func (i *Instance) LPush(key string, values ...interface{}) error

LPush 向列表左端插入一个或多个元素。

func (*Instance) LRange

func (i *Instance) LRange(key string, start, stop int64) ([]string, error)

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

func (*Instance) Ping

func (i *Instance) Ping() (string, error)

Ping 检查与 Redis 服务器的连接是否正常。

func (*Instance) Publish

func (i *Instance) Publish(channel string, message interface{}) error

Publish 向指定频道发布消息。

func (*Instance) RPop

func (i *Instance) RPop(key string) (string, error)

RPop 从列表右端弹出一个元素。

func (*Instance) RPush

func (i *Instance) RPush(key string, values ...interface{}) error

RPush 向列表右端插入一个或多个元素。

func (*Instance) SAdd

func (i *Instance) SAdd(key string, members ...interface{}) (int64, error)

SAdd 向集合添加一个或多个成员。

func (*Instance) SIsMember

func (i *Instance) SIsMember(key string, member interface{}) (bool, error)

SIsMember 检查成员是否存在于集合中。

func (*Instance) SMembers

func (i *Instance) SMembers(key string) ([]string, error)

SMembers 获取集合中的所有成员。

func (*Instance) SRem

func (i *Instance) SRem(key string, members ...interface{}) (int64, error)

SRem 从集合中移除一个或多个成员。

func (*Instance) Set

func (i *Instance) Set(key string, value interface{}, args ...time.Duration) error

Set 设置键值。

参数:

  • key (必需): 要设置的键值。
  • value (必需): 要设置的值,将被转换为 JSON 格式保存。
  • expire (可选): 数据的过期时间,如果未设置则数据将永不过期。

返回值:

  • error: 如果发生错误则返回相应的错误信息。

示例:

err := Set(key, value, time.Hour)
if err != nil {
    // 处理错误
}

func (*Instance) Subscribe

func (i *Instance) Subscribe(channels ...string) (*redis.PubSub, error)

Subscribe 订阅指定频道接收消息。

func (*Instance) TTL

func (i *Instance) TTL(key string) (time.Duration, error)

TTL 获取键的剩余过期时间。

type StatusCmd

type StatusCmd = redis.StatusCmd

type StringCmd

type StringCmd = redis.StringCmd

Jump to

Keyboard shortcuts

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