root/trunk/pdns/pdns/recpacketcache.cc @ 1456

Revision 1456, 1.2 KB (checked in by ahu, 3 years ago)

slight speedup of the recursor packetcache (inline comparison function)

Line 
1#include <iostream>
2#include "recpacketcache.hh"
3#include "dns.hh"
4#include "namespaces.hh"
5#include "lock.hh"
6
7
8RecursorPacketCache::RecursorPacketCache()
9{
10  pthread_rwlock_init(&d_rwlock, 0);
11}
12
13bool RecursorPacketCache::getResponsePacket(const std::string& queryPacket, time_t now, std::string* responsePacket)
14{
15  TryReadLock l(&d_rwlock);
16  if(!l.gotIt())
17    return false; 
18
19  struct Entry e;
20  e.d_packet=queryPacket;
21 
22  packetCache_t::const_iterator iter = d_packetCache.find(e);
23  if(iter != d_packetCache.end() && (uint32_t)now < iter->d_ttd) {
24    uint16_t id = ((struct dnsheader*)queryPacket.c_str())->id;
25    *responsePacket = iter->d_packet;
26    ((struct dnsheader*)responsePacket->c_str())->id=id;
27    return true;
28  }
29  return false;
30}
31
32void RecursorPacketCache::insertResponsePacket(const std::string& responsePacket, time_t now, uint32_t ttl)
33{
34  TryWriteLock l(&d_rwlock);
35  if(!l.gotIt())
36    return;
37   
38  struct Entry e;
39  e.d_packet = responsePacket;
40  e.d_ttd = now+ttl;
41  packetCache_t::iterator iter = d_packetCache.find(e);
42  if(iter != d_packetCache.end()) {
43    iter->d_packet = responsePacket;
44    iter->d_ttd = now +ttl;
45  }
46  else 
47    d_packetCache.insert(e);
48}
49
Note: See TracBrowser for help on using the browser.