cache.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package cache
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "time"
  7. "fwh-toolbox-server/cache/redisx"
  8. "github.com/redis/go-redis/v9"
  9. )
  10. func Init(addr string) {
  11. opt := redisx.Config{
  12. Addrs: []string{addr},
  13. DB: 12,
  14. }
  15. redisx.Init(&opt)
  16. }
  17. // ErrNotFound 用于表示缓存未命中
  18. var ErrNotFound = errors.New("cachex: not found")
  19. // helper: compute full key with prefix
  20. func fullKey(prefix, key string) string {
  21. if prefix == "" {
  22. return key
  23. }
  24. return fmt.Sprintf("%s:%s", prefix, key)
  25. }
  26. // RegisterHitHook / RegisterMissHook: 简单 hook,用户可利用来统计
  27. var (
  28. hitHook func(key string)
  29. missHook func(key string)
  30. )
  31. func RegisterHitHook(fn func(key string)) {
  32. hitHook = fn
  33. }
  34. func RegisterMissHook(fn func(key string)) {
  35. missHook = fn
  36. }
  37. // internal helpers to call hooks safely
  38. func onHit(key string) {
  39. if hitHook != nil {
  40. go func() { defer func() { recover() }(); hitHook(key) }()
  41. }
  42. }
  43. func onMiss(key string) {
  44. if missHook != nil {
  45. go func() { defer func() { recover() }(); missHook(key) }()
  46. }
  47. }
  48. /********* 字符串操作 *********/
  49. // Set string value
  50. func Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error {
  51. return redisx.Set(ctx, key, value, ttl)
  52. }
  53. // Get string value
  54. func Get(ctx context.Context, key string) (string, error) {
  55. val, err := redisx.Get(ctx, key)
  56. if err != nil {
  57. return "", err
  58. }
  59. return val, nil
  60. }
  61. func MSet(ctx context.Context, pairs ...interface{}) error {
  62. return redisx.MSet(ctx, pairs...)
  63. }
  64. func MGet(ctx context.Context, keys ...string) ([]interface{}, error) {
  65. return redisx.MGet(ctx, keys...)
  66. }
  67. func Incr(ctx context.Context, key string) (int64, error) {
  68. return redisx.Incr(ctx, key)
  69. }
  70. /********* 哈希操作 *********/
  71. func HSet(ctx context.Context, key string, values ...interface{}) error {
  72. return redisx.HSet(ctx, key, values...)
  73. }
  74. func HGet(ctx context.Context, key, field string) (string, error) {
  75. return redisx.HGet(ctx, key, field)
  76. }
  77. func HGetAll(ctx context.Context, key string) (map[string]string, error) {
  78. return redisx.HGetAll(ctx, key)
  79. }
  80. /********* 列表操作 *********/
  81. func LPush(ctx context.Context, key string, values ...interface{}) error {
  82. return redisx.LPush(ctx, key, values...)
  83. }
  84. func RPush(ctx context.Context, key string, values ...interface{}) error {
  85. return redisx.RPush(ctx, key, values...)
  86. }
  87. func LPop(ctx context.Context, key string) (string, error) {
  88. return redisx.LPop(ctx, key)
  89. }
  90. /********* 集合操作 *********/
  91. func SAdd(ctx context.Context, key string, members ...interface{}) error {
  92. return redisx.SAdd(ctx, key, members...)
  93. }
  94. func SRem(ctx context.Context, key string, members ...interface{}) error {
  95. return redisx.SRem(ctx, key, members...)
  96. }
  97. func SMembers(ctx context.Context, key string) ([]string, error) {
  98. return redisx.SMembers(ctx, key)
  99. }
  100. func SIsMember(ctx context.Context, key string, member interface{}) (bool, error) {
  101. return redisx.SIsMember(ctx, key, member)
  102. }
  103. /********* 有序集合操作 *********/
  104. func ZAdd(ctx context.Context, key string, members ...redis.Z) error {
  105. return redisx.ZAdd(ctx, key, members...)
  106. }
  107. func ZRange(ctx context.Context, key string, start, stop int64) ([]string, error) {
  108. return redisx.ZRange(ctx, key, start, stop)
  109. }
  110. /********* Stream(流)操作 *********/
  111. // 添加消息
  112. func XAdd(ctx context.Context, stream string, values map[string]interface{}) (string, error) {
  113. return redisx.XAdd(ctx, stream, values)
  114. }
  115. // 读取消息
  116. func XRead(ctx context.Context, streams map[string]string, count int64) ([]redis.XStream, error) {
  117. return redisx.XRead(ctx, streams, count)
  118. }
  119. // 创建消费者组
  120. func XGroupCreate(ctx context.Context, stream, group, start string) error {
  121. return redisx.XGroupCreate(ctx, stream, group, start)
  122. }
  123. // 从消费者组读取
  124. func XReadGroup(ctx context.Context, group, consumer string, streams map[string]string, count int64) ([]redis.XStream, error) {
  125. return redisx.XReadGroup(ctx, group, consumer, streams, count)
  126. }
  127. // 从消费者组读取滞留消息
  128. func XAutoClaim(ctx context.Context, stream string, group string, consumer string, idle time.Duration, count int64) (messages []redis.XMessage, start string, err error) {
  129. return redisx.XAutoClaim(ctx, stream, group, consumer, idle, count)
  130. }
  131. // 从消费者组读取指定ID的Pending状态信息
  132. func XPendingExtOne(ctx context.Context, stream string, group string, consumer string, id string) (*redis.XPendingExt, error) {
  133. return redisx.XPendingExtOne(ctx, stream, group, consumer, id)
  134. }
  135. // 确认消息
  136. func XAck(ctx context.Context, stream, group string, ids ...string) error {
  137. return redisx.XAck(ctx, stream, group, ids...)
  138. }
  139. // 确认同时删除消息
  140. func XAckDel(ctx context.Context, stream, group string, ids ...string) error {
  141. return redisx.XAckDel(ctx, stream, group, ids...)
  142. }
  143. // 查询范围内指定的消息信息
  144. func XRangeN(ctx context.Context, stream, start, stop string, count int64) ([]redis.XMessage, error) {
  145. return redisx.XRangeN(ctx, stream, start, stop, count)
  146. }
  147. // 获取完整的Stream信息
  148. func XInfoStreamFull(ctx context.Context, key string, count int) (*redis.XInfoStreamFull, error) {
  149. return redisx.XInfoStreamFull(ctx, key, count)
  150. }
  151. /********* 分布式锁操作 *********/
  152. type RedisLock = redisx.RedisLock
  153. func TryLock(ctx context.Context, key string, ttl time.Duration) (*RedisLock, bool, error) {
  154. return redisx.TryLock(ctx, key, ttl)
  155. }
  156. func Lock(ctx context.Context, key string, ttl, retryInterval, timeout time.Duration) (*RedisLock, error) {
  157. return redisx.Lock(ctx, key, ttl, retryInterval, timeout)
  158. }
  159. /********* JSON操作 *********/
  160. // SetJSON 将任意结构体序列化为 JSON 存入 Redis
  161. func SetJSON(ctx context.Context, key string, value interface{}, ttl time.Duration) error {
  162. return redisx.SetJSON(ctx, key, value, ttl)
  163. }
  164. // GetJSON 从 Redis 获取并反序列化为指定结构体
  165. func GetJSON[T any](ctx context.Context, key string, dest *T) error {
  166. return redisx.GetJSON[T](ctx, key, dest)
  167. }
  168. // MustGetJSON 返回默认值版本(未命中时返回 nil)
  169. func MustGetJSON[T any](ctx context.Context, key string) *T {
  170. return redisx.MustGetJSON[T](ctx, key)
  171. }
  172. // HSetJSON 在 Hash 中存储 JSON
  173. func HSetJSON(ctx context.Context, key, field string, value interface{}) error {
  174. return redisx.HSetJSON(ctx, key, field, value)
  175. }
  176. // HGetJSON 从 Hash 中读取 JSON
  177. func HGetJSON[T any](ctx context.Context, key, field string, dest *T) error {
  178. return redisx.HGetJSON[T](ctx, key, field, dest)
  179. }
  180. // MGetJSON 支持批量获取多个 JSON 对象
  181. func MGetJSON[T any](ctx context.Context, keys ...string) (map[string]*T, []string, error) {
  182. return redisx.MGetJSON[T](ctx, keys...)
  183. }
  184. /********* Lua脚本操作 *********/
  185. func RunScript(ctx context.Context, script string, keys []string, args ...interface{}) (interface{}, error) {
  186. return redisx.RunScript(ctx, script, keys, args...)
  187. }
  188. /********* 发布订阅操作 *********/
  189. func Publish(ctx context.Context, channel string, msg interface{}) error {
  190. return redisx.Publish(ctx, channel, msg)
  191. }
  192. func Subscribe(ctx context.Context, channel string) *redis.PubSub {
  193. return redisx.Subscribe(ctx, channel)
  194. }
  195. /********* 管道与事务 *********/
  196. func Pipeline(ctx context.Context, fn func(redis.Pipeliner) error) ([]redis.Cmder, error) {
  197. return redisx.Pipeline(ctx, fn)
  198. }
  199. /********* 工具方法 *********/
  200. // Delete
  201. func Delete(ctx context.Context, keys ...string) error {
  202. return redisx.Del(ctx, keys...)
  203. }
  204. // Expire
  205. func Expire(ctx context.Context, key string, ttl time.Duration) (bool, error) {
  206. return redisx.Expire(ctx, key, ttl)
  207. }
  208. // ExpireAt
  209. func ExpireAt(ctx context.Context, key string, tm time.Time) (bool, error) {
  210. return redisx.ExpireAt(ctx, key, tm)
  211. }
  212. // TTL
  213. func TTL(ctx context.Context, key string) (time.Duration, error) {
  214. return redisx.TTL(ctx, key)
  215. }
  216. // Exists
  217. func Exists(ctx context.Context, key string) (bool, error) {
  218. return redisx.Exists(ctx, key)
  219. }