9#ifndef SQUID__SRC_BASE_CLPMAP_H
10#define SQUID__SRC_BASE_CLPMAP_H
20#include <unordered_map>
39template <
class Key,
class Value, u
int64_t MemoryUsedBy(const Value &) = DefaultMemoryUsage>
47 ClpMap(uint64_t capacity,
Ttl defaultTtl);
59 const Value *
get(
const Key &);
64 bool add(
const Key &,
const Value &,
Ttl);
70 void del(
const Key &);
92 Entry(
const Key &,
const Value &,
const Ttl);
105 using Entries = std::list<Entry, PoolingAllocator<Entry> >;
108 using IndexItem = std::pair<const Key, EntriesIterator>;
113 static std::optional<uint64_t>
MemoryCountedFor(
const Key &,
const Value &);
115 void trim(uint64_t wantSpace);
135template <
class Key,
class Value, u
int64_t MemoryUsedBy(const Value &)>
137 defaultTtl_(defaultTtl)
143template <
class Key,
class Value, u
int64_t MemoryUsedBy(const Value &)>
147 if (memUsed_ > newLimit)
148 trim(memLimit_ - newLimit);
149 memLimit_ = newLimit;
153template <
class Key,
class Value, u
int64_t MemoryUsedBy(const Value &)>
157 const auto i = index_.find(key);
158 if (i == index_.end())
161 const auto entryPosition = i->second;
162 if (!entryPosition->expired()) {
163 if (entryPosition != entries_.begin())
164 entries_.splice(entries_.begin(), entries_, entryPosition);
173template <
class Key,
class Value, u
int64_t MemoryUsedBy(const Value &)>
177 const auto i = find(key);
178 if (i != index_.end()) {
179 const auto &entry = *(i->second);
185template <
class Key,
class Value, u
int64_t MemoryUsedBy(const Value &)>
186std::optional<uint64_t>
193 const auto keySz = k.length();
196 return NaturalSum<uint64_t>(
199 sizeof(
typename Entries::value_type),
202 sizeof(
typename Index::value_type));
205template <
class Key,
class Value, u
int64_t MemoryUsedBy(const Value &)>
218 const auto memoryRequirements = MemoryCountedFor(key, v);
219 if (!memoryRequirements)
222 const auto wantSpace = memoryRequirements.value();
223 if (wantSpace > memLimit() || wantSpace == 0)
227 auto &
addedEntry = entries_.emplace_front(key, v, ttl);
228 index_.emplace(key, entries_.begin());
231 memUsed_ += wantSpace;
232 assert(memUsed_ >= wantSpace);
237template <
class Key,
class Value, u
int64_t MemoryUsedBy(const Value &)>
241 assert(i != index_.end());
242 const auto entryPosition = i->second;
244 assert(entryPosition != entries_.end());
245 const auto sz = entryPosition->memCounted;
250 entries_.erase(entryPosition);
253template <
class Key,
class Value, u
int64_t MemoryUsedBy(const Value &)>
257 const auto i = find(key);
258 if (i != index_.end())
263template <
class Key,
class Value, u
int64_t MemoryUsedBy(const Value &)>
267 assert(wantSpace <= memLimit());
268 while (freeMem() < wantSpace) {
269 assert(!entries_.empty());
272 del(entries_.rbegin()->key);
276template <
class Key,
class Value, u
int64_t MemoryUsedBy(const Value &)>
uint64_t DefaultMemoryUsage(const Value &e)
static char const * trim(char const *s)
S SetToNaturalSumOrMax(S &var, const Args... args)
the keeper of cache entry Key, Value, and caching-related entry metadata
bool expired() const
whether the entry is stale
Value value
cached value provided by the map user
time_t expires
get() stops returning the entry after this time
uint64_t memCounted
memory accounted for this entry in our ClpMap
Key key
the entry search key; see ClpMap::get()
Entry(const Key &, const Value &, const Ttl)
const Value * get(const Key &)
std::pair< const Key, EntriesIterator > IndexItem
ClpMap(const ClpMap &)=delete
ClpMap(const uint64_t capacity)
void del(const Key &)
Remove the corresponding entry (if any)
void trim(uint64_t wantSpace)
purges entries to make free memory large enough to fit wantSpace bytes
typename Entries::iterator EntriesIterator
uint64_t memLimit() const
The memory capacity for the map.
uint64_t freeMem() const
The free space of the map.
ClpMap & operator=(const ClpMap &)=delete
Ttl defaultTtl_
entry TTL to use if none provided to add()
typename Index::iterator IndexIterator
uint64_t memUsed_
the total amount of memory we currently use for all cached entries
void erase(const IndexIterator &)
removes the cached entry (identified by its index) from the map
Index index_
entries_ positions indexed by the entry key
static std::optional< uint64_t > MemoryCountedFor(const Key &, const Value &)
uint64_t memLimit_
the maximum memory we are allowed to use for all cached entries
IndexIterator find(const Key &)
std::unordered_map< Key, EntriesIterator, std::hash< Key >, std::equal_to< Key >, PoolingAllocator< IndexItem > > Index
key:entry_position mapping for fast entry lookups by key
int Ttl
maximum desired entry caching duration (a.k.a. TTL), in seconds
void setMemLimit(uint64_t newLimit)
Reset the memory capacity for this map, purging if needed.
size_t entries() const
The number of currently stored entries, including expired ones.
bool add(const Key &, const Value &, Ttl)
uint64_t memoryUsed() const
The current (approximate) memory usage of the map.
Entries entries_
cached entries, including expired ones, in LRU order
std::list< Entry, PoolingAllocator< Entry > > Entries
Entries in LRU order.
bool add(const Key &key, const Value &v)
Copy the given value into the map (with the given key and default TTL)
STL Allocator that uses Squid memory pools for memory management.
A const & max(A const &lhs, A const &rhs)
static StoreEntry * addedEntry(Store::Disk *aStore, String name, String, String)