2020-01-25 17:43:02 +00:00
|
|
|
package cache
|
2018-07-01 15:49:24 +00:00
|
|
|
|
|
|
|
import (
|
2020-01-25 17:43:02 +00:00
|
|
|
"github.com/go-redis/redis/v7"
|
2018-07-01 15:49:24 +00:00
|
|
|
"github.com/miekg/dns"
|
2020-01-25 17:43:02 +00:00
|
|
|
"meow.tf/joker/godns/settings"
|
|
|
|
"time"
|
2018-07-01 15:49:24 +00:00
|
|
|
)
|
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
func NewRedisCache(c settings.RedisSettings, expire time.Duration) *RedisCache {
|
2020-01-25 17:43:02 +00:00
|
|
|
rc := redis.NewClient(&redis.Options{Addr: c.Addr(), DB: c.DB, Password: c.Password})
|
2018-07-01 15:49:24 +00:00
|
|
|
|
|
|
|
return &RedisCache{
|
|
|
|
backend: rc,
|
|
|
|
expire: expire,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RedisCache struct {
|
|
|
|
Cache
|
|
|
|
|
|
|
|
backend *redis.Client
|
2020-01-25 18:48:26 +00:00
|
|
|
expire time.Duration
|
2018-07-01 15:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *RedisCache) Set(key string, msg *dns.Msg) error {
|
|
|
|
var val []byte
|
|
|
|
var err error
|
|
|
|
|
2020-01-25 17:43:02 +00:00
|
|
|
key = "cache:" + key
|
|
|
|
|
2018-07-01 15:49:24 +00:00
|
|
|
// handle cases for negacache where it sets nil values
|
|
|
|
if msg == nil {
|
|
|
|
val = []byte("nil")
|
|
|
|
} else {
|
|
|
|
val, err = msg.Pack()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
err = SerializerError{err}
|
|
|
|
}
|
2020-01-25 18:48:26 +00:00
|
|
|
return m.backend.Set(key, val, m.expire).Err()
|
2018-07-01 15:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *RedisCache) Get(key string) (*dns.Msg, error) {
|
|
|
|
var msg dns.Msg
|
2020-01-25 17:43:02 +00:00
|
|
|
var err error
|
|
|
|
key = "cache:" + key
|
|
|
|
|
|
|
|
item, err := m.backend.Get(key).Bytes()
|
|
|
|
|
2018-07-01 15:49:24 +00:00
|
|
|
if err != nil {
|
|
|
|
err = KeyNotFound{key}
|
|
|
|
return &msg, err
|
|
|
|
}
|
2020-01-25 17:43:02 +00:00
|
|
|
|
2018-07-01 15:49:24 +00:00
|
|
|
err = msg.Unpack(item)
|
|
|
|
if err != nil {
|
|
|
|
err = SerializerError{err}
|
|
|
|
}
|
|
|
|
return &msg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *RedisCache) Exists(key string) bool {
|
2020-01-25 17:43:02 +00:00
|
|
|
res, err := m.backend.Exists(key).Result()
|
|
|
|
|
2018-07-01 15:49:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
2020-01-25 17:43:02 +00:00
|
|
|
|
|
|
|
return res == 1
|
2018-07-01 15:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *RedisCache) Remove(key string) error {
|
2020-01-25 17:43:02 +00:00
|
|
|
return m.backend.Del(key).Err()
|
2018-07-01 15:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *RedisCache) Full() bool {
|
2018-08-05 08:48:26 +00:00
|
|
|
// redis is never full (LRU)
|
2018-07-01 15:49:24 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-01-25 17:43:02 +00:00
|
|
|
|
|
|
|
func (m *RedisCache) Purge() error {
|
|
|
|
iter := m.backend.Scan(0, "cache:*", 0).Iterator()
|
|
|
|
|
|
|
|
if iter.Err() != nil {
|
|
|
|
return iter.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
for iter.Next() {
|
|
|
|
err = m.backend.Del(iter.Val()).Err()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|