PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual > Class Template Reference

Thread-safe LRU cache with TTL support. More...

#include <simple_lru_cache.h>

Inheritance diagram for kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >:
Inheritance graph
Collaboration diagram for kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >:
Collaboration graph

Classes

struct  cache_entry
 

Public Types

using key_type = Key
 
using value_type = Value
 
using size_type = std::size_t
 
using clock_type = std::chrono::steady_clock
 
using time_point = clock_type::time_point
 

Public Member Functions

 simple_lru_cache (const cache_config &config=cache_config{})
 Construct a cache with the given configuration.
 
 simple_lru_cache (size_type max_size, std::chrono::seconds ttl)
 Construct a cache with size and TTL.
 
 simple_lru_cache (const simple_lru_cache &)=delete
 Non-copyable.
 
simple_lru_cacheoperator= (const simple_lru_cache &)=delete
 
 simple_lru_cache (simple_lru_cache &&other) noexcept
 Movable.
 
simple_lru_cacheoperator= (simple_lru_cache &&other) noexcept
 
 ~simple_lru_cache ()=default
 
std::optional< Value > get (const Key &key)
 Retrieve a value from the cache.
 
void put (const Key &key, const Value &value)
 Store a value in the cache.
 
void put (const Key &key, Value &&value)
 Store a value in the cache (move semantics)
 
bool contains (const Key &key) const
 Check if a key exists in the cache (without affecting LRU order)
 
bool invalidate (const Key &key)
 Remove a specific entry from the cache.
 
template<typename Predicate >
size_type invalidate_if (Predicate pred)
 
void clear ()
 Remove all entries from the cache.
 
size_type purge_expired ()
 Remove all expired entries from the cache.
 
size_type size () const
 Get the current number of entries in the cache.
 
bool empty () const
 Check if the cache is empty.
 
size_type max_size () const noexcept
 Get the maximum cache size.
 
std::chrono::seconds ttl () const noexcept
 Get the TTL duration.
 
const std::string & name () const noexcept
 Get the cache name (for metrics identification)
 
const cache_configconfig () const noexcept
 Get the cache configuration.
 
const cache_statsstats () const noexcept
 Get cache statistics.
 
double hit_rate () const noexcept
 Get the cache hit rate.
 
void reset_stats () noexcept
 Reset cache statistics.
 

Private Types

using list_type = std::list<cache_entry>
 
using list_iterator = typename list_type::iterator
 
using map_type = std::unordered_map<Key, list_iterator, Hash, KeyEqual>
 

Private Member Functions

time_point calculate_expiry () const
 
bool is_expired (time_point expiry) const
 
bool is_expired (time_point expiry, time_point now) const
 
void evict_oldest ()
 
void remove_entry (typename map_type::iterator map_it)
 

Private Attributes

cache_config config_
 
size_type max_size_
 
std::shared_mutex mutex_
 
list_type lru_list_
 
map_type cache_map_
 
cache_stats stats_
 

Detailed Description

template<typename Key, typename Value, typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
class kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >

Thread-safe LRU cache with TTL support.

This class provides a least-recently-used (LRU) cache that automatically evicts the oldest entries when the cache reaches its maximum size. Each entry has a configurable time-to-live (TTL) after which it expires.

The implementation uses:

  • std::list for O(1) insertion/removal and LRU ordering
  • std::unordered_map for O(1) key lookups
  • std::shared_mutex for reader-writer locking (multiple readers, single writer)

Thread Safety: All public methods are thread-safe. Read operations (get) use shared locks allowing concurrent reads. Write operations (put, invalidate, clear) use exclusive locks.

Template Parameters
KeyThe key type (must be hashable and equality comparable)
ValueThe value type (must be copy constructible)
HashHash function for keys (defaults to std::hash<Key>)
KeyEqualEquality comparison for keys (defaults to std::equal_to<Key>)

Definition at line 177 of file simple_lru_cache.h.

Member Typedef Documentation

◆ clock_type

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
using kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::clock_type = std::chrono::steady_clock

◆ key_type

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
using kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::key_type = Key

◆ list_iterator

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
using kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::list_iterator = typename list_type::iterator
private

◆ list_type

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
using kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::list_type = std::list<cache_entry>
private

◆ map_type

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
using kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::map_type = std::unordered_map<Key, list_iterator, Hash, KeyEqual>
private

