nfx-lrucache 0.1.1
High-performance C++20 thread-safe LRU cache with sliding expiration
Loading...
Searching...
No Matches
nfx::cache::LruCache< TKey, TValue > Class Template Referencefinal

Thread-safe memory cache with size limits and expiration policies. More...

#include <nfx/cache/LruCache.h>

Public Types

using FactoryFunction = std::function<TValue()>
 Function type for creating cache values when not found.
using ConfigFunction = std::function<void( CacheEntry& )>
 Function type for configuring cache entry metadata.

Public Member Functions

 LruCache (const LruCacheOptions &options={})
 Construct memory cache with specified options.
 LruCache (const LruCache &)=delete
 LruCache (LruCache &&)=delete
LruCacheoperator= (const LruCache &)=delete
LruCacheoperator= (LruCache &&)=delete
TValue * get (const TKey &key, FactoryFunction factory, ConfigFunction configure=nullptr)
 Get a cache entry, creating it with factory function if not found.
TValue * find (const TKey &key)
 Find a cached value without creating it.
bool remove (const TKey &key)
 Remove an entry from the cache.
void clear ()
 Clear all cache entries.
std::size_t size () const
 Get current cache size.
bool isEmpty () const
 Check if cache is empty.
void cleanupExpired ()
 Manually trigger cleanup of expired entries.

Detailed Description

template<typename TKey, typename TValue>
class nfx::cache::LruCache< TKey, TValue >

Thread-safe memory cache with size limits and expiration policies.

Template Parameters
TKeyKey type for cache entries
TValueValue type for cached objects

Definition at line 175 of file LruCache.h.

Member Typedef Documentation

◆ ConfigFunction

template<typename TKey, typename TValue>
using nfx::cache::LruCache< TKey, TValue >::ConfigFunction = std::function<void( CacheEntry& )>

Function type for configuring cache entry metadata.

Definition at line 186 of file LruCache.h.

◆ FactoryFunction

template<typename TKey, typename TValue>
using nfx::cache::LruCache< TKey, TValue >::FactoryFunction = std::function<TValue()>

Function type for creating cache values when not found.

Definition at line 183 of file LruCache.h.

Constructor & Destructor Documentation

◆ LruCache()

template<typename TKey, typename TValue>
nfx::cache::LruCache< TKey, TValue >::LruCache ( const LruCacheOptions & options = {})
inlineexplicit

Construct memory cache with specified options.

Parameters
optionsConfiguration options for cache behavior

Member Function Documentation

◆ find()

template<typename TKey, typename TValue>
TValue * nfx::cache::LruCache< TKey, TValue >::find ( const TKey & key)
inline

Find a cached value without creating it.

Parameters
keyThe cache key
Returns
Pointer to the cached value if found and not expired, nullptr otherwise

◆ get()

template<typename TKey, typename TValue>
TValue * nfx::cache::LruCache< TKey, TValue >::get ( const TKey & key,
FactoryFunction factory,
ConfigFunction configure = nullptr )
inline

Get a cache entry, creating it with factory function if not found.

Parameters
keyThe cache key
factoryFunction to create the value if not cached
configureOptional function to configure cache entry
Returns
Pointer to the cached value (never null; throws on factory failure)

◆ isEmpty()

template<typename TKey, typename TValue>
bool nfx::cache::LruCache< TKey, TValue >::isEmpty ( ) const
inline

Check if cache is empty.

Returns
True if cache contains no entries

◆ remove()

template<typename TKey, typename TValue>
bool nfx::cache::LruCache< TKey, TValue >::remove ( const TKey & key)
inline

Remove an entry from the cache.

Parameters
keyThe cache key to remove
Returns
True if entry was removed, false if not found

◆ size()

template<typename TKey, typename TValue>
std::size_t nfx::cache::LruCache< TKey, TValue >::size ( ) const
inline

Get current cache size.

Returns
Number of entries in cache

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