cache.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 map[string]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 SMembers(ctx context.Context, key string) ([]string, error) {
  95. return redisx.SMembers(ctx, key)
  96. }
  97. /********* 有序集合操作 *********/
  98. func ZAdd(ctx context.Context, key string, members ...redis.Z) error {
  99. return redisx.ZAdd(ctx, key, members...)
  100. }
  101. func ZRange(ctx context.Context, key string, start, stop int64) ([]string, error) {
  102. return redisx.ZRange(ctx, key, start, stop)
  103. }
  104. /********* Stream(流)操作 *********/
  105. // 添加消息
  106. func XAdd(ctx context.Context, stream string, values map[string]interface{}) (string, error) {
  107. return redisx.XAdd(ctx, stream, values)
  108. }
  109. // 读取消息
  110. func XRead(ctx context.Context, streams map[string]string, count int64) ([]redis.XStream, error) {
  111. return redisx.XRead(ctx, streams, count)
  112. }
  113. // 创建消费者组
  114. func XGroupCreate(ctx context.Context, stream, group, start string) error {
  115. return redisx.XGroupCreate(ctx, stream, group, start)
  116. }
  117. // 从消费者组读取
  118. func XReadGroup(ctx context.Context, group, consumer string, streams map[string]string, count int64) ([]redis.XStream, error) {
  119. return redisx.XReadGroup(ctx, group, consumer, streams, count)
  120. }
  121. // 从消费者组读取滞留消息
  122. func XAutoClaim(ctx context.Context, stream string, group string, consumer string, idle time.Duration, count int64) (messages []redis.XMessage, start string, err error) {
  123. return redisx.XAutoClaim(ctx, stream, group, consumer, idle, count)
  124. }
  125. // 从消费者组读取指定ID的Pending状态信息
  126. func XPendingExtOne(ctx context.Context, stream string, group string, consumer string, id string) (*redis.XPendingExt, error) {
  127. return redisx.XPendingExtOne(ctx, stream, group, consumer, id)
  128. }
  129. // 确认消息
  130. func XAck(ctx context.Context, stream, group string, ids ...string) error {
  131. return redisx.XAck(ctx, stream, group, ids...)
  132. }
  133. // 确认同时删除消息
  134. func XAckDel(ctx context.Context, stream, group string, ids ...string) error {
  135. return redisx.XAckDel(ctx, stream, group, ids...)
  136. }
  137. // 查询范围内指定的消息信息
  138. func XRangeN(ctx context.Context, stream, start, stop string, count int64) ([]redis.XMessage, error) {
  139. return redisx.XRangeN(ctx, stream, start, stop, count)
  140. }
  141. // 获取完整的Stream信息
  142. func XInfoStreamFull(ctx context.Context, key string, count int) (*redis.XInfoStreamFull, error) {
  143. return redisx.XInfoStreamFull(ctx, key, count)
  144. }
  145. /********* 分布式锁操作 *********/
  146. type RedisLock = redisx.RedisLock
  147. func TryLock(ctx context.Context, key string, ttl time.Duration) (*RedisLock, bool, error) {
  148. return redisx.TryLock(ctx, key, ttl)
  149. }
  150. func Lock(ctx context.Context, key string, ttl, retryInterval, timeout time.Duration) (*RedisLock, error) {
  151. return redisx.Lock(ctx, key, ttl, retryInterval, timeout)
  152. }
  153. /********* JSON操作 *********/
  154. // SetJSON 将任意结构体序列化为 JSON 存入 Redis
  155. func SetJSON(ctx context.Context, key string, value interface{}, ttl time.Duration) error {
  156. return redisx.SetJSON(ctx, key, value, ttl)
  157. }
  158. // GetJSON 从 Redis 获取并反序列化为指定结构体
  159. func GetJSON[T any](ctx context.Context, key string, dest *T) error {
  160. return redisx.GetJSON[T](ctx, key, dest)
  161. }
  162. // MustGetJSON 返回默认值版本(未命中时返回 nil)
  163. func MustGetJSON[T any](ctx context.Context, key string) *T {
  164. return redisx.MustGetJSON[T](ctx, key)
  165. }
  166. // HSetJSON 在 Hash 中存储 JSON
  167. func HSetJSON(ctx context.Context, key, field string, value interface{}) error {
  168. return redisx.HSetJSON(ctx, key, field, value)
  169. }
  170. // HGetJSON 从 Hash 中读取 JSON
  171. func HGetJSON[T any](ctx context.Context, key, field string, dest *T) error {
  172. return redisx.HGetJSON[T](ctx, key, field, dest)
  173. }
  174. // MGetJSON 支持批量获取多个 JSON 对象
  175. func MGetJSON[T any](ctx context.Context, keys ...string) (map[string]*T, []string, error) {
  176. return redisx.MGetJSON[T](ctx, keys...)
  177. }
  178. /********* Lua脚本操作 *********/
  179. func RunScript(ctx context.Context, script string, keys []string, args ...interface{}) (interface{}, error) {
  180. return redisx.RunScript(ctx, script, keys, args...)
  181. }
  182. /********* 发布订阅操作 *********/
  183. func Publish(ctx context.Context, channel string, msg interface{}) error {
  184. return redisx.Publish(ctx, channel, msg)
  185. }
  186. func Subscribe(ctx context.Context, channel string) *redis.PubSub {
  187. return redisx.Subscribe(ctx, channel)
  188. }
  189. /********* 管道与事务 *********/
  190. func Pipeline(ctx context.Context, fn func(redis.Pipeliner) error) ([]redis.Cmder, error) {
  191. return redisx.Pipeline(ctx, fn)
  192. }
  193. /********* 工具方法 *********/
  194. // Delete
  195. func Delete(ctx context.Context, keys ...string) error {
  196. return redisx.Del(ctx, keys...)
  197. }
  198. // Expire
  199. func Expire(ctx context.Context, key string, ttl time.Duration) (bool, error) {
  200. return redisx.Expire(ctx, key, ttl)
  201. }
  202. // ExpireAt
  203. func ExpireAt(ctx context.Context, key string, tm time.Time) (bool, error) {
  204. return redisx.ExpireAt(ctx, key, tm)
  205. }
  206. // TTL
  207. func TTL(ctx context.Context, key string) (time.Duration, error) {
  208. return redisx.TTL(ctx, key)
  209. }
  210. // Exists
  211. func Exists(ctx context.Context, key string) (bool, error) {
  212. return redisx.Exists(ctx, key)
  213. }