convox/pkg/router/cache_redis.go
David Dollar e0223e3138
router: add redis storage backend (#91)
* router: add redis storage backend

* add newline to error print
2020-01-29 19:15:56 -05:00

74 lines
1.4 KiB
Go

package router
import (
"context"
"crypto/tls"
"fmt"
"github.com/go-redis/redis"
"golang.org/x/crypto/acme/autocert"
)
type CacheRedis struct {
redis *redis.Client
}
func NewCacheRedis(addr, password string, secure bool) (*CacheRedis, error) {
fmt.Printf("ns=cache.redis at=new addr=%s\n", addr)
opts := &redis.Options{
Addr: addr,
Password: password,
}
if secure {
opts.TLSConfig = &tls.Config{}
}
rc := redis.NewClient(opts)
if _, err := rc.Ping().Result(); err != nil {
return nil, err
}
r := &CacheRedis{
redis: rc,
}
return r, nil
}
func (c *CacheRedis) Delete(ctx context.Context, key string) error {
fmt.Printf("ns=cache.redis at=delete key=%s\n", key)
if _, err := c.redis.Del(fmt.Sprintf("router/cache/%s", key)).Result(); err != nil {
return err
}
return nil
}
func (c *CacheRedis) Get(ctx context.Context, key string) ([]byte, error) {
fmt.Printf("ns=cache.redis at=get key=%s\n", key)
v, err := c.redis.Get(fmt.Sprintf("router/cache/%s", key)).Bytes()
if err == redis.Nil {
return nil, autocert.ErrCacheMiss
}
if err != nil {
return nil, err
}
return v, nil
}
func (c *CacheRedis) Put(ctx context.Context, key string, data []byte) error {
fmt.Printf("ns=cache.redis at=put key=%s\n", key)
if _, err := c.redis.Set(fmt.Sprintf("router/cache/%s", key), data, 0).Result(); err != nil {
return err
}
return nil
}