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 ## Configuration
All the configuration on `godns.conf` a TOML formating config file. 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 #### 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" "crypto/md5"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/hoisie/redis"
"github.com/miekg/dns" "github.com/miekg/dns"
"time" "time"
) )
@ -47,10 +48,9 @@ type Cache interface {
} }
type MemoryCache struct { type MemoryCache struct {
backend map[string]*dns.Msg backend map[string]*dns.Msg
serializer *JsonSerializer expire time.Duration
expire time.Duration maxcount int
maxcount int
} }
func (c *MemoryCache) Get(key string) (*dns.Msg, error) { 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} 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 return mesg, nil
} }
@ -73,13 +68,6 @@ func (c *MemoryCache) Set(key string, mesg *dns.Msg) error {
if c.Full() && !c.Exists(key) { if c.Full() && !c.Exists(key) {
return CacheIsFull{} return CacheIsFull{}
} }
// data, err := c.serializer.Dumps(mesg)
// if err != nil {
// return SerializerError{}
// }
// c.backend[key] = string(data)
c.backend[key] = mesg c.backend[key] = mesg
return nil return nil
} }
@ -105,21 +93,28 @@ func (c *MemoryCache) Full() bool {
return c.Length() >= c.maxcount 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 { func KeyGen(q Question) string {
h := md5.New() h := md5.New()

View File

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

View File

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