add redid cache backend in todo list

This commit is contained in:
kenshin 2013-07-24 22:40:18 +08:00
parent 0dfc4bf100
commit f30e0ba257
4 changed files with 33 additions and 37 deletions

View File

@ -19,8 +19,6 @@ Similar as [dnsmasq](http://www.thekelleys.org.uk/dnsmasq/doc.html) ,but support
## Configuration
All the configuration on `godns.conf` a TOML formating config file.
@ -46,6 +44,8 @@ If multi `namerserver` set at resolv.conf, the upsteam server will try in order
#### cache
Only the local memory storage backend implemented now. The redis backend is in todo list

View File

@ -4,6 +4,7 @@ import (
"crypto/md5"
"encoding/json"
"fmt"
"github.com/hoisie/redis"
"github.com/miekg/dns"
"time"
)
@ -47,10 +48,9 @@ type Cache interface {
}
type MemoryCache struct {
backend map[string]*dns.Msg
serializer *JsonSerializer
expire time.Duration
maxcount int
backend map[string]*dns.Msg
expire time.Duration
maxcount int
}
func (c *MemoryCache) Get(key string) (*dns.Msg, error) {
@ -60,11 +60,6 @@ func (c *MemoryCache) Get(key string) (*dns.Msg, error) {
return nil, KeyNotFound{key}
}
// mesg := new(dns.Msg)
// if err := c.serializer.Loads([]byte(data), &mesg); err != nil {
// fmt.Println(err)
// return nil, SerializerError{}
// }
return mesg, nil
}
@ -73,13 +68,6 @@ func (c *MemoryCache) Set(key string, mesg *dns.Msg) error {
if c.Full() && !c.Exists(key) {
return CacheIsFull{}
}
// data, err := c.serializer.Dumps(mesg)
// if err != nil {
// return SerializerError{}
// }
// c.backend[key] = string(data)
c.backend[key] = mesg
return nil
}
@ -105,21 +93,28 @@ func (c *MemoryCache) Full() bool {
return c.Length() >= c.maxcount
}
// type RedisCache struct{
// backend redis.client
// }
/*
TODO: Redis cache backend
*/
// func (c *RedisCache) Get(key string) {
type RedisCache struct {
backend *redis.Client
serializer JsonSerializer
expire time.Duration
maxcount int
}
// }
func (c *RedisCache) Get() {
// func (c *RedisCache) Set() {
}
// }
func (c *RedisCache) Set() {
// func (c &RedisCache) Remove(){
}
// }
func (c *RedisCache) Remove() {
}
func KeyGen(q Question) string {
h := md5.New()

View File

@ -28,6 +28,7 @@ file = ""
[cache]
# backend option [memory|redis]
# redis backend not implemented yet
backend = "memory"
expire = 600 # 10 minutes
maxcount = 100000

View File

@ -42,18 +42,18 @@ func NewHandler() *GODNSHandler {
switch cacheConfig.Backend {
case "memory":
cache = &MemoryCache{
backend: make(map[string]*dns.Msg),
serializer: new(JsonSerializer),
expire: cacheConfig.Expire,
maxcount: cacheConfig.Maxcount,
backend: make(map[string]*dns.Msg),
expire: cacheConfig.Expire,
maxcount: cacheConfig.Maxcount,
}
case "redis":
cache = &MemoryCache{
backend: make(map[string]*dns.Msg),
serializer: new(JsonSerializer),
expire: cacheConfig.Expire,
maxcount: cacheConfig.Maxcount,
}
// cache = &MemoryCache{
// backend: make(map[string]*dns.Msg),
// serializer: new(JsonSerializer),
// expire: cacheConfig.Expire,
// maxcount: cacheConfig.Maxcount,
// }
panic("Redis cache backend not implement yet")
default:
logger.Printf("Invalid cache backend %s", cacheConfig.Backend)
panic("Invalid cache backend")