libosmscout  0.1
osmscout::Cache< K, V, IK > Class Template Reference

#include <Cache.h>

Classes

struct  CacheEntry
 An individual entry in the cache. More...
 
class  ValueSizer
 ValueSizer returns the size (in bytes) of an individual cache value. More...
 

Public Types

using CacheRef = typename OrderList::iterator
 
using Map = std::unordered_map< K, typename OrderList::iterator >
 
using OrderList = std::list< CacheEntry >
 

Public Member Functions

 Cache (size_t maxSize)
 Create a new cache object with the given max size. More...
 
void DumpStatistics (const char *cacheName, const ValueSizer &sizer)
 Dump some cache statistics to std::cout. More...
 
void Flush ()
 Completely flush the cache removing all entries from it. More...
 
bool GetEntry (const K &key, CacheRef &reference)
 Getting the value with the given key from cache. More...
 
size_t GetMaxSize () const
 Returns the maximum size of the cache. More...
 
size_t GetMemory (const ValueSizer &sizer) const
 
size_t GetSize () const
 Returns the current size of the cache. More...
 
bool IsActive () const
 Returns if the cache is active (maxSize > 0) More...
 
Cache::CacheRef SetEntry (const CacheEntry &entry)
 Set or update the cache with the given value for the given key. More...
 
void SetMaxSize (size_t maxSize)
 Set a new cache max size, possible striping the oldest entries from cache if the new size is smaller than the old one. More...
 

Detailed Description

template<class K, class V, class IK = PageId>
class osmscout::Cache< K, V, IK >

Generic FIFO cache implementation with O(n log n) semantic.

Template parameter class K holds the key value (must be a numerical value), parameter class V holds the data class that is to be cached, and parameter IK holds the internal key value, must be an unsigned value, default is PageId.

  • The cache is not threadsafe.
  • It uses a std::vector<std::list>> as a hash table for data lookup
  • It uses an std::list for implementing FIFO characteristics.

Implementation details: If std::unordered_map ist available we use this for fast detection (O(1)) if an object is already in the cast. If it is not available, we use a vector as a hashtable via the key value with list as entry type for hash code conflict handling.

Member Typedef Documentation

◆ CacheRef

template<class K , class V , class IK = PageId>
using osmscout::Cache< K, V, IK >::CacheRef = typename OrderList::iterator

◆ Map

template<class K , class V , class IK = PageId>
using osmscout::Cache< K, V, IK >::Map = std::unordered_map<K, typename OrderList::iterator>

◆ OrderList

template<class K , class V , class IK = PageId>
using osmscout::Cache< K, V, IK >::OrderList = std::list<CacheEntry>

Constructor & Destructor Documentation

◆ Cache()

template<class K , class V , class IK = PageId>
osmscout::Cache< K, V, IK >::Cache ( size_t  maxSize)
inlineexplicit

Create a new cache object with the given max size.

Member Function Documentation

◆ DumpStatistics()

template<class K , class V , class IK = PageId>
void osmscout::Cache< K, V, IK >::DumpStatistics ( const char *  cacheName,
const ValueSizer sizer 
)
inline

Dump some cache statistics to std::cout.

Referenced by osmscout::AreaAreaIndex::DumpStatistics().

◆ Flush()

template<class K , class V , class IK = PageId>
void osmscout::Cache< K, V, IK >::Flush ( )
inline

Completely flush the cache removing all entries from it.

Referenced by osmscout::AreaAreaIndex::Close(), and osmscout::AreaAreaIndex::FlushCache().

◆ GetEntry()

template<class K , class V , class IK = PageId>
bool osmscout::Cache< K, V, IK >::GetEntry ( const K &  key,
CacheRef reference 
)
inline

Getting the value with the given key from cache.

If there is no valued stored with the given key, false will be returned and the reference will be untouched.

If there is a value with the given key, reference will return a reference to the value and the value will moved to the front position of the cache to assure FIFO behaviour.

◆ GetMaxSize()

template<class K , class V , class IK = PageId>
size_t osmscout::Cache< K, V, IK >::GetMaxSize ( ) const
inline

Returns the maximum size of the cache.

◆ GetMemory()

template<class K , class V , class IK = PageId>
size_t osmscout::Cache< K, V, IK >::GetMemory ( const ValueSizer sizer) const
inline

◆ GetSize()

template<class K , class V , class IK = PageId>
size_t osmscout::Cache< K, V, IK >::GetSize ( ) const
inline

Returns the current size of the cache.

◆ IsActive()

template<class K , class V , class IK = PageId>
bool osmscout::Cache< K, V, IK >::IsActive ( ) const
inline

◆ SetEntry()

template<class K , class V , class IK = PageId>
Cache::CacheRef osmscout::Cache< K, V, IK >::SetEntry ( const CacheEntry entry)
inline

Set or update the cache with the given value for the given key.

If the key is not available in the cache the value will be added to the front of the cache (FIFO semantic) else the value will be updated (also moving it to the front of the cache (FIFO again).

◆ SetMaxSize()

template<class K , class V , class IK = PageId>
void osmscout::Cache< K, V, IK >::SetMaxSize ( size_t  maxSize)
inline

Set a new cache max size, possible striping the oldest entries from cache if the new size is smaller than the old one.


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