◆ size_type

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
using kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::size_type = std::size_t

◆ time_point

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
using kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::time_point = clock_type::time_point

◆ value_type

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
using kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::value_type = Value

Constructor & Destructor Documentation

◆ simple_lru_cache() [1/4]

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::simple_lru_cache ( const cache_config & config = cache_config{})
inlineexplicit

Construct a cache with the given configuration.

Parameters
configCache configuration options
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/simple_lru_cache.h.

Definition at line 193 of file simple_lru_cache.h.

193 {})
194 : config_(config)
196 }
size_type max_size() const noexcept
Get the maximum cache size.
const cache_config & config() const noexcept
Get the cache configuration.

◆ simple_lru_cache() [2/4]

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::simple_lru_cache ( size_type max_size,
std::chrono::seconds ttl )
inline

Construct a cache with size and TTL.

Parameters
max_sizeMaximum number of entries
ttlTime-to-live for entries

Definition at line 203 of file simple_lru_cache.h.

204 : max_size_(max_size > 0 ? max_size : 1) {
206 config_.ttl = ttl;
207 }
std::chrono::seconds ttl() const noexcept
Get the TTL duration.
std::size_t max_size
Maximum number of entries in the cache.
std::chrono::seconds ttl
Time-To-Live for cache entries in seconds (default: 300 = 5 minutes)

References kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::config_, kcenon::pacs::services::cache::cache_config::max_size, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::max_size_, kcenon::pacs::services::cache::cache_config::ttl, and kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::ttl().

Here is the call graph for this function:

◆ simple_lru_cache() [3/4]

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::simple_lru_cache ( const simple_lru_cache< Key, Value, Hash, KeyEqual > & )
delete

Non-copyable.

◆ simple_lru_cache() [4/4]

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::simple_lru_cache ( simple_lru_cache< Key, Value, Hash, KeyEqual > && other)
inlinenoexcept

◆ ~simple_lru_cache()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::~simple_lru_cache ( )
default

Member Function Documentation

◆ calculate_expiry()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
time_point kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::calculate_expiry ( ) const
inlinenodiscardprivate

◆ clear()

◆ config()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
const cache_config & kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::config ( ) const
inlinenodiscardnoexcept

◆ contains()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
bool kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::contains ( const Key & key) const
inlinenodiscard

Check if a key exists in the cache (without affecting LRU order)

Note: This does not check TTL expiration for performance reasons. Use get() if you need TTL-aware existence checking.

Parameters
keyThe key to check
Returns
true if the key exists in the cache
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/simple_lru_cache.h.

Definition at line 357 of file simple_lru_cache.h.

357 {
358 std::shared_lock lock(mutex_);
359 return cache_map_.find(key) != cache_map_.end();
360 }

References kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::cache_map_, and kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::mutex_.

◆ empty()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
bool kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::empty ( ) const
inlinenodiscard

◆ evict_oldest()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
void kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::evict_oldest ( )
inlineprivate

◆ get()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
std::optional< Value > kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::get ( const Key & key)
inlinenodiscard

Retrieve a value from the cache.

If the key exists and the entry has not expired, returns the value and moves the entry to the front of the LRU list (most recently used). Expired entries are automatically removed.

Parameters
keyThe key to look up
Returns
The cached value if found and not expired, std::nullopt otherwise
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/simple_lru_cache.h.

Definition at line 253 of file simple_lru_cache.h.

253 {
254 std::unique_lock lock(mutex_);
255
256 auto it = cache_map_.find(key);
257 if (it == cache_map_.end()) {
258 // Cache miss
259 stats_.misses.fetch_add(1, std::memory_order_relaxed);
260 return std::nullopt;
261 }
262
263 auto& entry = it->second;
264
265 // Check TTL expiration
266 if (is_expired(entry->expiry_time)) {
267 // Entry expired - remove it
268 stats_.expirations.fetch_add(1, std::memory_order_relaxed);
269 stats_.misses.fetch_add(1, std::memory_order_relaxed);
270 remove_entry(it);
271 return std::nullopt;
272 }
273
274 // Cache hit - move to front (most recently used)
275 stats_.hits.fetch_add(1, std::memory_order_relaxed);
276 lru_list_.splice(lru_list_.begin(), lru_list_, entry);
277
278 return entry->value;
279 }
void remove_entry(typename map_type::iterator map_it)
std::atomic< std::uint64_t > hits
Number of cache hits.
std::atomic< std::uint64_t > expirations
Number of TTL expirations.
std::atomic< std::uint64_t > misses
Number of cache misses.

References kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::cache_map_, kcenon::pacs::services::cache::cache_stats::expirations, kcenon::pacs::services::cache::cache_stats::hits, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::is_expired(), kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::lru_list_, kcenon::pacs::services::cache::cache_stats::misses, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::mutex_, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::remove_entry(), and kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::stats_.

Here is the call graph for this function:

◆ hit_rate()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
double kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::hit_rate ( ) const
inlinenodiscardnoexcept

Get the cache hit rate.

Returns
Hit rate as a percentage (0.0 to 100.0)
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/simple_lru_cache.h.

Definition at line 537 of file simple_lru_cache.h.

537 {
538 return stats_.hit_rate();
539 }
double hit_rate() const noexcept
Calculate the cache hit rate.

References kcenon::pacs::services::cache::cache_stats::hit_rate(), and kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::stats_.

Here is the call graph for this function:

◆ invalidate()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
bool kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::invalidate ( const Key & key)
inline

Remove a specific entry from the cache.

Parameters
keyThe key to remove
Returns
true if the entry was found and removed, false otherwise
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/simple_lru_cache.h.

Definition at line 368 of file simple_lru_cache.h.

368 {
369 std::unique_lock lock(mutex_);
370
371 auto it = cache_map_.find(key);
372 if (it == cache_map_.end()) {
373 return false;
374 }
375
376 remove_entry(it);
377 return true;
378 }

References kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::cache_map_, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::mutex_, and kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::remove_entry().

Here is the call graph for this function:

◆ invalidate_if()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
template<typename Predicate >
size_type kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::invalidate_if ( Predicate pred)
inline

◆ is_expired() [1/2]

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
bool kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::is_expired ( time_point expiry) const
inlinenodiscardprivate

◆ is_expired() [2/2]

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
bool kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::is_expired ( time_point expiry,
time_point now ) const
inlinenodiscardprivate

Definition at line 577 of file simple_lru_cache.h.

577 {
578 return now > expiry;
579 }

◆ max_size()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
size_type kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::max_size ( ) const
inlinenodiscardnoexcept

◆ name()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
const std::string & kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::name ( ) const
inlinenodiscardnoexcept

Get the cache name (for metrics identification)

Returns
Cache name
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/simple_lru_cache.h.

Definition at line 509 of file simple_lru_cache.h.

509 {
510 return config_.cache_name;
511 }
std::string cache_name
Name for metrics identification (e.g., "query_cache", "study_cache")

References kcenon::pacs::services::cache::cache_config::cache_name, and kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::config_.

◆ operator=() [1/2]

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
simple_lru_cache & kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::operator= ( const simple_lru_cache< Key, Value, Hash, KeyEqual > & )
delete

◆ operator=() [2/2]

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
simple_lru_cache & kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::operator= ( simple_lru_cache< Key, Value, Hash, KeyEqual > && other)
inlinenoexcept

◆ purge_expired()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
size_type kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::purge_expired ( )
inline

Remove all expired entries from the cache.

This is useful for periodic cleanup to free memory from expired entries that haven't been accessed.

Returns
Number of expired entries removed
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/simple_lru_cache.h.

Definition at line 445 of file simple_lru_cache.h.

445 {
446 std::unique_lock lock(mutex_);
447
448 size_type removed = 0;
449 const auto now = clock_type::now();
450
451 auto it = lru_list_.begin();
452 while (it != lru_list_.end()) {
453 if (is_expired(it->expiry_time, now)) {
454 cache_map_.erase(it->key);
455 it = lru_list_.erase(it);
456 ++removed;
457 stats_.expirations.fetch_add(1, std::memory_order_relaxed);
458 } else {
459 ++it;
460 }
461 }
462
463 stats_.current_size.store(cache_map_.size(), std::memory_order_relaxed);
464 return removed;
465 }

References kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::cache_map_, kcenon::pacs::services::cache::cache_stats::current_size, kcenon::pacs::services::cache::cache_stats::expirations, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::is_expired(), kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::lru_list_, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::mutex_, and kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::stats_.

