From: Bert Hubert Date: Mon, 30 Aug 2010 20:03:55 +0000 (+0000) Subject: unify existing cache cleaning algorithms into one bit of code. Next up: the negcache X-Git-Tag: rec-3.3~20 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=38c9ceaa257f469d776a716b4d91eb00ec8d2491;p=pdns unify existing cache cleaning algorithms into one bit of code. Next up: the negcache git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@1702 d19b8d6e-7fed-0310-83ef-9ca221ded41b --- diff --git a/pdns/cachecleaner.hh b/pdns/cachecleaner.hh new file mode 100644 index 000000000..af2e82a0a --- /dev/null +++ b/pdns/cachecleaner.hh @@ -0,0 +1,58 @@ +#ifndef PDNS_CACHECLEANER_HH +#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 +template void pruneCollection(T& collection, unsigned int maxCached) +{ + uint32_t now=(uint32_t)time(0); + unsigned int toTrim=0; + + unsigned int cacheSize=collection.size(); + + if(maxCached && cacheSize > maxCached) { + toTrim = cacheSize - maxCached; + } + +// cout<<"Need to trim "<::type sequence_t; + sequence_t& sidx=collection.get<1>(); + + unsigned int tried=0, lookAt, erased=0; + + // two modes - if toTrim is 0, just look through 10000 records and nuke everything that is expired + // otherwise, scan first 5*toTrim records, and stop once we've nuked enough + if(toTrim) + lookAt=5*toTrim; + else + lookAt=cacheSize/1000; + + typename sequence_t::iterator iter=sidx.begin(), eiter; + for(; iter != sidx.end() && tried < lookAt ; ++tried) { + if(iter->getTTD() < now) { + sidx.erase(iter++); + erased++; + } + else + ++iter; + + if(toTrim && erased > toTrim) + break; + } + + //cout<<"erased "<= toTrim) // done + return; + + toTrim -= erased; + + //if(toTrim) + // cout<<"Still have "< + #include #include "recpacketcache.hh" #include "utility.hh" @@ -35,6 +36,7 @@ #include #include #include "recursor_cache.hh" +#include "cachecleaner.hh" #include #include #include diff --git a/pdns/recpacketcache.cc b/pdns/recpacketcache.cc index 99809297c..dd2ee75e3 100644 --- a/pdns/recpacketcache.cc +++ b/pdns/recpacketcache.cc @@ -1,5 +1,6 @@ #include #include "recpacketcache.hh" +#include "cachecleaner.hh" #include "dns.hh" #include "namespaces.hh" #include "lock.hh" @@ -65,59 +66,8 @@ uint64_t RecursorPacketCache::size() return d_packetCache.size(); } -// this code is almost a copy of the one in recursor_cache.cc void RecursorPacketCache::doPruneTo(unsigned int maxCached) { - uint32_t now=(uint32_t)time(0); - unsigned int toTrim=0; - - unsigned int cacheSize=d_packetCache.size(); - - if(maxCached && cacheSize > maxCached) { - toTrim = cacheSize - maxCached; - } - -// cout<<"Need to trim "<::type sequence_t; - sequence_t& sidx=d_packetCache.get<1>(); - - unsigned int tried=0, lookAt, erased=0; - - // two modes - if toTrim is 0, just look through 10000 records and nuke everything that is expired - // otherwise, scan first 5*toTrim records, and stop once we've nuked enough - if(toTrim) - lookAt=5*toTrim; - else - lookAt=cacheSize/1000; - - - sequence_t::iterator iter=sidx.begin(), eiter; - for(; iter != sidx.end() && tried < lookAt ; ++tried) { - if(iter->d_ttd < now) { - sidx.erase(iter++); - erased++; - } - else - ++iter; - - if(toTrim && erased > toTrim) - break; - } - - //cout<<"erased "<= toTrim) // done - return; - - - toTrim -= erased; - - //if(toTrim) - // cout<<"Still have "< maxCached) { - toTrim = cacheSize - maxCached; - } - - // cout<<"Need to trim "<::type sequence_t; - sequence_t& sidx=d_cache.get<1>(); - - unsigned int tried=0, lookAt, erased=0; - - // two modes - if toTrim is 0, just look through 0.1% of all records and nuke everything that is expired - // otherwise, scan first 5*toTrim records, and stop once we've nuked enough - if(toTrim) - lookAt=5*toTrim; - else - lookAt=cacheSize/1000; - - sequence_t::iterator iter=sidx.begin(), eiter; - for(; iter != sidx.end() && tried < lookAt ; ++tried) { - unsigned int ttd=iter->getTTD(); - if(ttd < now) { - sidx.erase(iter++); - erased++; - } - else - ++iter; - - if(toTrim && erased > toTrim) - break; - } - - // cout<<"erased "<= toTrim) - return; - - // if(toTrim) - // cout<<"Still have "<