From cf98aa404df9a4d1d721f8bda42c6b8fdc58fdfa Mon Sep 17 00:00:00 2001 From: Bert Hubert Date: Fri, 31 Mar 2006 06:46:58 +0000 Subject: [PATCH] implement very simple cache cache that catches repeated nearly identical lookups appears to have lowered cpu load by 40% git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@646 d19b8d6e-7fed-0310-83ef-9ca221ded41b --- pdns/recursor_cache.cc | 35 ++++++++++++++++++++++++----------- pdns/recursor_cache.hh | 6 +++++- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/pdns/recursor_cache.cc b/pdns/recursor_cache.cc index 686f0e541..9486f1965 100644 --- a/pdns/recursor_cache.cc +++ b/pdns/recursor_cache.cc @@ -78,22 +78,34 @@ unsigned int MemRecursorCache::bytes() int MemRecursorCache::get(time_t now, const string &qname, const QType& qt, set* res) { unsigned int ttd=0; - tuple key=make_tuple(toLowerCanonic(qname), qt.getCode()); + string lqname(toLowerCanonic(qname)); + + // cerr<<"looking up "<< toLowerCanonic(qname)+"|"+qt.getName()<<"\n"; + + if(!d_cachecachevalid || d_cachedqname != lqname) { + // cerr<<"had cache cache miss"<clear(); - if(j!=d_cache.end()) { + if(d_cachecache.first!=d_cachecache.second) { if(res) { - for(vector::const_iterator k=j->d_records.begin(); k != j->d_records.end(); ++k) { - if(k->d_ttd > (uint32_t) now) { - DNSResourceRecord rr=String2DNSRR(qname, qt, k->d_string, ttd=k->d_ttd); - res->insert(rr); - } - } + for(cache_t::const_iterator i=d_cachecache.first; i != d_cachecache.second; ++i) + if(i->d_qtype == qt.getCode()) + for(vector::const_iterator k=i->d_records.begin(); k != i->d_records.end(); ++k) { + if(k->d_ttd > (uint32_t) now) { + DNSResourceRecord rr=String2DNSRR(qname, qt, k->d_string, ttd=k->d_ttd); + res->insert(rr); + } + } } // cerr<<"time left : "<size() : 0) <<"\n"; @@ -108,6 +120,7 @@ int MemRecursorCache::get(time_t now, const string &qname, const QType& qt, set< touched, but only given a new ttd */ void MemRecursorCache::replace(const string &qname, const QType& qt, const set& content) { + d_cachecachevalid=false; tuple key=make_tuple(toLowerCanonic(qname), qt.getCode()); cache_t::iterator stored=d_cache.find(key); @@ -179,7 +192,7 @@ void MemRecursorCache::doDumpAndClose(int fd) void MemRecursorCache::doPrune(void) { uint32_t now=(uint32_t)time(0); - + d_cachecachevalid=false; // cout<<"Going to prune!\n"; typedef cache_t::nth_index<1>::type cache_by_ttd_t; diff --git a/pdns/recursor_cache.hh b/pdns/recursor_cache.hh index 4d63fef9f..0df92d364 100644 --- a/pdns/recursor_cache.hh +++ b/pdns/recursor_cache.hh @@ -24,6 +24,8 @@ using namespace ::boost::multi_index; class MemRecursorCache : public boost::noncopyable // : public RecursorCache { public: + MemRecursorCache() : d_cachecachevalid(false) + {} unsigned int size(); unsigned int bytes(); int get(time_t, const string &qname, const QType& qt, set* res); @@ -106,7 +108,9 @@ private: private: cache_t d_cache; - + pair d_cachecache; + string d_cachedqname; + bool d_cachecachevalid; }; -- 2.49.0