Skip to content

Commit

Permalink
improved LRU cache operations (avoid write lock on TotalCount)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xERR0R committed Jun 2, 2022
1 parent a120aaf commit fd8c61c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions cache/expirationcache/expiration_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (e *ExpiringLRUCache) cleanUp() {

// check for expired items and collect expired keys
for _, k := range e.lru.Keys() {
if v, ok := e.lru.Get(k); ok {
if v, ok := e.lru.Peek(k); ok {
if isExpired(v.(*element)) {
expiredKeys = append(expiredKeys, k.(string))
}
Expand Down Expand Up @@ -148,7 +148,7 @@ func calculateRemainTTL(expiresEpoch int64) time.Duration {

func (e *ExpiringLRUCache) TotalCount() (count int) {
for _, k := range e.lru.Keys() {
if v, ok := e.lru.Get(k); ok {
if v, ok := e.lru.Peek(k); ok {
if !isExpired(v.(*element)) {
count++
}
Expand Down
6 changes: 4 additions & 2 deletions resolver/caching_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,11 @@ func (r *CachingResolver) trackQueryDomainNameCount(domain string, cacheKey stri
}
domainCount++
r.prefetchingNameCache.Put(cacheKey, domainCount, r.prefetchExpires)
totalCount := r.prefetchingNameCache.TotalCount()

logger.Debugf("domain '%s' was requested %d times, "+
"total cache size: %d", util.Obfuscate(domain), domainCount, r.prefetchingNameCache.TotalCount())
evt.Bus().Publish(evt.CachingDomainsToPrefetchCountChanged, r.prefetchingNameCache.TotalCount())
"total cache size: %d", util.Obfuscate(domain), domainCount, totalCount)
evt.Bus().Publish(evt.CachingDomainsToPrefetchCountChanged, totalCount)
}
}

Expand Down

0 comments on commit fd8c61c

Please sign in to comment.