-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
116 lines (96 loc) · 2.41 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
package main
import (
"sync"
"time"
"github.com/miekg/dns"
)
// CacheItem DNS cache message item
type CacheItem struct {
Hit int64 `label:"query cache hit count"`
Expire int64 `label:"dns query result cache exprice, zero is never expire"`
Msg *dns.Msg `label:"dns query result"`
}
// Cache memory base dns query cache
// TODO 计划要添加一个后台线程,对查询次数多的进行后台更新来加速整体性能
type Cache struct {
MaxCount int `label:"number of dns query cache item, zero is not limit"`
MinTTL int64 `label:"min cache time, zero is not limit"`
MaxTTL int64 `label:"max cache time, zero is not limit"`
mu *sync.RWMutex `label:"query cache read & write lock"`
backend map[string]*CacheItem `label:"dns query cache store"`
}
// Get get query cache
func (c *Cache) Get(key string) (*dns.Msg, error) {
var err error
var msg *dns.Msg
c.mu.RLock()
if item, ok := c.backend[key]; ok {
msg = item.Msg.Copy()
if item.Expire > 0 && item.Expire < time.Now().Unix() {
err = ErrCacheExpire
}
} else {
err = ErrNotFound
}
c.mu.RUnlock()
return msg, err
}
// Set Set query cache
func (c *Cache) Set(key string, msg *CacheItem) bool {
c.mu.Lock()
c.backend[key] = msg
c.mu.Unlock()
return true
}
// Remove remove query cache
func (c *Cache) Remove(key string) {
c.mu.Lock()
delete(c.backend, key)
c.mu.Unlock()
}
// IsExpire check dns cache is expire
func (c *Cache) IsExpire(key string) bool {
var flag bool
c.mu.RLock()
if item, ok := c.backend[key]; ok {
if item.Expire > 0 && item.Expire < time.Now().Unix() {
flag = true
}
}
c.mu.RUnlock()
return flag
}
func (c *Cache) GC() {
var expire = time.Now().Unix() - 86400
c.mu.Lock()
for k, v := range c.backend {
if v.Expire > 0 && v.Expire < expire {
delete(c.backend, k)
}
}
c.mu.Unlock()
}
// Reset reset dns query cache result
func (c *Cache) Reset() {
c.mu.Lock()
c.backend = make(map[string]*CacheItem, 10240)
c.mu.Unlock()
}
// Exists cache is exists
func (c *Cache) Exists(key string) bool {
c.mu.RLock()
_, ok := c.backend[key]
c.mu.RUnlock()
return ok
}
// Full cache is full
// if Maxcount is zero. the cache will never be full.
func (c *Cache) Full() bool {
return c.MaxCount > 0 && c.Length() >= c.MaxCount
}
// Length cache length
func (c *Cache) Length() int {
c.mu.RLock()
defer c.mu.RUnlock()
return len(c.backend)
}