* `getCache()`: return the current packet cache, if any
* `setCache(PacketCache)`: set the cache for this pool
* PacketCache related:
- * `expungeByName(DNSName)`: remove entries matching the supplied DNSName from the cache
+ * `expunge(n)`: remove entries from the cache, leaving at most `n` entries
+ * `expungeByName(DNSName [, qtype=ANY])`: remove entries matching the supplied DNSName and type from the cache
* `isFull()`: return true if the cache has reached the maximum number of entries
* `newPacketCache(maxEntries, maxTTL=86400, minTTL=60)`: return a new PacketCache
* `printStats()`: print the cache stats (hits, misses, deferred lookups and deferred inserts)
- * `purge()`: remove entries from the cache until it the number of entries is lower than the maximum, starting with expired ones.
+ * `purgeExpired(n)`: remove expired entries from the cache until there is at most `n` entries remaining in the cache
* `toString()`: return the number of entries in the Packet Cache, and the maximum number of entries
* Advanced functions for writing your own policies and hooks
* ComboAddress related:
return true;
}
-void DNSDistPacketCache::purge(size_t upTo)
+/* Remove expired entries, until the cache has at most
+ upTo entries in it.
+*/
+void DNSDistPacketCache::purgeExpired(size_t upTo)
{
time_t now = time(NULL);
WriteLock w(&d_lock);
- if (upTo <= d_map.size())
+ if (upTo >= d_map.size()) {
return;
+ }
size_t toRemove = d_map.size() - upTo;
for(auto it = d_map.begin(); toRemove > 0 && it != d_map.end(); ) {
}
}
-void DNSDistPacketCache::expunge(const DNSName& name, uint16_t qtype)
+/* Remove all entries, keeping only upTo
+ entries in the cache */
+void DNSDistPacketCache::expunge(size_t upTo)
+{
+ WriteLock w(&d_lock);
+
+ if (upTo >= d_map.size()) {
+ return;
+ }
+
+ size_t toRemove = d_map.size() - upTo;
+ auto beginIt = d_map.begin();
+ auto endIt = beginIt;
+ std::advance(endIt, toRemove);
+ d_map.erase(beginIt, endIt);
+}
+
+void DNSDistPacketCache::expungeByName(const DNSName& name, uint16_t qtype)
{
WriteLock w(&d_lock);
void insert(uint32_t key, const DNSName& qname, uint16_t qtype, uint16_t qclass, const char* response, uint16_t responseLen, bool tcp);
bool get(const unsigned char* query, uint16_t queryLen, const DNSName& qname, uint16_t qtype, uint16_t qclass, uint16_t consumed, uint16_t queryId, char* response, uint16_t* responseLen, bool tcp, uint32_t* keyOut, bool skipAging=false);
- void purge(size_t upTo=0);
- void expunge(const DNSName& name, uint16_t qtype=QType::ANY);
+ void purgeExpired(size_t upTo=0);
+ void expunge(size_t upTo=0);
+ void expungeByName(const DNSName& name, uint16_t qtype=QType::ANY);
bool isFull();
string toString();
uint64_t getSize() const { return d_map.size(); };
});
g_lua.registerFunction("toString", &DNSDistPacketCache::toString);
g_lua.registerFunction("isFull", &DNSDistPacketCache::isFull);
- g_lua.registerFunction("purge", &DNSDistPacketCache::purge);
+ g_lua.registerFunction("purgeExpired", &DNSDistPacketCache::purgeExpired);
+ g_lua.registerFunction("expunge", &DNSDistPacketCache::expunge);
g_lua.registerFunction<void(std::shared_ptr<DNSDistPacketCache>::*)(const DNSName& dname, boost::optional<uint16_t> qtype)>("expungeByName", [](std::shared_ptr<DNSDistPacketCache> cache, const DNSName& dname, boost::optional<uint16_t> qtype) {
- cache->expunge(dname, qtype ? *qtype : QType::ANY);
+ cache->expungeByName(dname, qtype ? *qtype : QType::ANY);
});
g_lua.registerFunction<void(std::shared_ptr<DNSDistPacketCache>::*)()>("printStats", [](const std::shared_ptr<DNSDistPacketCache> cache) {
g_outputBuffer="Hits: " + std::to_string(cache->getHits()) + "\n";
packetCache = entry.second->packetCache;
}
if (packetCache) {
- packetCache->purge();
+ packetCache->purgeExpired();
}
}
counter = 0;
uint32_t key = 0;
bool found = PC.get((const unsigned char*) query.data(), query.size(), a, QType::A, QClass::IN, a.wirelength(), 0, responseBuf, &responseBufSize, false, &key);
if (found == true) {
- PC.expunge(a);
+ PC.expungeByName(a);
deleted++;
}
}