Here is the call graph for this function:

◆ put() [1/2]

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
void kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::put ( const Key & key,
const Value & value )
inline

Store a value in the cache.

If the key already exists, updates the value and moves to front. If the cache is full, evicts the least recently used entry.

Parameters
keyThe key to store
valueThe value to cache
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/simple_lru_cache.h.

Definition at line 290 of file simple_lru_cache.h.

290 {
291 std::unique_lock lock(mutex_);
292
293 auto it = cache_map_.find(key);
294 if (it != cache_map_.end()) {
295 // Update existing entry
296 auto& entry = it->second;
297 entry->value = value;
298 entry->expiry_time = calculate_expiry();
299 lru_list_.splice(lru_list_.begin(), lru_list_, entry);
300 return;
301 }
302
303 // Evict if at capacity
304 while (cache_map_.size() >= max_size_ && !lru_list_.empty()) {
305 evict_oldest();
306 }
307
308 // Insert new entry at front
309 lru_list_.emplace_front(cache_entry{key, value, calculate_expiry()});
310 cache_map_[key] = lru_list_.begin();
311
312 stats_.insertions.fetch_add(1, std::memory_order_relaxed);
313 stats_.current_size.store(cache_map_.size(), std::memory_order_relaxed);
314 }
std::atomic< std::uint64_t > insertions
Number of insertions.

References kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::cache_map_, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::calculate_expiry(), kcenon::pacs::services::cache::cache_stats::current_size, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::evict_oldest(), kcenon::pacs::services::cache::cache_stats::insertions, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::lru_list_, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::max_size_, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::mutex_, and kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::stats_.

Here is the call graph for this function:

◆ put() [2/2]

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
void kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::put ( const Key & key,
Value && value )
inline

Store a value in the cache (move semantics)

Parameters
keyThe key to store
valueThe value to cache (moved)

Definition at line 322 of file simple_lru_cache.h.

322 {
323 std::unique_lock lock(mutex_);
324
325 auto it = cache_map_.find(key);
326 if (it != cache_map_.end()) {
327 // Update existing entry
328 auto& entry = it->second;
329 entry->value = std::move(value);
330 entry->expiry_time = calculate_expiry();
331 lru_list_.splice(lru_list_.begin(), lru_list_, entry);
332 return;
333 }
334
335 // Evict if at capacity
336 while (cache_map_.size() >= max_size_ && !lru_list_.empty()) {
337 evict_oldest();
338 }
339
340 // Insert new entry at front
341 lru_list_.emplace_front(cache_entry{key, std::move(value), calculate_expiry()});
342 cache_map_[key] = lru_list_.begin();
343
344 stats_.insertions.fetch_add(1, std::memory_order_relaxed);
345 stats_.current_size.store(cache_map_.size(), std::memory_order_relaxed);
346 }

References kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::cache_map_, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::calculate_expiry(), kcenon::pacs::services::cache::cache_stats::current_size, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::evict_oldest(), kcenon::pacs::services::cache::cache_stats::insertions, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::lru_list_, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::max_size_, kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::mutex_, and kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::stats_.

Here is the call graph for this function:

◆ remove_entry()

◆ reset_stats()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
void kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::reset_stats ( )
inlinenoexcept

Reset cache statistics.

Resets all counters except current_size which reflects actual cache state.

Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/simple_lru_cache.h.

Definition at line 546 of file simple_lru_cache.h.

546 {
547 stats_.reset();
548 }
void reset() noexcept
Reset all statistics to zero.

References kcenon::pacs::services::cache::cache_stats::reset(), and kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::stats_.

Here is the call graph for this function:

◆ size()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
size_type kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::size ( ) const
inlinenodiscard

◆ stats()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
const cache_stats & kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::stats ( ) const
inlinenodiscardnoexcept

Get cache statistics.

Returns
Reference to cache statistics
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/simple_lru_cache.h.

Definition at line 529 of file simple_lru_cache.h.

529 {
530 return stats_;
531 }

References kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::stats_.

◆ ttl()

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
std::chrono::seconds kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::ttl ( ) const
inlinenodiscardnoexcept

Member Data Documentation

◆ cache_map_

template<typename Key , typename Value , typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>>
map_type kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::cache_map_
private

◆ config_

◆ lru_list_

◆ max_size_

◆ mutex_

◆ stats_


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