root/trunk/pdns/pdns/packetcache.hh @ 1209

Revision 1209, 3.0 KB (checked in by ahu, 5 years ago)

initial cleanups for performance boost of packet cache

  • Property svn:eol-style set to native
  • Property svn:keywords set to author date id revision
Line 
1/*
2    PowerDNS Versatile Database Driven Nameserver
3    Copyright (C) 2002 - 2008  PowerDNS.COM BV
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License version 2
7    as published by the Free Software Foundation
8   
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18*/
19#ifndef PACKETCACHE_HH
20#define PACKETCACHE_HH
21
22#include <string>
23#include <utility>
24#include <map>
25
26#ifndef WIN32
27# if __GNUC__ >= 3
28#   include <ext/hash_map>
29using namespace __gnu_cxx;
30# else
31#   include <hash_map>
32# endif // __GNUC__
33
34#else
35# include <map>
36
37#endif // WIN32
38
39using namespace std;
40
41#include "dnspacket.hh"
42#include "lock.hh"
43#include "statbag.hh"
44
45/** This class performs 'whole packet caching'. Feed it a question packet and it will
46    try to find an answer. If you have an answer, insert it to have it cached for later use.
47    Take care not to replace existing cache entries. While this works, it is wasteful. Only
48    insert packets that where not found by get()
49
50    Locking!
51
52    The cache itself is protected by a read/write lock. Because deleting is a two step process, which
53    first marks and then sweeps, a second lock is present to prevent simultaneous inserts and deletes.
54
55    Overloading!
56
57    The packet cache contains packets but also negative UeberBackend queries. Those last are recognized
58    because they start with a | and have empty content. One day, this content may also contain queries.
59
60*/
61class PacketCache
62{
63public:
64  PacketCache();
65  void insert(DNSPacket *q, DNSPacket *r);  //!< We copy the contents of *p into our cache. Do not needlessly call this to insert questions already in the cache as it wastes resources
66
67  void insert(const string &key, const string &packet, unsigned int ttl);
68
69  int get(DNSPacket *p, DNSPacket *q); //!< We return a dynamically allocated copy out of our cache. You need to delete it. You also need to spoof in the right ID with the DNSPacket.spoofID() method.
70  bool getKey(const string &key, string &content);
71  int size(); //!< number of entries in the cache
72  void cleanup(); //!< force the cache to preen itself from expired packets
73  int purge(const string &prefix="");
74
75  map<char,int> getCounts();
76private:
77  typedef string ckey_t;
78
79  class CacheContent
80  {
81  public:
82    time_t ttd;
83    string value;
84  };
85
86  typedef CacheContent cvalue_t;
87  void getTTLS();
88  typedef map< ckey_t, cvalue_t > cmap_t;
89
90  cmap_t d_map;
91
92  pthread_rwlock_t d_mut;
93  pthread_mutex_t d_dellock;
94
95  int d_hit;
96  int d_miss;
97  int d_ttl;
98  int d_recursivettl;
99  bool d_doRecursion;
100  int *statnumhit;
101  int *statnummiss;
102  int *statnumentries;
103};
104
105
106
107#endif /* PACKETCACHE_HH */
108
Note: See TracBrowser for help on using the browser.