omnix-sopiga/framework/query/cache.go

38 lines
721 B
Go
Raw Normal View History

2026-08-07 12:21:42 +07:00
package query
import (
"context"
"encoding/json"
"time"
"github.com/redis/go-redis/v9"
)
type Cache struct {
client *redis.Client
}
func NewCache(client *redis.Client) *Cache {
return &Cache{client: client}
}
func (c *Cache) Get(ctx context.Context, key string, dest any) error {
val, err := c.client.Get(ctx, key).Bytes()
if err != nil {
return err
}
return json.Unmarshal(val, dest)
}
func (c *Cache) Set(ctx context.Context, key string, value any, ttl time.Duration) error {
data, err := json.Marshal(value)
if err != nil {
return err
}
return c.client.Set(ctx, key, data, ttl).Err()
}
func (c *Cache) Invalidate(ctx context.Context, key string) error {
return c.client.Del(ctx, key).Err()
}