Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
correlation::app::ThreadSafeLRUCache< ValueType > Class Template Reference

Thread-safe LRU cache storing per-atom parameters or descriptors for up to N trajectory frames, bounded by both frame count and a configurable memory byte budget. More...

#include <SteinhardtLRUCache.hpp>

Inheritance diagram for correlation::app::ThreadSafeLRUCache< ValueType >:

Classes

struct  CacheEntry

Public Types

using ParamsPtr = std::shared_ptr<const ValueType>

Public Member Functions

 ThreadSafeLRUCache (std::size_t max_capacity=512, std::size_t max_bytes=kDefaultMaxBytes) noexcept
 Constructs cache with specified maximum frame capacity and memory byte budget.
 ~ThreadSafeLRUCache ()=default
 ThreadSafeLRUCache (const ThreadSafeLRUCache &)=delete
ThreadSafeLRUCache & operator= (const ThreadSafeLRUCache &)=delete
 ThreadSafeLRUCache (ThreadSafeLRUCache &&) noexcept=delete
ThreadSafeLRUCache & operator= (ThreadSafeLRUCache &&) noexcept=delete
ParamsPtr get (std::size_t frame_index)
 Retrieves cached parameters and marks frame as most recently used.
ParamsPtr peek (std::size_t frame_index) const
 Inspects cache for frame without updating LRU order.
void put (std::size_t frame_index, ParamsPtr params)
 Inserts or updates cached parameters for a frame.
bool contains (std::size_t frame_index) const
 Checks if a given frame index resides in the cache.
std::size_t size () const
 Gets current count of cached frames.
std::size_t capacity () const noexcept
 Gets maximum frame capacity of the cache.
void setCapacity (std::size_t new_capacity)
 Updates maximum frame capacity, evicting oldest entries if shrinking.
std::size_t memoryBytes () const noexcept
 Gets current estimated memory footprint of cached entries in bytes.
std::size_t maxBytes () const noexcept
 Gets configured maximum byte budget (0 indicates unlimited).
void setMaxBytes (std::size_t new_max_bytes)
 Updates the byte budget, evicting oldest entries if over budget.
template<typename Pred>
std::size_t countMatching (Pred &&pred) const
 Counts cached entries satisfying a predicate.
void clear ()
 Purges all entries from the cache.

Static Public Attributes

static constexpr std::size_t kDefaultMaxBytes = 256UL * 1024UL * 1024UL
 256 MiB cap.

Private Member Functions

void evictIfNeededLocked ()

Private Attributes

std::shared_mutex mutex_
std::size_t capacity_ {512}
std::size_t max_bytes_ {kDefaultMaxBytes}
std::size_t current_bytes_ {0}
std::list< std::size_t > lru_order_
std::unordered_map< std::size_t, CacheEntry > map_

Detailed Description

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
class correlation::app::ThreadSafeLRUCache< ValueType >

Thread-safe LRU cache storing per-atom parameters or descriptors for up to N trajectory frames, bounded by both frame count and a configurable memory byte budget.

Utilizes std::shared_mutex to permit concurrent read queries while synchronizing LRU mutations and evictions under write locks.

Member Typedef Documentation

◆ ParamsPtr

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
using correlation::app::ThreadSafeLRUCache< ValueType >::ParamsPtr = std::shared_ptr<const ValueType>

Constructor & Destructor Documentation

◆ ThreadSafeLRUCache() [1/3]

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
correlation::app::ThreadSafeLRUCache< ValueType >::ThreadSafeLRUCache ( std::size_t max_capacity = 512,
std::size_t max_bytes = kDefaultMaxBytes )
inlineexplicitnoexcept

Constructs cache with specified maximum frame capacity and memory byte budget.

Parameters
[in]max_capacityMaximum number of frames cached simultaneously (default 512).
[in]max_bytesMaximum resident byte budget (default 256 MiB; 0 disables byte cap).

◆ ~ThreadSafeLRUCache()

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
correlation::app::ThreadSafeLRUCache< ValueType >::~ThreadSafeLRUCache ( )
default

◆ ThreadSafeLRUCache() [2/3]

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
correlation::app::ThreadSafeLRUCache< ValueType >::ThreadSafeLRUCache ( const ThreadSafeLRUCache< ValueType > & )
delete

◆ ThreadSafeLRUCache() [3/3]

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
correlation::app::ThreadSafeLRUCache< ValueType >::ThreadSafeLRUCache ( ThreadSafeLRUCache< ValueType > && )
deletenoexcept

Member Function Documentation

◆ capacity()

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
std::size_t correlation::app::ThreadSafeLRUCache< ValueType >::capacity ( ) const
inlinenodiscardnoexcept

Gets maximum frame capacity of the cache.

Returns
Cache capacity limit.

◆ clear()

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
void correlation::app::ThreadSafeLRUCache< ValueType >::clear ( )
inline

