mirror of
https://github.com/yusing/godoxy.git
synced 2025-07-16 10:14:04 +02:00
fix(acl): caching logic
This commit is contained in:
parent
f1abb745fe
commit
8f20bd3840
2 changed files with 2 additions and 4 deletions
|
@ -3,11 +3,9 @@ package acl
|
||||||
import (
|
import (
|
||||||
"github.com/puzpuzpuz/xsync/v3"
|
"github.com/puzpuzpuz/xsync/v3"
|
||||||
acl "github.com/yusing/go-proxy/internal/acl/types"
|
acl "github.com/yusing/go-proxy/internal/acl/types"
|
||||||
"go.uber.org/atomic"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var cityCache = xsync.NewMapOf[string, *acl.City]()
|
var cityCache = xsync.NewMapOf[string, *acl.City]()
|
||||||
var numCachedLookup atomic.Uint64
|
|
||||||
|
|
||||||
func (cfg *MaxMindConfig) lookupCity(ip *acl.IPInfo) (*acl.City, bool) {
|
func (cfg *MaxMindConfig) lookupCity(ip *acl.IPInfo) (*acl.City, bool) {
|
||||||
if ip.City != nil {
|
if ip.City != nil {
|
||||||
|
@ -20,7 +18,7 @@ func (cfg *MaxMindConfig) lookupCity(ip *acl.IPInfo) (*acl.City, bool) {
|
||||||
|
|
||||||
city, ok := cityCache.Load(ip.Str)
|
city, ok := cityCache.Load(ip.Str)
|
||||||
if ok {
|
if ok {
|
||||||
numCachedLookup.Inc()
|
ip.City = city
|
||||||
return city, true
|
return city, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,7 +63,7 @@ type checkCache struct {
|
||||||
const cacheTTL = 1 * time.Minute
|
const cacheTTL = 1 * time.Minute
|
||||||
|
|
||||||
func (c *checkCache) Expired() bool {
|
func (c *checkCache) Expired() bool {
|
||||||
return c.created.Add(cacheTTL).After(utils.TimeNow())
|
return c.created.Add(cacheTTL).Before(utils.TimeNow())
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: add stats
|
//TODO: add stats
|
||||||
|
|
Loading…
Add table
Reference in a new issue