Skip to content

Commit

Permalink
refactor: Replace common/cache with freelru
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Nov 14, 2024
1 parent 8a783bb commit 0a21cc9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
43 changes: 24 additions & 19 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"time"

"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/cache"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/common/task"
"github.com/sagernet/sing/contrab/freelru"
"github.com/sagernet/sing/contrab/maphash"

"github.com/miekg/dns"
)
Expand All @@ -37,8 +38,8 @@ type Client struct {
rdrc RDRCStore
initRDRCFunc func() RDRCStore
logger logger.ContextLogger
cache *cache.LruCache[dns.Question, *dns.Msg]
transportCache *cache.LruCache[transportCacheKey, *dns.Msg]
cache freelru.Cache[dns.Question, *dns.Msg]
transportCache freelru.Cache[transportCacheKey, *dns.Msg]
}

type RDRCStore interface {
Expand All @@ -57,6 +58,7 @@ type ClientOptions struct {
DisableCache bool
DisableExpire bool
IndependentCache bool
CacheCapacity uint32
RDRC func() RDRCStore
Logger logger.ContextLogger
}
Expand All @@ -73,11 +75,15 @@ func NewClient(options ClientOptions) *Client {
if client.timeout == 0 {
client.timeout = DefaultTimeout
}
cacheCapacity := options.CacheCapacity
if cacheCapacity < 1024 {
cacheCapacity = 1024
}
if !client.disableCache {
if !client.independentCache {
client.cache = cache.New[dns.Question, *dns.Msg]()
client.cache = common.Must1(freelru.NewSharded[dns.Question, *dns.Msg](cacheCapacity, maphash.NewHasher[dns.Question]().Hash32))
} else {
client.transportCache = cache.New[transportCacheKey, *dns.Msg]()
client.transportCache = common.Must1(freelru.NewSharded[transportCacheKey, *dns.Msg](cacheCapacity, maphash.NewHasher[transportCacheKey]().Hash32))
}
}
return client
Expand Down Expand Up @@ -395,10 +401,10 @@ func (c *Client) LookupWithResponseCheck(ctx context.Context, transport Transpor

func (c *Client) ClearCache() {
if c.cache != nil {
c.cache.Clear()
c.cache.Purge()
}
if c.transportCache != nil {
c.transportCache.Clear()
c.transportCache.Purge()
}
}

Expand Down Expand Up @@ -474,23 +480,22 @@ func (c *Client) storeCache(transport Transport, question dns.Question, message
}
if c.disableExpire {
if !c.independentCache {
c.cache.Store(question, message)
c.cache.Add(question, message)
} else {
c.transportCache.Store(transportCacheKey{
c.transportCache.Add(transportCacheKey{
Question: question,
transportName: transport.Name(),
}, message)
}
return
}
expireAt := time.Now().Add(time.Second * time.Duration(timeToLive))
if !c.independentCache {
c.cache.StoreWithExpire(question, message, expireAt)
c.cache.AddWithLifetime(question, message, time.Second*time.Duration(timeToLive))
} else {
c.transportCache.StoreWithExpire(transportCacheKey{
c.transportCache.AddWithLifetime(transportCacheKey{
Question: question,
transportName: transport.Name(),
}, message, expireAt)
}, message, time.Second*time.Duration(timeToLive))
}
}

Expand Down Expand Up @@ -569,9 +574,9 @@ func (c *Client) loadResponse(question dns.Question, transport Transport) (*dns.
)
if c.disableExpire {
if !c.independentCache {
response, loaded = c.cache.Load(question)
response, loaded = c.cache.Get(question)
} else {
response, loaded = c.transportCache.Load(transportCacheKey{
response, loaded = c.transportCache.Get(transportCacheKey{
Question: question,
transportName: transport.Name(),
})
Expand All @@ -583,9 +588,9 @@ func (c *Client) loadResponse(question dns.Question, transport Transport) (*dns.
} else {
var expireAt time.Time
if !c.independentCache {
response, expireAt, loaded = c.cache.LoadWithExpire(question)
response, expireAt, loaded = c.cache.GetWithLifetime(question)
} else {
response, expireAt, loaded = c.transportCache.LoadWithExpire(transportCacheKey{
response, expireAt, loaded = c.transportCache.GetWithLifetime(transportCacheKey{
Question: question,
transportName: transport.Name(),
})
Expand All @@ -596,9 +601,9 @@ func (c *Client) loadResponse(question dns.Question, transport Transport) (*dns.
timeNow := time.Now()
if timeNow.After(expireAt) {
if !c.independentCache {
c.cache.Delete(question)
c.cache.Remove(question)
} else {
c.transportCache.Delete(transportCacheKey{
c.transportCache.Remove(transportCacheKey{
Question: question,
transportName: transport.Name(),
})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/miekg/dns v1.1.62
github.com/sagernet/quic-go v0.47.0-beta.2
github.com/sagernet/sing v0.5.0
github.com/sagernet/sing v0.6.0-alpha.10
github.com/stretchr/testify v1.9.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ github.com/quic-go/qtls-go1-20 v0.4.1 h1:D33340mCNDAIKBqXuAvexTNMUByrYmFYVfKfDN5
github.com/quic-go/qtls-go1-20 v0.4.1/go.mod h1:X9Nh97ZL80Z+bX/gUXMbipO6OxdiDi58b/fMC9mAL+k=
github.com/sagernet/quic-go v0.47.0-beta.2 h1:1tCGWFOSaXIeuQaHrwOMJIYvlupjTcaVInGQw5ArULU=
github.com/sagernet/quic-go v0.47.0-beta.2/go.mod h1:bLVKvElSEMNv7pu7SZHscW02TYigzQ5lQu3Nh4wNh8Q=
github.com/sagernet/sing v0.5.0 h1:soo2wVwLcieKWWKIksFNK6CCAojUgAppqQVwyRYGkEM=
github.com/sagernet/sing v0.5.0/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
github.com/sagernet/sing v0.6.0-alpha.10 h1:gIUiFof6SDDcAg3m3pUOshOPZBLC7z9VCgDt4Tzs24g=
github.com/sagernet/sing v0.6.0-alpha.10/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down

0 comments on commit 0a21cc9

Please sign in to comment.