38 lines
721 B
Go
38 lines
721 B
Go
|
|
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()
|
||
|
|
}
|