Refactor: lrucache use generics
This commit is contained in:
@ -11,7 +11,7 @@ import (
|
||||
type ResolverEnhancer struct {
|
||||
mode C.DNSMode
|
||||
fakePool *fakeip.Pool
|
||||
mapping *cache.LruCache
|
||||
mapping *cache.LruCache[string, string]
|
||||
}
|
||||
|
||||
func (h *ResolverEnhancer) FakeIPEnabled() bool {
|
||||
@ -67,7 +67,7 @@ func (h *ResolverEnhancer) FindHostByIP(ip net.IP) (string, bool) {
|
||||
|
||||
if mapping := h.mapping; mapping != nil {
|
||||
if host, existed := h.mapping.Get(ip.String()); existed {
|
||||
return host.(string), true
|
||||
return host, true
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,11 +99,11 @@ func (h *ResolverEnhancer) FlushFakeIP() error {
|
||||
|
||||
func NewEnhancer(cfg Config) *ResolverEnhancer {
|
||||
var fakePool *fakeip.Pool
|
||||
var mapping *cache.LruCache
|
||||
var mapping *cache.LruCache[string, string]
|
||||
|
||||
if cfg.EnhancedMode != C.DNSNormal {
|
||||
fakePool = cfg.Pool
|
||||
mapping = cache.NewLRUCache(cache.WithSize(4096), cache.WithStale(true))
|
||||
mapping = cache.NewLRUCache[string, string](cache.WithSize[string, string](4096), cache.WithStale[string, string](true))
|
||||
}
|
||||
|
||||
return &ResolverEnhancer{
|
||||
|
@ -63,7 +63,7 @@ func withHosts(hosts *trie.DomainTrie) middleware {
|
||||
}
|
||||
}
|
||||
|
||||
func withMapping(mapping *cache.LruCache) middleware {
|
||||
func withMapping(mapping *cache.LruCache[string, string]) middleware {
|
||||
return func(next handler) handler {
|
||||
return func(ctx *context.DNSContext, r *D.Msg) (*D.Msg, error) {
|
||||
q := r.Question[0]
|
||||
|
@ -39,7 +39,7 @@ type Resolver struct {
|
||||
fallbackDomainFilters []fallbackDomainFilter
|
||||
fallbackIPFilters []fallbackIPFilter
|
||||
group singleflight.Group
|
||||
lruCache *cache.LruCache
|
||||
lruCache *cache.LruCache[string, *D.Msg]
|
||||
policy *trie.DomainTrie
|
||||
proxyServer []dnsClient
|
||||
}
|
||||
@ -103,7 +103,7 @@ func (r *Resolver) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, e
|
||||
cache, expireTime, hit := r.lruCache.GetWithExpire(q.String())
|
||||
if hit {
|
||||
now := time.Now()
|
||||
msg = cache.(*D.Msg).Copy()
|
||||
msg = cache.Copy()
|
||||
if expireTime.Before(now) {
|
||||
setMsgTTL(msg, uint32(1)) // Continue fetch
|
||||
go r.exchangeWithoutCache(ctx, m)
|
||||
@ -336,13 +336,13 @@ type Config struct {
|
||||
func NewResolver(config Config) *Resolver {
|
||||
defaultResolver := &Resolver{
|
||||
main: transform(config.Default, nil),
|
||||
lruCache: cache.NewLRUCache(cache.WithSize(4096), cache.WithStale(true)),
|
||||
lruCache: cache.NewLRUCache[string, *D.Msg](cache.WithSize[string, *D.Msg](4096), cache.WithStale[string, *D.Msg](true)),
|
||||
}
|
||||
|
||||
r := &Resolver{
|
||||
ipv6: config.IPv6,
|
||||
main: transform(config.Main, defaultResolver),
|
||||
lruCache: cache.NewLRUCache(cache.WithSize(4096), cache.WithStale(true)),
|
||||
lruCache: cache.NewLRUCache[string, *D.Msg](cache.WithSize[string, *D.Msg](4096), cache.WithStale[string, *D.Msg](true)),
|
||||
hosts: config.Hosts,
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@ import (
|
||||
D "github.com/miekg/dns"
|
||||
)
|
||||
|
||||
func putMsgToCache(c *cache.LruCache, key string, msg *D.Msg) {
|
||||
func putMsgToCache(c *cache.LruCache[string, *D.Msg], key string, msg *D.Msg) {
|
||||
var ttl uint32
|
||||
switch {
|
||||
case len(msg.Answer) != 0:
|
||||
|
Reference in New Issue
Block a user