2025-12-29 04:10:34 +08:00
|
|
|
package redis
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-05-20 16:34:46 +08:00
|
|
|
"errors"
|
2025-12-29 04:10:34 +08:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (c *Client) GetBytes(ctx context.Context, key string) ([]byte, error) {
|
2026-05-20 16:34:46 +08:00
|
|
|
if c == nil || c.rdb == nil {
|
|
|
|
|
return nil, errors.New("redis client not initialized")
|
|
|
|
|
}
|
2025-12-29 04:10:34 +08:00
|
|
|
return c.rdb.Get(ctx, key).Bytes()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) SetBytes(ctx context.Context, key string, value []byte, ttl time.Duration) error {
|
2026-05-20 16:34:46 +08:00
|
|
|
if c == nil || c.rdb == nil {
|
|
|
|
|
return errors.New("redis client not initialized")
|
|
|
|
|
}
|
2025-12-29 04:10:34 +08:00
|
|
|
return c.rdb.Set(ctx, key, value, ttl).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) Del(ctx context.Context, key string) error {
|
2026-05-20 16:34:46 +08:00
|
|
|
if c == nil || c.rdb == nil {
|
|
|
|
|
return errors.New("redis client not initialized")
|
|
|
|
|
}
|
2025-12-29 04:10:34 +08:00
|
|
|
return c.rdb.Del(ctx, key).Err()
|
|
|
|
|
}
|
2026-03-13 15:32:48 +08:00
|
|
|
|
2026-05-23 09:23:47 +08:00
|
|
|
func (c *Client) DelByPattern(ctx context.Context, pattern string) error {
|
|
|
|
|
if c == nil || c.rdb == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
iter := c.rdb.Scan(ctx, 0, pattern, 0).Iterator()
|
|
|
|
|
for iter.Next(ctx) {
|
|
|
|
|
_ = c.rdb.Del(ctx, iter.Val())
|
|
|
|
|
}
|
|
|
|
|
return iter.Err()
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 18:57:02 +08:00
|
|
|
func (c *Client) MGet(cacheCtx context.Context, cacheKeys ...string) ([]interface{}, error) {
|
2026-05-20 16:34:46 +08:00
|
|
|
if c == nil || c.rdb == nil {
|
|
|
|
|
return nil, errors.New("redis client not initialized")
|
|
|
|
|
}
|
2026-03-14 18:57:02 +08:00
|
|
|
return c.rdb.MGet(cacheCtx, cacheKeys...).Result()
|
2026-03-13 15:32:48 +08:00
|
|
|
}
|