memory cache with map[string]string

This commit is contained in:
kenshin 2013-07-24 18:48:43 +08:00
parent 57304bf910
commit 51be27eee9
2 changed files with 14 additions and 16 deletions

View File

@ -39,7 +39,7 @@ func (e SerializerError) Error() string {
} }
type Cache interface { type Cache interface {
Get(string) ([]byte, error) Get(string) (*dns.Msg, error)
Set(string, *dns.Msg) error Set(string, *dns.Msg) error
Exists(string) bool Exists(string) bool
Remove() Remove()
@ -53,20 +53,19 @@ type MemoryCache struct {
maxcount int maxcount int
} }
func (c *MemoryCache) Get(key string) ([]byte, error) { func (c *MemoryCache) Get(key string) (*dns.Msg, error) {
fmt.Println(c.backend) fmt.Println(c.backend)
data, ok := c.backend[key] data, ok := c.backend[key]
if !ok { if !ok {
return nil, KeyNotFound{key} return nil, KeyNotFound{key}
} }
return []byte(data), nil
// mesg := new(dns.Msg) mesg := new(dns.Msg)
// if err := c.serializer.Loads([]byte(data), &mesg); err != nil { if err := c.serializer.Loads([]byte(data), &mesg); err != nil {
// fmt.Println(err) fmt.Println(err)
// return nil, SerializerError{} return nil, SerializerError{}
// } }
// return mesg, nil return mesg, nil
} }
@ -74,13 +73,13 @@ 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) data, err := c.serializer.Dumps(mesg)
// if err != nil { if err != nil {
// return SerializerError{} return SerializerError{}
// } }
c.backend[key] = mesg.String() c.backend[key] = string(data)
return nil return nil
} }

View File

@ -79,8 +79,7 @@ func (h *GODNSHandler) do(net string, w dns.ResponseWriter, req *dns.Msg) {
Debug("%s didn't hit cache: %s", Q.String(), err) Debug("%s didn't hit cache: %s", Q.String(), err)
} else { } else {
Debug("%s hit cache", Q.String()) Debug("%s hit cache", Q.String())
fmt.Println(string(mesg)) w.WriteMsg(mesg)
w.Write(mesg)
return return
} }