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

Revision 1218, 3.9 KB (checked in by ahu, 5 years ago)

the actual new and improved packetcache - but seriously unfinished! The core is there though.

  • 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#include <map>
26
27#include <boost/multi_index_container.hpp>
28#include <boost/multi_index/ordered_index.hpp>
29#include <boost/tuple/tuple_comparison.hpp>
30#include <boost/multi_index/key_extractors.hpp>
31#include <boost/multi_index/sequenced_index.hpp>
32#include <boost/version.hpp>
33using namespace std;
34using namespace ::boost::multi_index;
35
36using namespace boost;
37#include "dnspacket.hh"
38#include "lock.hh"
39#include "statbag.hh"
40
41/** This class performs 'whole packet caching'. Feed it a question packet and it will
42    try to find an answer. If you have an answer, insert it to have it cached for later use.
43    Take care not to replace existing cache entries. While this works, it is wasteful. Only
44    insert packets that where not found by get()
45
46    Locking!
47
48    The cache itself is protected by a read/write lock. Because deleting is a two step process, which
49    first marks and then sweeps, a second lock is present to prevent simultaneous inserts and deletes.
50*/
51
52class PacketCache
53{
54public:
55  PacketCache();
56  enum CacheEntryType { PACKETCACHE, QUERYCACHE, NEGCACHE};
57
58
59  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
60
61  void insert(const string &qname, const QType& qtype, CacheEntryType cet, const string& value, unsigned int ttl, int zoneID=-1, bool meritsRecursion=false);
62
63  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.
64  bool getEntry(const string &content, const QType& qtype, CacheEntryType cet, string& entry, int zoneID=-1, bool meritsRecursion=false);
65
66  int size(); //!< number of entries in the cache
67  void cleanup(); //!< force the cache to preen itself from expired packets
68  int purge(const string &prefix="");
69
70  map<char,int> getCounts();
71private:
72  struct CacheEntry
73  {
74    CacheEntry() { qtype = ctype = 0; zoneID = -1; meritsRecursion=false;}
75
76    string qname;
77    uint16_t qtype;
78    uint16_t ctype;
79    int zoneID;
80    time_t ttd;
81    bool meritsRecursion;
82    string value;
83  };
84
85  void getTTLS();
86
87  typedef multi_index_container<
88    CacheEntry,
89    indexed_by <
90                ordered_unique<
91                      composite_key< 
92                        CacheEntry,
93                        member<CacheEntry,string,&CacheEntry::qname>,
94                        member<CacheEntry,uint16_t,&CacheEntry::qtype>,
95                        member<CacheEntry,uint16_t, &CacheEntry::ctype>,
96                        member<CacheEntry,int, &CacheEntry::zoneID>,
97                        member<CacheEntry,bool, &CacheEntry::meritsRecursion>
98                      >,
99                  composite_key_compare<CIStringCompare, std::less<uint16_t>, std::less<uint16_t>, std::less<int>, std::less<bool> >
100                >,
101               sequenced<>
102               >
103  > cmap_t;
104
105
106  cmap_t d_map;
107
108  pthread_rwlock_t d_mut;
109  pthread_mutex_t d_dellock;
110
111  int d_hit;
112  int d_miss;
113  int d_ttl;
114  int d_recursivettl;
115  bool d_doRecursion;
116  int *statnumhit;
117  int *statnummiss;
118  int *statnumentries;
119};
120
121
122
123#endif /* PACKETCACHE_HH */
124
Note: See TracBrowser for help on using the browser.