-
Notifications
You must be signed in to change notification settings - Fork 109
/
cache.go
217 lines (180 loc) · 4.01 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"crypto/md5"
"fmt"
"sync"
"time"
"github.com/miekg/dns"
)
// KeyNotFound type
type KeyNotFound struct {
key string
}
// Error formats an error for the KeyNotFound type
func (e KeyNotFound) Error() string {
return e.key + " " + "not found"
}
// KeyExpired type
type KeyExpired struct {
Key string
}
// Error formats an error for the KeyExpired type
func (e KeyExpired) Error() string {
return e.Key + " " + "expired"
}
// CacheIsFull type
type CacheIsFull struct {
}
// Error formats an error for the CacheIsFull type
func (e CacheIsFull) Error() string {
return "Cache is Full"
}
// SerializerError type
type SerializerError struct {
}
// Error formats an error for the SerializerError type
func (e SerializerError) Error() string {
return "Serializer error"
}
// Mesg represents a cache entry
type Mesg struct {
Msg *dns.Msg
Expire time.Time
}
// Cache interface
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)
Length() int
}
// MemoryCache type
type MemoryCache struct {
Backend map[string]Mesg
Expire time.Duration
Maxcount int
mu sync.RWMutex
}
// MemoryBlockCache type
type MemoryBlockCache struct {
Backend map[string]bool
mu sync.RWMutex
}
// MemoryQuestionCache type
type MemoryQuestionCache struct {
Backend []QuestionCacheEntry `json:"entry"`
Maxcount int
mu sync.RWMutex
}
// Get returns the entry for a key or an error
func (c *MemoryCache) Get(key string) (*dns.Msg, error) {
c.mu.RLock()
mesg, ok := c.Backend[key]
c.mu.RUnlock()
if !ok {
return nil, KeyNotFound{key}
}
if mesg.Expire.Before(time.Now()) {
c.Remove(key)
return nil, KeyExpired{key}
}
return mesg.Msg, nil
}
// Set sets a keys value to a Mesg
func (c *MemoryCache) Set(key string, msg *dns.Msg) error {
if c.Full() && !c.Exists(key) {
return CacheIsFull{}
}
expire := time.Now().Add(c.Expire)
mesg := Mesg{msg, expire}
c.mu.Lock()
c.Backend[key] = mesg
c.mu.Unlock()
return nil
}
// Remove removes an entry from the cache
func (c *MemoryCache) Remove(key string) {
c.mu.Lock()
delete(c.Backend, key)
c.mu.Unlock()
}
// Exists returns whether or not a key exists in the cache
func (c *MemoryCache) Exists(key string) bool {
c.mu.RLock()
_, ok := c.Backend[key]
c.mu.RUnlock()
return ok
}
// Length returns the caches length
func (c *MemoryCache) Length() int {
c.mu.RLock()
defer c.mu.RUnlock()
return len(c.Backend)
}
// Full returns whether or not the cache is full
func (c *MemoryCache) Full() bool {
if c.Maxcount == 0 {
return false
}
return c.Length() >= c.Maxcount
}
// KeyGen generates a key for the hash from a question
func KeyGen(q Question) string {
h := md5.New()
h.Write([]byte(q.String()))
x := h.Sum(nil)
key := fmt.Sprintf("%x", x)
return key
}
// Get returns the entry for a key or an error
func (c *MemoryBlockCache) Get(key string) (bool, error) {
c.mu.RLock()
val, ok := c.Backend[key]
c.mu.RUnlock()
if !ok {
return false, KeyNotFound{key}
}
return val, nil
}
// Set sets a value in the BlockCache
func (c *MemoryBlockCache) Set(key string, value bool) error {
c.mu.Lock()
c.Backend[key] = value
c.mu.Unlock()
return nil
}
// Exists returns whether or not a key exists in the cache
func (c *MemoryBlockCache) Exists(key string) bool {
c.mu.RLock()
_, ok := c.Backend[key]
c.mu.RUnlock()
return ok
}
// Length returns the caches length
func (c *MemoryBlockCache) Length() int {
c.mu.RLock()
defer c.mu.RUnlock()
return len(c.Backend)
}
// Add adds a question to the cache
func (c *MemoryQuestionCache) Add(q QuestionCacheEntry) {
c.mu.Lock()
if c.Maxcount != 0 && len(c.Backend) >= c.Maxcount {
c.Backend = nil
}
c.Backend = append(c.Backend, q)
c.mu.Unlock()
}
// Clear clears the contents of the cache
func (c *MemoryQuestionCache) Clear() {
c.mu.Lock()
c.Backend = nil
c.mu.Unlock()
}
// Length returns the caches length
func (c *MemoryQuestionCache) Length() int {
c.mu.RLock()
defer c.mu.RUnlock()
return len(c.Backend)
}