Permalink
Browse files

rec: Move expired cache entries to the front so they are expunged

(cherry picked from commit 197d755)
  • Loading branch information...
rgacogne committed Jun 15, 2017
1 parent 0ebdaa5 commit 71df1567734bacf3e84d2279f6f21326213dc95a
Showing with 33 additions and 27 deletions.
  1. +33 −27 pdns/recursor_cache.cc
View
@@ -48,39 +48,45 @@ int MemRecursorCache::get(time_t now, const DNSName &qname, const QType& qt, vec
res->clear();
if(d_cachecache.first!=d_cachecache.second) {
for(cache_t::const_iterator i=d_cachecache.first; i != d_cachecache.second; ++i)
if(i->d_ttd > now && ((i->d_qtype == qt.getCode() || qt.getCode()==QType::ANY ||
for(cache_t::const_iterator i=d_cachecache.first; i != d_cachecache.second; ++i) {
if(i->d_ttd > now) {
if ((i->d_qtype == qt.getCode() || qt.getCode()==QType::ANY ||
(qt.getCode()==QType::ADDR && (i->d_qtype == QType::A || i->d_qtype == QType::AAAA) ))
&& (i->d_netmask.empty() || i->d_netmask.match(who)))
&& (i->d_netmask.empty() || i->d_netmask.match(who))
) {
ttd = i->d_ttd;
// cerr<<"Looking at "<<i->d_records.size()<<" records for this name"<<endl;
for(auto k=i->d_records.begin(); k != i->d_records.end(); ++k) {
if(res) {
DNSRecord dr;
dr.d_name = qname;
dr.d_type = i->d_qtype;
dr.d_class = 1;
dr.d_content = *k;
dr.d_ttl = i->d_ttd;
dr.d_place = DNSResourceRecord::ANSWER;
res->push_back(dr);
}
}
if(signatures) // if you do an ANY lookup you are hosed XXXX
*signatures=i->d_signatures;
if(res) {
if(res->empty())
moveCacheItemToFront(d_cache, i);
else
ttd = i->d_ttd;
// cerr<<"Looking at "<<i->d_records.size()<<" records for this name"<<endl;
for(auto k=i->d_records.begin(); k != i->d_records.end(); ++k) {
if(res) {
DNSRecord dr;
dr.d_name = qname;
dr.d_type = i->d_qtype;
dr.d_class = 1;
dr.d_content = *k;
dr.d_ttl = static_cast<uint32_t>(i->d_ttd);
dr.d_place = DNSResourceRecord::ANSWER;
res->push_back(dr);
}
}
if(signatures) // if you do an ANY lookup you are hosed XXXX
*signatures=i->d_signatures;
if(res && !res->empty()) {
// cache hit
moveCacheItemToBack(d_cache, i);
}
if(qt.getCode()!=QType::ANY && qt.getCode()!=QType::ADDR) // normally if we have a hit, we are done
break;
}
if(qt.getCode()!=QType::ANY && qt.getCode()!=QType::ADDR) // normally if we have a hit, we are done
break;
} else {
// expired entry
moveCacheItemToFront(d_cache, i);
}
}
// cerr<<"time left : "<<ttd - now<<", "<< (res ? res->size() : 0) <<"\n";
return (int)ttd-now;
}

0 comments on commit 71df156

Please sign in to comment.