readonlyx.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package redisx
  2. import (
  3. "context"
  4. "time"
  5. "github.com/redis/go-redis/v9"
  6. )
  7. // 只读命令补充封装(供 MCP 只读工具使用;均不改动数据)。
  8. // 已有的只读封装分散在 stringx/hashx/listx/setx/zsetx/streamx/helper 中,
  9. // 这里补齐缺失部分,避免 MCP 直接触达底层 client。
  10. /********* 通用 *********/
  11. // Type 返回 key 的数据类型(string/hash/list/set/zset/stream/none)。
  12. func Type(ctx context.Context, key string) (string, error) {
  13. return getClient().Type(ctx, key).Result()
  14. }
  15. // Scan 非阻塞游标遍历 key。match 为空表示不过滤;count 为提示值。
  16. // 返回本批 keys 与下一个游标(游标为 0 表示遍历结束)。
  17. func Scan(ctx context.Context, cursor uint64, match string, count int64) ([]string, uint64, error) {
  18. return getClient().Scan(ctx, cursor, match, count).Result()
  19. }
  20. // Keys 按 pattern 返回匹配的 key(阻塞命令,大库慎用,建议优先用 Scan)。
  21. func Keys(ctx context.Context, pattern string) ([]string, error) {
  22. return getClient().Keys(ctx, pattern).Result()
  23. }
  24. // PTTL 返回 key 剩余存活时间(毫秒精度的 time.Duration)。
  25. func PTTL(ctx context.Context, key string) (time.Duration, error) {
  26. return getClient().PTTL(ctx, key).Result()
  27. }
  28. // MemoryUsage 返回 key 占用的内存字节数(估算)。
  29. func MemoryUsage(ctx context.Context, key string) (int64, error) {
  30. return getClient().MemoryUsage(ctx, key).Result()
  31. }
  32. /********* 字符串 *********/
  33. // StrLen 返回 string 类型 value 的长度。
  34. func StrLen(ctx context.Context, key string) (int64, error) {
  35. return getClient().StrLen(ctx, key).Result()
  36. }
  37. /********* 哈希 *********/
  38. // HKeys 返回 hash 的所有 field 名。
  39. func HKeys(ctx context.Context, key string) ([]string, error) {
  40. return getClient().HKeys(ctx, key).Result()
  41. }
  42. // HLen 返回 hash 的 field 数量。
  43. func HLen(ctx context.Context, key string) (int64, error) {
  44. return getClient().HLen(ctx, key).Result()
  45. }
  46. // HExists 判断 hash field 是否存在。
  47. func HExists(ctx context.Context, key, field string) (bool, error) {
  48. return getClient().HExists(ctx, key, field).Result()
  49. }
  50. // HMGet 批量读取 hash 的多个 field。
  51. func HMGet(ctx context.Context, key string, fields ...string) ([]interface{}, error) {
  52. return getClient().HMGet(ctx, key, fields...).Result()
  53. }
  54. /********* 列表 *********/
  55. // LIndex 返回 list 指定下标的元素。
  56. func LIndex(ctx context.Context, key string, index int64) (string, error) {
  57. return getClient().LIndex(ctx, key, index).Result()
  58. }
  59. /********* 集合 *********/
  60. // SCard 返回 set 的元素数量。
  61. func SCard(ctx context.Context, key string) (int64, error) {
  62. return getClient().SCard(ctx, key).Result()
  63. }
  64. /********* 有序集合 *********/
  65. // ZCard 返回 zset 的成员数量。
  66. func ZCard(ctx context.Context, key string) (int64, error) {
  67. return getClient().ZCard(ctx, key).Result()
  68. }
  69. // ZScore 返回 zset 中成员的分数。
  70. func ZScore(ctx context.Context, key, member string) (float64, error) {
  71. return getClient().ZScore(ctx, key, member).Result()
  72. }
  73. // ZRangeWithScores 返回 zset 指定区间的成员及分数(按分数升序)。
  74. func ZRangeWithScores(ctx context.Context, key string, start, stop int64) ([]redis.Z, error) {
  75. return getClient().ZRangeWithScores(ctx, key, start, stop).Result()
  76. }
  77. // ZRevRangeWithScores 返回 zset 指定区间的成员及分数(按分数降序)。
  78. func ZRevRangeWithScores(ctx context.Context, key string, start, stop int64) ([]redis.Z, error) {
  79. return getClient().ZRevRangeWithScores(ctx, key, start, stop).Result()
  80. }
  81. /********* Stream *********/
  82. // XLen 返回 stream 的消息条数。
  83. func XLen(ctx context.Context, key string) (int64, error) {
  84. return getClient().XLen(ctx, key).Result()
  85. }
  86. // XInfoGroups 返回 stream 的消费者组信息。
  87. func XInfoGroups(ctx context.Context, key string) ([]redis.XInfoGroup, error) {
  88. return getClient().XInfoGroups(ctx, key).Result()
  89. }