39 lines
980 B
Go
39 lines
980 B
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"github.com/go-redis/redis/v8"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
func newRedisCache() Manager {
|
|
redisClient := redis.NewClient(&redis.Options{
|
|
Addr: os.Getenv("REDIS_URL"),
|
|
Username: os.Getenv("REDIS_USERNAME"),
|
|
Password: os.Getenv("REDIS_PASSWORD"),
|
|
})
|
|
return &redisCache{redisClient}
|
|
}
|
|
|
|
type redisCache struct {
|
|
client *redis.Client
|
|
}
|
|
|
|
func (r *redisCache) Set(key, value string, expiration time.Duration) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
|
defer cancel()
|
|
return r.client.Set(ctx, key, value, expiration).Err()
|
|
}
|
|
func (r *redisCache) Get(key string) (string, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
|
defer cancel()
|
|
cacheValue := r.client.Get(ctx, key)
|
|
return cacheValue.Val(), cacheValue.Err()
|
|
}
|
|
func (r *redisCache) Remove(key string) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
|
defer cancel()
|
|
r.client.Del(ctx, key)
|
|
}
|