PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
kcenon::pacs::services::cache::query_cache Class Reference

DICOM query result cache with monitoring integration. More...

#include <query_cache.h>

Collaboration diagram for kcenon::pacs::services::cache::query_cache:
Collaboration graph

Public Types

using key_type = std::string
 
using value_type = cached_query_result
 
using size_type = std::size_t
 

Public Member Functions

 query_cache (const query_cache_config &config=query_cache_config{})
 Construct a query cache with the given configuration.
 
 query_cache (const query_cache &)=delete
 Non-copyable.
 
query_cacheoperator= (const query_cache &)=delete
 
 query_cache (query_cache &&) noexcept=default
 Movable.
 
query_cacheoperator= (query_cache &&) noexcept=default
 
 ~query_cache ()=default
 
std::optional< cached_query_resultget (const key_type &key)
 Retrieve a cached query result.
 
void put (const key_type &key, const cached_query_result &result)
 Store a query result in the cache.
 
void put (const key_type &key, cached_query_result &&result)
 Store a query result in the cache (move semantics)
 
bool invalidate (const key_type &key)
 Remove a specific entry from the cache.
 
size_type invalidate_by_prefix (const std::string &prefix)
 
size_type invalidate_by_query_level (const std::string &query_level)
 Remove all entries for a specific query level.
 
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.
 
size_type size () const
 Get the current number of cached entries.
 
bool empty () const
 Check if the cache is empty.
 
size_type max_size () const noexcept
 Get the maximum cache size.
 
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.
 

Static Public Member Functions

static std::string build_key (const std::string &query_level, const std::vector< std::pair< std::string, std::string > > &params)
 Build a cache key from query parameters.
 
static std::string build_key_with_ae (const std::string &calling_ae, const std::string &query_level, const std::vector< std::pair< std::string, std::string > > &params)
 Build a cache key with AE title prefix.
 

Private Attributes

query_cache_config config_
 
string_lru_cache< cached_query_resultcache_
 

Detailed Description

DICOM query result cache with monitoring integration.

This class provides a specialized cache for C-FIND query results with:

  • LRU eviction policy
  • Configurable TTL (Time-To-Live)
  • Integration with PACS monitoring system
  • Thread-safe concurrent access

Thread Safety: All public methods are thread-safe.

Definition at line 132 of file query_cache.h.

Member Typedef Documentation

◆ key_type

◆ size_type

◆ value_type

Constructor & Destructor Documentation

◆ query_cache() [1/3]

kcenon::pacs::services::cache::query_cache::query_cache ( const query_cache_config & config = query_cache_config{})
explicit

Construct a query cache with the given configuration.

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

Definition at line 22 of file query_cache.cpp.

23 : config_(config)
24 , cache_(cache_config{
25 config.max_entries,
26 config.ttl,
27 config.enable_metrics,
28 config.cache_name}) {
29}
string_lru_cache< cached_query_result > cache_

◆ query_cache() [2/3]

kcenon::pacs::services::cache::query_cache::query_cache ( const query_cache & )
delete

Non-copyable.

◆ query_cache() [3/3]

kcenon::pacs::services::cache::query_cache::query_cache ( query_cache && )
defaultnoexcept

Movable.

◆ ~query_cache()

kcenon::pacs::services::cache::query_cache::~query_cache ( )
default

Member Function Documentation

◆ build_key()

std::string kcenon::pacs::services::cache::query_cache::build_key ( const std::string & query_level,
const std::vector< std::pair< std::string, std::string > > & params )
staticnodiscard

Build a cache key from query parameters.

Creates a deterministic key from the query level and parameters. The key format is: "level:param1=value1;param2=value2;..." Parameters are sorted by name for consistent key generation.

Parameters
query_levelThe query retrieve level (PATIENT, STUDY, SERIES, IMAGE)
paramsList of parameter name-value pairs
Returns
Cache key string
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/query_cache.h.

Definition at line 110 of file query_cache.cpp.

112 {
113
114 // Sort parameters for consistent key generation
115 auto sorted_params = params;
116 std::sort(sorted_params.begin(), sorted_params.end(),
117 [](const auto& a, const auto& b) { return a.first < b.first; });
118
119 std::ostringstream oss;
120 oss << query_level << ":";
121
122 bool first = true;
123 for (const auto& [name, value] : sorted_params) {
124 if (!first) {
125 oss << ";";
126 }
127 first = false;
128 oss << name << "=" << value;
129 }
130
131 return oss.str();
132}
std::string_view name

References name.

Referenced by build_key_with_ae().

Here is the caller graph for this function:

◆ build_key_with_ae()

std::string kcenon::pacs::services::cache::query_cache::build_key_with_ae ( const std::string & calling_ae,
const std::string & query_level,
const std::vector< std::pair< std::string, std::string > > & params )
staticnodiscard

Build a cache key with AE title prefix.

Includes the calling AE title in the key to support per-client caching.

Parameters
calling_aeThe calling AE title
query_levelThe query retrieve level
paramsList of parameter name-value pairs
Returns
Cache key string
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/query_cache.h.

Definition at line 134 of file query_cache.cpp.

137 {
138
139 return calling_ae + "/" + build_key(query_level, params);
140}
static std::string build_key(const std::string &query_level, const std::vector< std::pair< std::string, std::string > > &params)
Build a cache key from query parameters.

References build_key().

Here is the call graph for this function:

◆ clear()

void kcenon::pacs::services::cache::query_cache::clear ( )

Remove all entries from the cache.

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

Definition at line 78 of file query_cache.cpp.

78 {
79 cache_.clear();
80}

References cache_.

◆ empty()

bool kcenon::pacs::services::cache::query_cache::empty ( ) const
nodiscard

Check if the cache is empty.

Returns
true if no entries are cached
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/query_cache.h.

Definition at line 90 of file query_cache.cpp.

90 {
91 return cache_.empty();
92}

References cache_.

◆ get()

std::optional< cached_query_result > kcenon::pacs::services::cache::query_cache::get ( const key_type & key)
nodiscard

Retrieve a cached query result.

If found and not expired, the result is returned and the entry is marked as recently used. Cache metrics are updated.

Parameters
keyThe cache key (use build_key to generate)
Returns
The cached result if found, std::nullopt otherwise
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/query_cache.h.

Definition at line 31 of file query_cache.cpp.

31 {
32 return cache_.get(key);
33}

References cache_.

◆ hit_rate()

double kcenon::pacs::services::cache::query_cache::hit_rate ( ) const
nodiscardnoexcept

Get the cache hit rate.

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

Definition at line 102 of file query_cache.cpp.

102 {
103 return cache_.hit_rate();
104}

References cache_, and kcenon::pacs::services::cache::cache_stats::hit_rate().

Here is the call graph for this function:

◆ invalidate()

bool kcenon::pacs::services::cache::query_cache::invalidate ( const key_type & key)

Remove a specific entry from the cache.

Parameters
keyThe cache key
Returns
true if the entry was found and removed
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/query_cache.h.

Definition at line 43 of file query_cache.cpp.

43 {
44 return cache_.invalidate(key);
45}

References cache_.

◆ invalidate_by_prefix()

query_cache::size_type kcenon::pacs::services::cache::query_cache::invalidate_by_prefix ( const std::string & prefix)
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/query_cache.h.

Definition at line 47 of file query_cache.cpp.

47 {
48 return cache_.invalidate_if(
49 [&prefix](const std::string& key, const cached_query_result&) {
50 return key.size() >= prefix.size() &&
51 key.compare(0, prefix.size(), prefix) == 0;
52 });
53}

References cache_.

◆ invalidate_by_query_level()

query_cache::size_type kcenon::pacs::services::cache::query_cache::invalidate_by_query_level ( const std::string & query_level)

Remove all entries for a specific query level.

Parameters
query_levelThe query level (PATIENT, STUDY, SERIES, IMAGE)
Returns
Number of entries removed
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/query_cache.h.

