#include <ClpMap.h>

Collaboration diagram for ClpMap< Key, Value, MemoryUsedBy >:

Classes

class  Entry
 the keeper of cache entry Key, Value, and caching-related entry metadata More...
 

Public Types

using Ttl = int
 maximum desired entry caching duration (a.k.a. TTL), in seconds More...
 

Public Member Functions

 ClpMap (const uint64_t capacity)
 
 ClpMap (uint64_t capacity, Ttl defaultTtl)
 
 ~ClpMap ()=default
 
 ClpMap (const ClpMap &)=delete
 
ClpMapoperator= (const ClpMap &)=delete
 
const Value * get (const Key &)
 
bool add (const Key &, const Value &, Ttl)
 
bool add (const Key &key, const Value &v)
 Copy the given value into the map (with the given key and default TTL) More...
 
void del (const Key &)
 Remove the corresponding entry (if any) More...
 
void setMemLimit (uint64_t newLimit)
 Reset the memory capacity for this map, purging if needed. More...
 
uint64_t memLimit () const
 The memory capacity for the map. More...
 
uint64_t freeMem () const
 The free space of the map. More...
 
uint64_t memoryUsed () const
 The current (approximate) memory usage of the map. More...
 
size_t entries () const
 The number of currently stored entries, including expired ones. More...
 

Private Types

using Entries = std::list< Entry, PoolingAllocator< Entry > >
 Entries in LRU order. More...
 
using EntriesIterator = typename Entries::iterator
 
using IndexItem = std::pair< Key, EntriesIterator >
 
using Index = std::unordered_map< Key, EntriesIterator, std::hash< Key >, std::equal_to< Key >, PoolingAllocator< IndexItem > >
 key:entry_position mapping for fast entry lookups by key More...
 
using IndexIterator = typename Index::iterator
 

Private Member Functions

void trim (uint64_t wantSpace)
 purges entries to make free memory large enough to fit wantSpace bytes More...
 
void erase (const IndexIterator &)
 removes the cached entry (identified by its index) from the map More...
 
IndexIterator find (const Key &)
 

Static Private Member Functions

static Optional< uint64_t > MemoryCountedFor (const Key &, const Value &)
 

Private Attributes

Entries entries_
 cached entries, including expired ones, in LRU order More...
 
Index index_
 entries_ positions indexed by the entry key More...
 
Ttl defaultTtl_ = std::numeric_limits<Ttl>::max()
 entry TTL to use if none provided to add() More...
 
uint64_t memLimit_ = 0
 the maximum memory we are allowed to use for all cached entries More...
 
uint64_t memUsed_ = 0
 the total amount of memory we currently use for all cached entries More...
 

Detailed Description

template<class Key, class Value, uint64_t MemoryUsedBy = DefaultMemoryUsage>
class ClpMap< Key, Value, MemoryUsedBy >

An in-memory associative container enforcing three primary caching policies:

  • Capacity: The memory used by cached entries has a configurable limit;
  • Lifetime: Entries are hidden (and may be deleted) after their TTL expires;
  • Priority: Capacity victims are purged in LRU order. Individual cache entry operations have average constant-time complexity.

Value must meet STL requirements of Erasable and EmplaceConstructible. Key must come with std::hash<Key> and std::equal_to<Key> instantiations. Key::length() must return the number of memory bytes in use by the key. MemoryUsedBy() must return the number of memory bytes in use by the value.

Definition at line 40 of file ClpMap.h.

Member Typedef Documentation

◆ Entries

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
using ClpMap< Key, Value, MemoryUsedBy >::Entries = std::list<Entry, PoolingAllocator<Entry> >
private

Definition at line 105 of file ClpMap.h.

◆ EntriesIterator

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
using ClpMap< Key, Value, MemoryUsedBy >::EntriesIterator = typename Entries::iterator
private

Definition at line 106 of file ClpMap.h.

◆ Index

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
using ClpMap< Key, Value, MemoryUsedBy >::Index = std::unordered_map<Key, EntriesIterator, std::hash<Key>, std::equal_to<Key>, PoolingAllocator<IndexItem> >
private

Definition at line 110 of file ClpMap.h.

◆ IndexItem

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
using ClpMap< Key, Value, MemoryUsedBy >::IndexItem = std::pair<Key, EntriesIterator>
private

Definition at line 108 of file ClpMap.h.

◆ IndexIterator

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
using ClpMap< Key, Value, MemoryUsedBy >::IndexIterator = typename Index::iterator
private

Definition at line 111 of file ClpMap.h.

◆ Ttl

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
using ClpMap< Key, Value, MemoryUsedBy >::Ttl = int

Definition at line 44 of file ClpMap.h.

Constructor & Destructor Documentation

◆ ClpMap() [1/3]

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
ClpMap< Key, Value, MemoryUsedBy >::ClpMap ( const uint64_t  capacity)
inlineexplicit

Definition at line 46 of file ClpMap.h.

References ClpMap< Key, Value, MemoryUsedBy >::setMemLimit().

◆ ClpMap() [2/3]

template<class Key , class Value , uint64_t MemoryUsedBy>
ClpMap< Key, Value, MemoryUsedBy >::ClpMap ( uint64_t  capacity,
Ttl  defaultTtl 
)

Definition at line 136 of file ClpMap.h.

References assert, and ClpMap< Key, Value, MemoryUsedBy >::setMemLimit().

