This repository was archived by the owner on Aug 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
310 lines (262 loc) · 7.05 KB
/
cache.go
File metadata and controls
310 lines (262 loc) · 7.05 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package godns
import (
"context"
"crypto/md5"
"encoding/json"
"fmt"
"sync"
"time"
"github.com/bradfitz/gomemcache/memcache"
"github.com/redis/go-redis/v9"
"github.com/miekg/dns"
)
// KeyNotFound is an error type for when a key does not exist in the cache.
type KeyNotFound struct {
key string
}
func (e KeyNotFound) Error() string {
return fmt.Sprintf("key %q not found", e.key)
}
// KeyExpired is an error type for when a key exists but has expired.
type KeyExpired struct {
Key string
}
func (e KeyExpired) Error() string {
return fmt.Sprintf("key %q expired", e.Key)
}
// CacheIsFull is an error type indicating the cache has reached its maximum capacity.
type CacheIsFull struct{}
func (e CacheIsFull) Error() string {
return "cache is full"
}
// SerializerError is an error type for issues with message serialization.
type SerializerError struct {
err error
}
func (e SerializerError) Error() string {
return fmt.Sprintf("serializer error: %v", e.err)
}
// Mesg wraps a dns.Msg with its expiration time.
type Mesg struct {
Msg *dns.Msg
Expire time.Time
}
// Cache defines the interface for different caching backends.
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
}
// MemoryCache is a simple in-memory cache implementation.
type MemoryCache struct {
Backend map[string]Mesg
Expire time.Duration
Maxcount int
mu sync.RWMutex
}
// Get retrieves a DNS message from the in-memory cache by key.
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 stores a DNS message in the in-memory cache.
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 deletes a DNS message from the in-memory cache.
func (c *MemoryCache) Remove(key string) error {
c.mu.Lock()
delete(c.Backend, key)
c.mu.Unlock()
return nil
}
// Exists checks if a key is present in the in-memory cache.
func (c *MemoryCache) Exists(key string) bool {
c.mu.RLock()
_, ok := c.Backend[key]
c.mu.RUnlock()
return ok
}
// Length returns the number of items in the cache.
func (c *MemoryCache) Length() int {
c.mu.RLock()
defer c.mu.RUnlock()
return len(c.Backend)
}
// Full checks if the in-memory cache has reached its maximum capacity.
func (c *MemoryCache) Full() bool {
// If Maxcount is zero, the cache is never full.
if c.Maxcount == 0 {
return false
}
return c.Length() >= c.Maxcount
}
// NewMemcachedCache creates a new MemcachedCache instance.
func NewMemcachedCache(servers []string, expire int32) *MemcachedCache {
c := memcache.New(servers...)
return &MemcachedCache{
backend: c,
expire: expire,
}
}
// MemcachedCache is a cache implementation using Memcached as the backend.
type MemcachedCache struct {
backend *memcache.Client
expire int32
}
// Set stores a DNS message in Memcached.
func (m *MemcachedCache) Set(key string, msg *dns.Msg) error {
var val []byte
var err error
if msg == nil {
val = []byte("nil")
} else {
val, err = msg.Pack()
}
if err != nil {
return SerializerError{err}
}
return m.backend.Set(&memcache.Item{Key: key, Value: val, Expiration: m.expire})
}
// Get retrieves a DNS message from Memcached.
func (m *MemcachedCache) Get(key string) (*dns.Msg, error) {
var msg dns.Msg
item, err := m.backend.Get(key)
if err != nil {
return &msg, KeyNotFound{key}
}
err = msg.Unpack(item.Value)
if err != nil {
return &msg, SerializerError{err}
}
return &msg, err
}
// Exists checks if a key is present in Memcached.
func (m *MemcachedCache) Exists(key string) bool {
_, err := m.backend.Get(key)
return err == nil
}
// Remove deletes a key from Memcached.
func (m *MemcachedCache) Remove(key string) error {
return m.backend.Delete(key)
}
// Full indicates whether the cache is at capacity. Memcached is
// an LRU cache, so it is never "full" in this context.
func (m *MemcachedCache) Full() bool {
return false
}
// NewRedisCache creates a new RedisCache instance.
func NewRedisCache(rs RedisSettings, expire int64) *RedisCache {
rdb := redis.NewClient(&redis.Options{
Addr: rs.Addr, // Corrected: Used the existing Addr field
DB: rs.DB,
Password: rs.Password,
})
return &RedisCache{
Backend: rdb,
Expire: time.Duration(expire) * time.Second,
}
}
// RedisCache is a cache implementation using Redis as the backend.
type RedisCache struct {
Backend *redis.Client
Expire time.Duration
}
// Get retrieves a DNS message from Redis.
func (r *RedisCache) Get(key string) (*dns.Msg, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
item, err := r.Backend.Get(ctx, key).Bytes()
if err == redis.Nil {
return nil, KeyNotFound{key}
}
if err != nil {
return nil, err
}
var msg dns.Msg
if string(item) == "nil" {
// Handle nil values for negacache
return nil, nil
}
err = msg.Unpack(item)
if err != nil {
return &msg, SerializerError{err}
}
return &msg, err
}
// Set stores a DNS message in Redis with a specified expiration time.
func (r *RedisCache) Set(key string, msg *dns.Msg) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var val []byte
var err error
if msg == nil {
val = []byte("nil")
} else {
val, err = msg.Pack()
}
if err != nil {
return SerializerError{err}
}
return r.Backend.Set(ctx, key, val, r.Expire).Err()
}
// Exists checks if a key is present in Redis.
func (r *RedisCache) Exists(key string) bool {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
val, err := r.Backend.Exists(ctx, key).Result()
return err == nil && val > 0
}
// Remove deletes a key from Redis.
func (r *RedisCache) Remove(key string) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := r.Backend.Del(ctx, key).Result()
return err
}
// Full indicates whether the cache is at capacity. Redis is not "full"
// in this context as it can scale.
func (r *RedisCache) Full() bool {
return false
}
// KeyGen generates a unique key for a DNS question using MD5 hashing.
func KeyGen(q Question) string {
h := md5.New()
h.Write([]byte(q.String()))
x := h.Sum(nil)
key := fmt.Sprintf("%x", x)
return key
}
// JsonSerializer provides methods for marshaling and unmarshaling
// DNS messages to and from JSON format.
type JsonSerializer struct{}
// Dumps serializes a dns.Msg to a JSON byte slice.
func (*JsonSerializer) Dumps(mesg *dns.Msg) (encoded []byte, err error) {
encoded, err = json.Marshal(*mesg)
return
}
// Loads deserializes a JSON byte slice into a dns.Msg.
func (*JsonSerializer) Loads(data []byte) (*dns.Msg, error) {
var mesg dns.Msg
err := json.Unmarshal(data, &mesg)
return &mesg, err
}