2013-07-23 11:10:38 +00:00
|
|
|
package main
|
|
|
|
|
2013-07-24 10:29:38 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2015-02-10 09:00:59 +00:00
|
|
|
|
2018-07-01 15:49:24 +00:00
|
|
|
"crypto/md5"
|
2019-09-26 04:43:17 +00:00
|
|
|
"github.com/miekg/dns"
|
2013-07-24 10:29:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2016-02-13 09:55:19 +00:00
|
|
|
err error
|
2013-07-24 10:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e SerializerError) Error() string {
|
2016-02-13 09:55:19 +00:00
|
|
|
return fmt.Sprintf("Serializer error: got %v", e.err)
|
2013-07-24 10:29:38 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 16:09:07 +00:00
|
|
|
type Mesg struct {
|
|
|
|
Msg *dns.Msg
|
|
|
|
Expire time.Time
|
|
|
|
}
|
|
|
|
|
2013-07-24 10:29:38 +00:00
|
|
|
type Cache interface {
|
2013-07-24 16:09:07 +00:00
|
|
|
Get(key string) (Msg *dns.Msg, err error)
|
|
|
|
Set(key string, Msg *dns.Msg) error
|
2013-07-24 14:47:39 +00:00
|
|
|
Exists(key string) bool
|
2016-02-12 16:08:48 +00:00
|
|
|
Remove(key string) error
|
|
|
|
Full() bool
|
|
|
|
}
|
|
|
|
|
2013-07-24 10:29:38 +00:00
|
|
|
func KeyGen(q Question) string {
|
|
|
|
h := md5.New()
|
|
|
|
h.Write([]byte(q.String()))
|
|
|
|
x := h.Sum(nil)
|
|
|
|
key := fmt.Sprintf("%x", x)
|
|
|
|
return key
|
2018-07-01 15:49:24 +00:00
|
|
|
}
|