Purges all entries from the cache.

◆ contains()

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
bool correlation::app::ThreadSafeLRUCache< ValueType >::contains ( std::size_t frame_index) const
inlinenodiscard

Checks if a given frame index resides in the cache.

Parameters
[in]frame_indexTrajectory frame index.
Returns
True if frame is cached.

◆ countMatching()

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
template<typename Pred>
std::size_t correlation::app::ThreadSafeLRUCache< ValueType >::countMatching ( Pred && pred) const
inlinenodiscard

Counts cached entries satisfying a predicate.

Template Parameters
PredCallable taking const ValueType & and returning bool.
Parameters
[in]predPredicate evaluated on each non-null cached value.
Returns
Number of matching cached entries.

◆ evictIfNeededLocked()

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
void correlation::app::ThreadSafeLRUCache< ValueType >::evictIfNeededLocked ( )
inlineprivate
Here is the caller graph for this function:

◆ get()

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
ParamsPtr correlation::app::ThreadSafeLRUCache< ValueType >::get ( std::size_t frame_index)
inlinenodiscard

Retrieves cached parameters and marks frame as most recently used.

Parameters
[in]frame_indexTrajectory frame index.
Returns
Shared pointer to cached parameters, or nullptr if not cached.

◆ maxBytes()

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
std::size_t correlation::app::ThreadSafeLRUCache< ValueType >::maxBytes ( ) const
inlinenodiscardnoexcept

Gets configured maximum byte budget (0 indicates unlimited).

Returns
Maximum byte budget.

◆ memoryBytes()

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
std::size_t correlation::app::ThreadSafeLRUCache< ValueType >::memoryBytes ( ) const
inlinenodiscardnoexcept

Gets current estimated memory footprint of cached entries in bytes.

Returns
Estimated resident bytes.

◆ operator=() [1/2]

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
ThreadSafeLRUCache & correlation::app::ThreadSafeLRUCache< ValueType >::operator= ( const ThreadSafeLRUCache< ValueType > & )
delete

◆ operator=() [2/2]

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
ThreadSafeLRUCache & correlation::app::ThreadSafeLRUCache< ValueType >::operator= ( ThreadSafeLRUCache< ValueType > && )
deletenoexcept

◆ peek()

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
ParamsPtr correlation::app::ThreadSafeLRUCache< ValueType >::peek ( std::size_t frame_index) const
inlinenodiscard

Inspects cache for frame without updating LRU order.

Parameters
[in]frame_indexTrajectory frame index.
Returns
Shared pointer to cached parameters, or nullptr if not cached.

◆ put()

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
void correlation::app::ThreadSafeLRUCache< ValueType >::put ( std::size_t frame_index,
ParamsPtr params )
inline

Inserts or updates cached parameters for a frame.

Parameters
[in]frame_indexTrajectory frame index.
[in]paramsComputed parameters.

◆ setCapacity()

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
void correlation::app::ThreadSafeLRUCache< ValueType >::setCapacity ( std::size_t new_capacity)
inline

Updates maximum frame capacity, evicting oldest entries if shrinking.

Parameters
[in]new_capacityNew maximum frame count (clamped to >= 1).

◆ setMaxBytes()

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
void correlation::app::ThreadSafeLRUCache< ValueType >::setMaxBytes ( std::size_t new_max_bytes)
inline

Updates the byte budget, evicting oldest entries if over budget.

Parameters
[in]new_max_bytesNew byte budget (0 disables byte cap).

◆ size()

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
std::size_t correlation::app::ThreadSafeLRUCache< ValueType >::size ( ) const
inlinenodiscard

Gets current count of cached frames.

Returns
Number of entries in cache.

Member Data Documentation

◆ capacity_

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
std::size_t correlation::app::ThreadSafeLRUCache< ValueType >::capacity_ {512}
private

◆ current_bytes_

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
std::size_t correlation::app::ThreadSafeLRUCache< ValueType >::current_bytes_ {0}
private

◆ kDefaultMaxBytes

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
std::size_t correlation::app::ThreadSafeLRUCache< ValueType >::kDefaultMaxBytes = 256UL * 1024UL * 1024UL
staticconstexpr

256 MiB cap.

◆ lru_order_

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
std::list<std::size_t> correlation::app::ThreadSafeLRUCache< ValueType >::lru_order_
private

◆ map_

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
std::unordered_map<std::size_t, CacheEntry> correlation::app::ThreadSafeLRUCache< ValueType >::map_
private

◆ max_bytes_

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
std::size_t correlation::app::ThreadSafeLRUCache< ValueType >::max_bytes_ {kDefaultMaxBytes}
private

◆ mutex_

template<typename ValueType = calculators::SteinhardtCalculator::SteinhardtParams>
std::shared_mutex correlation::app::ThreadSafeLRUCache< ValueType >::mutex_
mutableprivate

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