tie(it, result) = map.insert({key, newValue});
if (result) {
- shard.d_entriesCount++;
return;
}
uint32_t shardIndex = getShardIndex(key);
- if (d_shards.at(shardIndex).d_entriesCount >= (d_maxEntries / d_shardCount)) {
+ if (d_shards.at(shardIndex).d_map.size() >= (d_maxEntries / d_shardCount)) {
return;
}
uint32_t shardIndex = (d_expungeIndex++ % d_shardCount);
WriteLock w(&d_shards.at(shardIndex).d_lock);
- if (d_shards.at(shardIndex).d_entriesCount <= maxPerShard) {
+ auto& map = d_shards.at(shardIndex).d_map;
+ if (map.size() <= maxPerShard) {
continue;
}
- size_t toRemove = d_shards.at(shardIndex).d_entriesCount - maxPerShard;
- auto& map = d_shards.at(shardIndex).d_map;
+ size_t toRemove = map.size() - maxPerShard;
for (auto it = map.begin(); toRemove > 0 && it != map.end(); ) {
const CacheValue& value = it->second;
if (value.validity <= now) {
it = map.erase(it);
--toRemove;
- d_shards[shardIndex].d_entriesCount--;
++removed;
} else {
++it;
if (map.size() >= toRemove) {
std::advance(endIt, toRemove);
map.erase(beginIt, endIt);
- shard.d_entriesCount -= toRemove;
removed += toRemove;
}
else {
removed += map.size();
map.clear();
- shard.d_entriesCount = 0;
}
}
if ((value.qname == name || (suffixMatch && value.qname.isPartOf(name))) && (qtype == QType::ANY || qtype == value.qtype)) {
it = map.erase(it);
- d_shards[shardIndex].d_entriesCount--;
++removed;
} else {
++it;
uint64_t count = 0;
for (uint32_t shardIndex = 0; shardIndex < d_shardCount; shardIndex++) {
- count += d_shards.at(shardIndex).d_entriesCount;
+ count += d_shards.at(shardIndex).d_map.size();
}
return count;
class CacheShard
{
public:
- CacheShard(): d_entriesCount(0)
+ CacheShard()
{
}
- CacheShard(const CacheShard& old): d_entriesCount(0)
+ CacheShard(const CacheShard& old)
{
}
std::unordered_map<uint32_t,CacheValue> d_map;
ReadWriteLock d_lock;
- std::atomic<uint64_t> d_entriesCount;
};
bool cachedValueMatches(const CacheValue& cachedValue, uint16_t queryFlags, const DNSName& qname, uint16_t qtype, uint16_t qclass, bool tcp, bool dnssecOK, const boost::optional<Netmask>& subnet) const;