From: Bert Hubert Date: Tue, 31 Aug 2010 06:49:01 +0000 (+0000) Subject: unify cache deletion queue management, and make negcache use it X-Git-Tag: rec-3.3~17 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c2567ad1f582a51eb8c7469ced4581e27b5bdd82;p=pdns unify cache deletion queue management, and make negcache use it git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@1705 d19b8d6e-7fed-0310-83ef-9ca221ded41b --- diff --git a/pdns/cachecleaner.hh b/pdns/cachecleaner.hh index af2e82a0a..defd3a4a7 100644 --- a/pdns/cachecleaner.hh +++ b/pdns/cachecleaner.hh @@ -2,6 +2,8 @@ #define PDNS_CACHECLEANER_HH // this function can clean any cache that has a getTTD() method on its entries, and a 'sequence' index as its second index +// the ritual is that the oldest entries are in *front* of the sequence collection, so on a hit, move an item to the end +// on a miss, move it to the beginning template void pruneCollection(T& collection, unsigned int maxCached) { uint32_t now=(uint32_t)time(0); @@ -55,4 +57,26 @@ template void pruneCollection(T& collection, unsigned int maxCached sidx.erase(iter, eiter); // just lob it off from the beginning } + +template void moveCacheItemToFrontOrBack(T& collection, typename T::iterator& iter, bool front) +{ + typedef typename T::template nth_index<1>::type sequence_t; + sequence_t& sidx=collection.get<1>(); + typename sequence_t::iterator si=collection.project<1>(iter); + if(front) + sidx.relocate(sidx.begin(), si); // at the beginning of the delete queue + else + sidx.relocate(sidx.end(), si); // back +} + +template void moveCacheItemToFront(T& collection, typename T::iterator& iter) +{ + moveCacheItemToFrontOrBack(collection, iter, true); +} + +template void moveCacheItemToBack(T& collection, typename T::iterator& iter) +{ + moveCacheItemToFrontOrBack(collection, iter, false); +} + #endif diff --git a/pdns/recpacketcache.cc b/pdns/recpacketcache.cc index dd2ee75e3..37a1af51c 100644 --- a/pdns/recpacketcache.cc +++ b/pdns/recpacketcache.cc @@ -23,9 +23,6 @@ bool RecursorPacketCache::getResponsePacket(const std::string& queryPacket, time d_misses++; return false; } - typedef packetCache_t::nth_index<1>::type sequence_t; - sequence_t& sidx=d_packetCache.get<1>(); - sequence_t::iterator si=d_packetCache.project<1>(iter); if((uint32_t)now < iter->d_ttd) { // it is fresh! // cerr<<"Fresh for another "<d_ttd - now<<" seconds!"<d_packet; ((struct dnsheader*)responsePacket->c_str())->id=id; d_hits++; - - sidx.relocate(sidx.end(), si); // put it at the end of the delete queue + moveCacheItemToBack(d_packetCache, iter); return true; } - sidx.relocate(sidx.begin(), si); // at the beginning of the delete queue + moveCacheItemToFront(d_packetCache, iter); d_misses++; return false; } diff --git a/pdns/recursor_cache.cc b/pdns/recursor_cache.cc index 80764e597..ff7114289 100644 --- a/pdns/recursor_cache.cc +++ b/pdns/recursor_cache.cc @@ -124,11 +124,7 @@ int MemRecursorCache::get(time_t now, const string &qname, const QType& qt, set< for(cache_t::const_iterator i=d_cachecache.first; i != d_cachecache.second; ++i) if(i->d_qtype == qt.getCode() || qt.getCode()==QType::ANY || (qt.getCode()==QType::ADDR && (i->d_qtype == QType::A || i->d_qtype == QType::AAAA) ) - ) { - typedef cache_t::nth_index<1>::type sequence_t; - sequence_t& sidx=d_cache.get<1>(); - sequence_t::iterator si=d_cache.project<1>(i); - + ) { for(vector::const_iterator k=i->d_records.begin(); k != i->d_records.end(); ++k) { if(k->d_ttd < 1000000000 || k->d_ttd > (uint32_t) now) { // FIXME what does the 100000000 number mean? ttd=k->d_ttd; @@ -140,9 +136,9 @@ int MemRecursorCache::get(time_t now, const string &qname, const QType& qt, set< } if(res) { if(res->empty()) - sidx.relocate(sidx.begin(), si); + moveCacheItemToFront(d_cache, i); else - sidx.relocate(sidx.end(), si); + moveCacheItemToBack(d_cache, i); } if(qt.getCode()!=QType::ANY && qt.getCode()!=QType::ADDR) // normally if we have a hit, we are done break; diff --git a/pdns/syncres.cc b/pdns/syncres.cc index cabb15637..22dd54433 100644 --- a/pdns/syncres.cc +++ b/pdns/syncres.cc @@ -36,6 +36,7 @@ #include "dnsparser.hh" #include "dns_random.hh" #include "lock.hh" +#include "cachecleaner.hh" __thread SyncRes::StaticStorage* t_sstorage; @@ -429,11 +430,13 @@ int SyncRes::doResolve(const string &qname, const QType &qtype, vectord_qname; sqt=QType::SOA; + moveCacheItemToBack(t_sstorage->negcache, ni); break; } else { - LOG<negcache, ni); } } }