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_cache & operator= (const simple_lru_cache &)=delete
 
 simple_lru_cache (simple_lru_cache &&other) noexcept
 Movable.
 
simple_lru_cache & operator= (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)
 Remove all entries matching a predicate.
 
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_config & config () const noexcept
 Get the cache configuration.
 
const cache_stats & stats () 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>)
Example:
// Create a cache for query results
config.ttl = std::chrono::seconds{300}; // 5 minutes
config.cache_name = "cfind_cache";
// Store a result
cache.put("patient_123", query_result);
// Retrieve a result
auto result = cache.get("patient_123");
if (result) {
// Use cached result
process(*result);
}
// Check cache performance
const auto& stats = cache.stats();
logger_adapter::info("Cache hit rate: {:.2f}%", stats.hit_rate());
Thread-safe LRU cache with TTL support.
const cache_config & config() const noexcept
Get the cache configuration.
const cache_stats & stats() const noexcept
Get cache statistics.
Configuration options for the LRU cache.
std::size_t max_size
Maximum number of entries in the cache.
std::string cache_name
Name for metrics identification (e.g., "query_cache", "study_cache")
std::chrono::seconds ttl
Time-To-Live for cache entries in seconds (default: 300 = 5 minutes)
double hit_rate() const noexcept
Calculate the cache hit rate.
Result of a C-FIND query operation.
Definition query_scu.h:91

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

Definition at line 182 of file simple_lru_cache.h.

◆ 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

Definition at line 179 of file simple_lru_cache.h.

◆ 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

Definition at line 562 of file simple_lru_cache.h.

◆ 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

Definition at line 561 of file simple_lru_cache.h.

◆ 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

Definition at line 563 of file simple_lru_cache.h.

◆ 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

Definition at line 181 of file simple_lru_cache.h.

◆ 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

Definition at line 183 of file simple_lru_cache.h.

◆ 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

Definition at line 180 of file simple_lru_cache.h.

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

Definition at line 193 of file simple_lru_cache.h.

◆ 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

◆ 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()

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 >::clear ( )
inline

◆ 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

Get the cache configuration.

Returns
Cache configuration

Definition at line 517 of file simple_lru_cache.h.

517 {
518 return config_;
519 }

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

◆ 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

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

Check if the cache is empty.

Returns
true if the cache contains no entries

Definition at line 484 of file simple_lru_cache.h.

484 {
485 std::shared_lock lock(mutex_);
486 return cache_map_.empty();
487 }

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_.

◆ 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

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)

Definition at line 537 of file simple_lru_cache.h.

537 {
538 return stats_.hit_rate();
539 }

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

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

Remove all entries matching a predicate.

Iterates through all cache entries and removes those for which the predicate returns true. This is useful for invalidating related entries when data changes (e.g., invalidating all cached queries for a specific patient when new studies arrive).

Template Parameters
PredicateA callable that takes (const Key&, const Value&) and returns bool
Parameters
predThe predicate function; entries where pred(key, value) returns true are removed
Returns
Number of entries removed
Example:
// Invalidate all entries with keys starting with "PATIENT:"
cache.invalidate_if([](const std::string& key, const auto&) {
return key.starts_with("PATIENT:");
});
// Invalidate entries older than a certain age
cache.invalidate_if([&](const auto&, const QueryResult& result) {
return result.match_count > 1000; // Remove large result sets
});

Definition at line 406 of file simple_lru_cache.h.

406 {
407 std::unique_lock lock(mutex_);
408
409 size_type removed = 0;
410 auto it = lru_list_.begin();
411
412 while (it != lru_list_.end()) {
413 if (pred(it->key, it->value)) {
414 cache_map_.erase(it->key);
415 it = lru_list_.erase(it);
416 ++removed;
417 } else {
418 ++it;
419 }
420 }
421
422 stats_.current_size.store(cache_map_.size(), std::memory_order_relaxed);
423 return removed;
424 }

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::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_.

◆ 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

Definition at line 573 of file simple_lru_cache.h.

573 {
574 return clock_type::now() > expiry;
575 }

Referenced by kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::get(), and kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::purge_expired().

Here is the caller graph for this function:

◆ 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

Get the maximum cache size.

Returns
Maximum number of entries

Definition at line 493 of file simple_lru_cache.h.

493 {
494 return max_size_;
495 }

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

◆ 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

Definition at line 509 of file simple_lru_cache.h.

509 {
510 return config_.cache_name;
511 }

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

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

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()

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 >::remove_entry ( typename map_type::iterator map_it)
inlineprivate

◆ 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.

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

Get the current number of entries in the cache.

Returns
Current cache size

Definition at line 475 of file simple_lru_cache.h.

475 {
476 std::shared_lock lock(mutex_);
477 return cache_map_.size();
478 }

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_.

◆ 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

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

Get the TTL duration.

Returns
Time-to-live for entries

Definition at line 501 of file simple_lru_cache.h.

501 {
502 return config_.ttl;
503 }

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

Referenced by kcenon::pacs::services::cache::simple_lru_cache< Key, Value, Hash, KeyEqual >::simple_lru_cache().

Here is the caller graph for this function:

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: