2013-07-23 11:10:38 +00:00
|
|
|
package main
|
|
|
|
|
2013-07-24 10:29:38 +00:00
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type KeyNotFound struct {
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e KeyNotFound) Error() string {
|
|
|
|
return e.key + " " + "not found"
|
|
|
|
}
|
|
|
|
|
|
|
|
type KeyExpired struct {
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e KeyExpired) Error() string {
|
|
|
|
return e.Key + " " + "expired"
|
|
|
|
}
|
|
|
|
|
|
|
|
type CacheIsFull struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e CacheIsFull) Error() string {
|
|
|
|
return "Cache is Full"
|
|
|
|
}
|
|
|
|
|
|
|
|
type SerializerError struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e SerializerError) Error() string {
|
|
|
|
return "Serializer error"
|
|
|
|
}
|
|
|
|
|
|
|
|
type Cache interface {
|
2013-07-24 10:48:43 +00:00
|
|
|
Get(string) (*dns.Msg, error)
|
2013-07-24 10:29:38 +00:00
|
|
|
Set(string, *dns.Msg) error
|
|
|
|
Exists(string) bool
|
|
|
|
Remove()
|
|
|
|
Length() int
|
|
|
|
}
|
|
|
|
|
|
|
|
type MemoryCache struct {
|
|
|
|
backend map[string]string
|
|
|
|
serializer *JsonSerializer
|
|
|
|
expire time.Duration
|
|
|
|
maxcount int
|
|
|
|
}
|
|
|
|
|
2013-07-24 10:48:43 +00:00
|
|
|
func (c *MemoryCache) Get(key string) (*dns.Msg, error) {
|
2013-07-24 10:29:38 +00:00
|
|
|
fmt.Println(c.backend)
|
|
|
|
data, ok := c.backend[key]
|
|
|
|
if !ok {
|
|
|
|
return nil, KeyNotFound{key}
|
|
|
|
}
|
|
|
|
|
2013-07-24 10:48:43 +00:00
|
|
|
mesg := new(dns.Msg)
|
|
|
|
if err := c.serializer.Loads([]byte(data), &mesg); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, SerializerError{}
|
|
|
|
}
|
|
|
|
return mesg, nil
|
2013-07-24 10:29:38 +00:00
|
|
|
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 10:29:38 +00:00
|
|
|
func (c *MemoryCache) Set(key string, mesg *dns.Msg) error {
|
|
|
|
if c.Full() && !c.Exists(key) {
|
|
|
|
return CacheIsFull{}
|
|
|
|
}
|
2013-07-24 10:48:43 +00:00
|
|
|
data, err := c.serializer.Dumps(mesg)
|
2013-07-24 10:29:38 +00:00
|
|
|
|
2013-07-24 10:48:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return SerializerError{}
|
|
|
|
}
|
2013-07-24 10:29:38 +00:00
|
|
|
|
2013-07-24 10:48:43 +00:00
|
|
|
c.backend[key] = string(data)
|
2013-07-24 10:29:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MemoryCache) Remove() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MemoryCache) Exists(key string) bool {
|
|
|
|
_, ok := c.backend[key]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MemoryCache) Length() int {
|
|
|
|
return len(c.backend)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MemoryCache) Full() bool {
|
|
|
|
// if maxcount is zero. the cache will never be full.
|
|
|
|
if c.maxcount == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return c.Length() >= c.maxcount
|
|
|
|
}
|
|
|
|
|
|
|
|
// type RedisCache struct{
|
|
|
|
// backend redis.client
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func (c *RedisCache) Get(key string) {
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func (c *RedisCache) Set() {
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// func (c &RedisCache) Remove(){
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
func KeyGen(q Question) string {
|
|
|
|
h := md5.New()
|
|
|
|
h.Write([]byte(q.String()))
|
|
|
|
x := h.Sum(nil)
|
|
|
|
key := fmt.Sprintf("%x", x)
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
type JsonSerializer struct {
|
|
|
|
}
|
2013-07-23 11:10:38 +00:00
|
|
|
|
2013-07-24 10:29:38 +00:00
|
|
|
func (*JsonSerializer) Dumps(mesg *dns.Msg) (encoded []byte, err error) {
|
|
|
|
encoded, err = json.Marshal(mesg)
|
|
|
|
return
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 10:29:38 +00:00
|
|
|
func (*JsonSerializer) Loads(data []byte, mesg **dns.Msg) error {
|
|
|
|
err := json.Unmarshal(data, mesg)
|
|
|
|
return err
|
2013-07-23 11:10:38 +00:00
|
|
|
|
|
|
|
}
|