Definition at line 55 of file query_cache.cpp.

55 {
56 // Keys are formatted as "LEVEL:params" or "AE/LEVEL:params"
57 // Match both formats
58 return cache_.invalidate_if(
59 [&query_level](const std::string& key, const cached_query_result&) {
60 // Direct match: "LEVEL:..."
61 if (key.starts_with(query_level + ":")) {
62 return true;
63 }
64 // AE prefix match: "AE/LEVEL:..."
65 auto slash_pos = key.find('/');
66 if (slash_pos != std::string::npos) {
67 auto level_start = slash_pos + 1;
68 auto colon_pos = key.find(':', level_start);
69 if (colon_pos != std::string::npos) {
70 auto key_level = key.substr(level_start, colon_pos - level_start);
71 return key_level == query_level;
72 }
73 }
74 return false;
75 });
76}
query_level
DICOM Query/Retrieve level enumeration.
Definition query_scp.h:63

References cache_.

◆ invalidate_if()

template<typename Predicate >
size_type kcenon::pacs::services::cache::query_cache::invalidate_if ( Predicate pred)
inline
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/query_cache.h.

Definition at line 241 of file query_cache.h.

241 {
242 return cache_.invalidate_if(std::move(pred));
243 }

References cache_.

◆ max_size()

query_cache::size_type kcenon::pacs::services::cache::query_cache::max_size ( ) const
nodiscardnoexcept

Get the maximum cache size.

Returns
Maximum number of entries
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/query_cache.h.

Definition at line 94 of file query_cache.cpp.

94 {
95 return cache_.max_size();
96}

References cache_.

◆ operator=() [1/2]

query_cache & kcenon::pacs::services::cache::query_cache::operator= ( const query_cache & )
delete

◆ operator=() [2/2]

query_cache & kcenon::pacs::services::cache::query_cache::operator= ( query_cache && )
defaultnoexcept

◆ purge_expired()

query_cache::size_type kcenon::pacs::services::cache::query_cache::purge_expired ( )

Remove all expired entries.

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

Definition at line 82 of file query_cache.cpp.

82 {
83 return cache_.purge_expired();
84}

References cache_.

◆ put() [1/2]

void kcenon::pacs::services::cache::query_cache::put ( const key_type & key,
cached_query_result && result )

Store a query result in the cache (move semantics)

Parameters
keyThe cache key
resultThe query result to cache (moved)

Definition at line 39 of file query_cache.cpp.

39 {
40 cache_.put(key, std::move(result));
41}

References cache_.

◆ put() [2/2]

void kcenon::pacs::services::cache::query_cache::put ( const key_type & key,
const cached_query_result & result )

Store a query result in the cache.

Parameters
keyThe cache key
resultThe query result to cache
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/query_cache.h.

Definition at line 35 of file query_cache.cpp.

35 {
36 cache_.put(key, result);
37}

References cache_.

◆ reset_stats()

void kcenon::pacs::services::cache::query_cache::reset_stats ( )
noexcept

Reset cache statistics.

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

Definition at line 106 of file query_cache.cpp.

106 {
107 cache_.reset_stats();
108}

References cache_.

◆ size()

query_cache::size_type kcenon::pacs::services::cache::query_cache::size ( ) const
nodiscard

Get the current number of cached entries.

Returns
Current cache size
Examples
/home/runner/work/pacs_system/pacs_system/include/kcenon/pacs/services/cache/query_cache.h.

Definition at line 86 of file query_cache.cpp.

86 {
87 return cache_.size();
88}

References cache_.

◆ stats()

const cache_stats & kcenon::pacs::services::cache::query_cache::stats ( ) const
nodiscardnoexcept

Get cache statistics.

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

Definition at line 98 of file query_cache.cpp.

98 {
99 return cache_.stats();
100}

References cache_.

Member Data Documentation

◆ cache_

◆ config_

query_cache_config kcenon::pacs::services::cache::query_cache::config_
private

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