concord/internal/infra/cache/cache.go

135 lines
3.0 KiB
Go

package cache
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/redis/go-redis/v9"
)
type Cache struct {
client *redis.Client
}
func New(host string, port int, password string, db int) (*Cache, error) {
client := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", host, port),
Password: password,
DB: db,
DialTimeout: 5 * time.Second,
ReadTimeout: 3 * time.Second,
WriteTimeout: 3 * time.Second,
PoolSize: 10,
MinIdleConns: 5,
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := client.Ping(ctx).Err(); err != nil {
return nil, fmt.Errorf("redis ping failed: %w", err)
}
return &Cache{client: client}, nil
}
func (c *Cache) Client() *redis.Client {
return c.client
}
func (c *Cache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error {
data, err := json.Marshal(value)
if err != nil {
return fmt.Errorf("marshal value: %w", err)
}
return c.client.Set(ctx, key, data, ttl).Err()
}
func (c *Cache) Get(ctx context.Context, key string, dest interface{}) error {
data, err := c.client.Get(ctx, key).Bytes()
if err == redis.Nil {
return ErrCacheMiss
}
if err != nil {
return err
}
return json.Unmarshal(data, dest)
}
func (c *Cache) Delete(ctx context.Context, keys ...string) error {
return c.client.Del(ctx, keys...).Err()
}
func (c *Cache) Exists(ctx context.Context, key string) (bool, error) {
result, err := c.client.Exists(ctx, key).Result()
if err != nil {
return false, err
}
return result > 0, nil
}
func (c *Cache) SetNX(ctx context.Context, key string, value interface{}, ttl time.Duration) (bool, error) {
data, err := json.Marshal(value)
if err != nil {
return false, fmt.Errorf("marshal value: %w", err)
}
return c.client.SetNX(ctx, key, data, ttl).Result()
}
func (c *Cache) Incr(ctx context.Context, key string) (int64, error) {
return c.client.Incr(ctx, key).Result()
}
func (c *Cache) Expire(ctx context.Context, key string, ttl time.Duration) error {
return c.client.Expire(ctx, key, ttl).Err()
}
func (c *Cache) Ping(ctx context.Context) error {
return c.client.Ping(ctx).Err()
}
func (c *Cache) Close() error {
return c.client.Close()
}
func (c *Cache) FlushAll(ctx context.Context) error {
return c.client.FlushAll(ctx).Err()
}
var ErrCacheMiss = fmt.Errorf("cache miss")
func (c *Cache) DeletePattern(ctx context.Context, pattern string) error {
iter := c.client.Scan(ctx, 0, pattern, 0).Iterator()
pipe := c.client.Pipeline()
for iter.Next(ctx) {
pipe.Del(ctx, iter.Val())
}
if err := iter.Err(); err != nil {
return err
}
_, err := pipe.Exec(ctx)
return err
}
func (c *Cache) HSet(ctx context.Context, key string, values map[string]string, ttl time.Duration) error {
pipe := c.client.Pipeline()
for k, v := range values {
pipe.HSet(ctx, key, k, v)
}
pipe.Expire(ctx, key, ttl)
_, err := pipe.Exec(ctx)
return err
}
func (c *Cache) HGetAll(ctx context.Context, key string) (map[string]string, error) {
return c.client.HGetAll(ctx, key).Result()
}