Skip to content

Instantly share code, notes, and snippets.

@dcaponi
Created August 11, 2021 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcaponi/4b9b02f73489a34f613a3de4280ca3d0 to your computer and use it in GitHub Desktop.
Save dcaponi/4b9b02f73489a34f613a3de4280ca3d0 to your computer and use it in GitHub Desktop.
pw_less cache.go
package cache
import (
"context"
"log"
"time"
"github.com/go-redis/redis/v8"
)
var ctx = context.Background()
type Cache interface {
Get(key string) (string, error)
Set(key string, val interface{}) error
Setex(key string, val interface{}, ttl time.Duration) error
Del(key string) error
}
type RedisCacheConfig struct {
Addr, Password string
DB int
}
type RedisCache struct {
Client *redis.Client
}
func NewRedisCache(c RedisCacheConfig) (RedisCache, error) {
cache := RedisCache{
Client: redis.NewClient(&redis.Options{
Addr: c.Addr,
Password: c.Password,
DB: c.DB,
}),
}
_, err := cache.Client.Ping(ctx).Result()
if err != nil {
return cache, err
}
log.Println("established connection to redis!")
return cache, nil
}
func (c RedisCache) Get(key string) (string, error) {
return c.Client.Get(ctx, key).Result()
}
func (c RedisCache) Set(key string, val interface{}) error {
return c.Client.Set(ctx, key, val, 0).Err()
}
func (c RedisCache) Setex(key string, val interface{}, ttl time.Duration) error {
return c.Client.Set(ctx, key, val, ttl).Err()
}
func (c RedisCache) Del(key string) error {
return c.Client.Del(ctx, key).Err()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment