package redisx import ( "context" "time" "github.com/redis/go-redis/v9" ) // 只读命令补充封装(供 MCP 只读工具使用;均不改动数据)。 // 已有的只读封装分散在 stringx/hashx/listx/setx/zsetx/streamx/helper 中, // 这里补齐缺失部分,避免 MCP 直接触达底层 client。 /********* 通用 *********/ // Type 返回 key 的数据类型(string/hash/list/set/zset/stream/none)。 func Type(ctx context.Context, key string) (string, error) { return getClient().Type(ctx, key).Result() } // Scan 非阻塞游标遍历 key。match 为空表示不过滤;count 为提示值。 // 返回本批 keys 与下一个游标(游标为 0 表示遍历结束)。 func Scan(ctx context.Context, cursor uint64, match string, count int64) ([]string, uint64, error) { return getClient().Scan(ctx, cursor, match, count).Result() } // Keys 按 pattern 返回匹配的 key(阻塞命令,大库慎用,建议优先用 Scan)。 func Keys(ctx context.Context, pattern string) ([]string, error) { return getClient().Keys(ctx, pattern).Result() } // PTTL 返回 key 剩余存活时间(毫秒精度的 time.Duration)。 func PTTL(ctx context.Context, key string) (time.Duration, error) { return getClient().PTTL(ctx, key).Result() } // MemoryUsage 返回 key 占用的内存字节数(估算)。 func MemoryUsage(ctx context.Context, key string) (int64, error) { return getClient().MemoryUsage(ctx, key).Result() } /********* 字符串 *********/ // StrLen 返回 string 类型 value 的长度。 func StrLen(ctx context.Context, key string) (int64, error) { return getClient().StrLen(ctx, key).Result() } /********* 哈希 *********/ // HKeys 返回 hash 的所有 field 名。 func HKeys(ctx context.Context, key string) ([]string, error) { return getClient().HKeys(ctx, key).Result() } // HLen 返回 hash 的 field 数量。 func HLen(ctx context.Context, key string) (int64, error) { return getClient().HLen(ctx, key).Result() } // HExists 判断 hash field 是否存在。 func HExists(ctx context.Context, key, field string) (bool, error) { return getClient().HExists(ctx, key, field).Result() } // HMGet 批量读取 hash 的多个 field。 func HMGet(ctx context.Context, key string, fields ...string) ([]interface{}, error) { return getClient().HMGet(ctx, key, fields...).Result() } /********* 列表 *********/ // LIndex 返回 list 指定下标的元素。 func LIndex(ctx context.Context, key string, index int64) (string, error) { return getClient().LIndex(ctx, key, index).Result() } /********* 集合 *********/ // SCard 返回 set 的元素数量。 func SCard(ctx context.Context, key string) (int64, error) { return getClient().SCard(ctx, key).Result() } /********* 有序集合 *********/ // ZCard 返回 zset 的成员数量。 func ZCard(ctx context.Context, key string) (int64, error) { return getClient().ZCard(ctx, key).Result() } // ZScore 返回 zset 中成员的分数。 func ZScore(ctx context.Context, key, member string) (float64, error) { return getClient().ZScore(ctx, key, member).Result() } // ZRangeWithScores 返回 zset 指定区间的成员及分数(按分数升序)。 func ZRangeWithScores(ctx context.Context, key string, start, stop int64) ([]redis.Z, error) { return getClient().ZRangeWithScores(ctx, key, start, stop).Result() } // ZRevRangeWithScores 返回 zset 指定区间的成员及分数(按分数降序)。 func ZRevRangeWithScores(ctx context.Context, key string, start, stop int64) ([]redis.Z, error) { return getClient().ZRevRangeWithScores(ctx, key, start, stop).Result() } /********* Stream *********/ // XLen 返回 stream 的消息条数。 func XLen(ctx context.Context, key string) (int64, error) { return getClient().XLen(ctx, key).Result() } // XInfoGroups 返回 stream 的消费者组信息。 func XInfoGroups(ctx context.Context, key string) ([]redis.XInfoGroup, error) { return getClient().XInfoGroups(ctx, key).Result() }