36#include <unordered_map>
91 std::size_t m_sizeLimit{ 0 };
94 std::chrono::milliseconds m_slidingExpiration{ std::chrono::minutes{ 60 } };
105 std::chrono::milliseconds m_backgroundCleanupInterval{ std::chrono::milliseconds{ 0 } };
141 inline CacheEntry( std::chrono::milliseconds expiration = std::chrono::hours( 1 ) );
174 template <typename TKey, typename TValue>
241 inline TValue*
find(
const TKey& key );
263 inline std::size_t
size()
const;
290 static constexpr size_t MAX_CLEANUP_PER_CYCLE = 10;
296 inline void checkAndPerformBackgroundCleanup();
315 mutable std::mutex m_mutex;
316 std::unordered_map<TKey, CachedItem> m_cache;
326 std::chrono::steady_clock::time_point m_lastCleanupTime;
336 inline void addToLruHead(
CacheEntry* entry )
noexcept;
342 inline void removeFromLru(
CacheEntry* entry )
noexcept;
348 inline void moveToLruHead(
CacheEntry* entry )
noexcept;
353 inline void evictLeastRecentlyUsed();
357#include "nfx/detail/cache/LruCache.inl"
Configuration options for LruCache behavior.
std::size_t sizeLimit() const
Get the maximum number of cache entries allowed.
std::chrono::milliseconds slidingExpiration() const
Get the default sliding expiration time.
std::chrono::milliseconds backgroundCleanupInterval() const
Get the background cleanup interval.
LruCacheOptions(std::size_t sizeLimit=0, std::chrono::milliseconds slidingExpiration=std::chrono::hours{ 1 }, std::chrono::milliseconds backgroundCleanupInterval=std::chrono::milliseconds{ 0 })
Construct LruCacheOptions with specified parameters.
Cache entry metadata with intrusive LRU list support.
CacheEntry * lruPrev
Previous entry in the LRU doubly-linked list.
const void * keyPtr
Pointer to the key for this cache entry.
std::chrono::milliseconds slidingExpiration
Sliding expiration time for this specific entry.
void touch() noexcept
Update the last accessed timestamp to current time.
std::chrono::steady_clock::time_point lastAccessed
Timestamp of the last access to this cache entry.
CacheEntry(std::chrono::milliseconds expiration=std::chrono::hours(1))
Construct cache entry with specified expiration time.
std::size_t size
Size of this cache entry for memory accounting.
CacheEntry * lruNext
Next entry in the LRU doubly-linked list.
bool isExpired() const noexcept
Check if this cache entry has expired based on sliding expiration.
Thread-safe memory cache with size limits and expiration policies.
TValue * find(const TKey &key)
Find a cached value without creating it.
bool isEmpty() const
Check if cache is empty.
LruCache(const LruCacheOptions &options={})
Construct memory cache with specified options.
bool remove(const TKey &key)
Remove an entry from the cache.
std::function< void(CacheEntry &)> ConfigFunction
Function type for configuring cache entry metadata.
std::size_t size() const
Get current cache size.
void clear()
Clear all cache entries.
void cleanupExpired()
Manually trigger cleanup of expired entries.
std::function< TValue()> FactoryFunction
Function type for creating cache values when not found.
TValue * get(const TKey &key, FactoryFunction factory, ConfigFunction configure=nullptr)
Get a cache entry, creating it with factory function if not found.