◆ ~ClpMap()

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
ClpMap< Key, Value, MemoryUsedBy >::~ClpMap ( )
default

◆ ClpMap() [3/3]

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
ClpMap< Key, Value, MemoryUsedBy >::ClpMap ( const ClpMap< Key, Value, MemoryUsedBy > &  )
delete

Member Function Documentation

◆ add() [1/2]

template<class Key , class Value , uint64_t MemoryUsedBy>
bool ClpMap< Key, Value, MemoryUsedBy >::add ( const Key &  key,
const Value &  v,
Ttl  ttl 
)

Copy the given value into the map (with the given key and TTL)

Return values
truethe value was successfully copied into the map
falsecaching was rejected (the map remains unchanged)

Definition at line 207 of file ClpMap.h.

References assert, and trim().

Referenced by ConnStateData::storeTlsContextToCache().

◆ add() [2/2]

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
bool ClpMap< Key, Value, MemoryUsedBy >::add ( const Key &  key,
const Value &  v 
)
inline

◆ del()

template<class Key , class Value , uint64_t MemoryUsedBy>
void ClpMap< Key, Value, MemoryUsedBy >::del ( const Key &  key)

Definition at line 255 of file ClpMap.h.

Referenced by ConnStateData::getTlsContextFromCache().

◆ entries()

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
size_t ClpMap< Key, Value, MemoryUsedBy >::entries ( ) const
inline

◆ erase()

template<class Key , class Value , uint64_t MemoryUsedBy>
void ClpMap< Key, Value, MemoryUsedBy >::erase ( const IndexIterator i)
private

Definition at line 239 of file ClpMap.h.

References assert, and sz.

◆ find()

template<class Key , class Value , uint64_t MemoryUsedBy>
ClpMap< Key, Value, MemoryUsedBy >::IndexIterator ClpMap< Key, Value, MemoryUsedBy >::find ( const Key &  key)
private
Returns
the index position of an entry identified by its key (or end())

Definition at line 155 of file ClpMap.h.

◆ freeMem()

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
uint64_t ClpMap< Key, Value, MemoryUsedBy >::freeMem ( ) const
inline

◆ get()

template<class Key , class Value , uint64_t MemoryUsedBy>
const Value * ClpMap< Key, Value, MemoryUsedBy >::get ( const Key &  key)
Returns
a pointer to a fresh cached value (or nil) The underlying value is owned by the map, so the pointer may be invalidated by any non-constant method call, including another get(). Also moves the found entry to the end of the purging queue.

Definition at line 175 of file ClpMap.h.

Referenced by ConnStateData::getTlsContextFromCache().

◆ memLimit()

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
uint64_t ClpMap< Key, Value, MemoryUsedBy >::memLimit ( ) const
inline

◆ MemoryCountedFor()

template<class Key , class Value , uint64_t MemoryUsedBy>
Optional< uint64_t > ClpMap< Key, Value, MemoryUsedBy >::MemoryCountedFor ( const Key &  k,
const Value &  v 
)
staticprivate

Definition at line 187 of file ClpMap.h.

◆ memoryUsed()

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
uint64_t ClpMap< Key, Value, MemoryUsedBy >::memoryUsed ( ) const
inline

◆ operator=()

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
ClpMap& ClpMap< Key, Value, MemoryUsedBy >::operator= ( const ClpMap< Key, Value, MemoryUsedBy > &  )
delete

◆ setMemLimit()

template<class Key , class Value , uint64_t MemoryUsedBy>
void ClpMap< Key, Value, MemoryUsedBy >::setMemLimit ( uint64_t  newLimit)

Definition at line 145 of file ClpMap.h.

References trim().

Referenced by ClpMap< Key, Value, MemoryUsedBy >::ClpMap().

◆ trim()

template<class Key , class Value , uint64_t MemoryUsedBy>
void ClpMap< Key, Value, MemoryUsedBy >::trim ( uint64_t  wantSpace)
private

Definition at line 265 of file ClpMap.h.

References assert.

Member Data Documentation

◆ defaultTtl_

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
Ttl ClpMap< Key, Value, MemoryUsedBy >::defaultTtl_ = std::numeric_limits<Ttl>::max()
private

Definition at line 126 of file ClpMap.h.

Referenced by ClpMap< Key, Value, MemoryUsedBy >::add().

◆ entries_

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
Entries ClpMap< Key, Value, MemoryUsedBy >::entries_
private

Definition at line 120 of file ClpMap.h.

Referenced by ClpMap< Key, Value, MemoryUsedBy >::entries().

◆ index_

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
Index ClpMap< Key, Value, MemoryUsedBy >::index_
private

Definition at line 123 of file ClpMap.h.

◆ memLimit_

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
uint64_t ClpMap< Key, Value, MemoryUsedBy >::memLimit_ = 0
private

Definition at line 129 of file ClpMap.h.

Referenced by ClpMap< Key, Value, MemoryUsedBy >::memLimit().

◆ memUsed_

template<class Key , class Value , uint64_t MemoryUsedBy = DefaultMemoryUsage>
uint64_t ClpMap< Key, Value, MemoryUsedBy >::memUsed_ = 0
private

Definition at line 132 of file ClpMap.h.

Referenced by ClpMap< Key, Value, MemoryUsedBy >::memoryUsed().


The documentation for this class was generated from the following file:

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors