godns/cache.go

61 lines
910 B
Go

package main
import (
"fmt"
"time"
"github.com/miekg/dns"
"crypto/md5"
)
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 {
err error
}
func (e SerializerError) Error() string {
return fmt.Sprintf("Serializer error: got %v", e.err)
}
type Mesg struct {
Msg *dns.Msg
Expire time.Time
}
type Cache interface {
Get(key string) (Msg *dns.Msg, err error)
Set(key string, Msg *dns.Msg) error
Exists(key string) bool
Remove(key string) error
Full() bool
}
func KeyGen(q Question) string {
h := md5.New()
h.Write([]byte(q.String()))
x := h.Sum(nil)
key := fmt.Sprintf("%x", x)
return key
}