1 Star 0 Fork 0

朽木木/gost-x

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cache.go 2.36 KB
一键复制 编辑 原始数据 按行查看 历史
hao_shen 提交于 2025-03-25 16:26 +08:00 . 2
package resolver
import (
"context"
"fmt"
"sync"
"time"
"gitee.com/hao_shen/gost-core/logger"
ctxvalue "gitee.com/hao_shen/gost-x/ctx"
"github.com/miekg/dns"
)
const (
defaultTTL = 60 * time.Second
)
type CacheKey string
// NewCacheKey generates resolver cache key from question of dns query.
func NewCacheKey(q *dns.Question) CacheKey {
if q == nil {
return ""
}
key := fmt.Sprintf("%s%s.%s", q.Name, dns.Class(q.Qclass).String(), dns.Type(q.Qtype).String())
return CacheKey(key)
}
type cacheItem struct {
msg *dns.Msg
ts time.Time
ttl time.Duration
}
type Cache struct {
m sync.Map
logger logger.Logger
}
func NewCache() *Cache {
return &Cache{}
}
func (c *Cache) WithLogger(logger logger.Logger) *Cache {
c.logger = logger
return c
}
func (c *Cache) Load(ctx context.Context, key CacheKey) (msg *dns.Msg, ttl time.Duration) {
v, ok := c.m.Load(key)
if !ok {
return
}
item, ok := v.(*cacheItem)
if !ok {
return
}
msg = item.msg.Copy()
for i := range msg.Answer {
d := uint32(time.Since(item.ts).Seconds())
if msg.Answer[i].Header().Ttl > d {
msg.Answer[i].Header().Ttl -= d
} else {
msg.Answer[i].Header().Ttl = 1
}
}
ttl = item.ttl - time.Since(item.ts)
if log := c.logger; log.IsLevelEnabled(logger.DebugLevel) {
if sid := ctxvalue.SidFromContext(ctx); sid != "" {
log = log.WithFields(map[string]any{
"sid": sid,
})
}
log.Debugf("resolver cache hit: %s, ttl: %v", key, ttl)
}
return
}
func (c *Cache) Store(ctx context.Context, key CacheKey, mr *dns.Msg, ttl time.Duration) {
if key == "" || mr == nil || ttl < 0 {
return
}
if ttl == 0 {
for _, answer := range mr.Answer {
v := time.Duration(answer.Header().Ttl) * time.Second
if ttl == 0 || ttl > v {
ttl = v
}
}
if ttl == 0 {
ttl = defaultTTL
}
} else {
for i := range mr.Answer {
mr.Answer[i].Header().Ttl = uint32(ttl.Seconds())
}
}
c.m.Store(key, &cacheItem{
msg: mr.Copy(),
ts: time.Now(),
ttl: ttl,
})
if log := c.logger; log.IsLevelEnabled(logger.DebugLevel) {
if sid := ctxvalue.SidFromContext(ctx); sid != "" {
log = log.WithFields(map[string]any{
"sid": sid,
})
}
log.Debugf("resolver cache store: %s, ttl: %v", key, ttl)
}
}
func (c *Cache) RefreshTTL(key CacheKey) {
v, ok := c.m.Load(key)
if !ok {
return
}
item, ok := v.(*cacheItem)
if !ok {
return
}
item.ts = time.Now()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hao_shen/gost-x.git
git@gitee.com:hao_shen/gost-x.git
hao_shen
gost-x
gost-x
v1.1.1

搜索帮助