PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
query_cache.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
22#pragma once
23
25
26#include <chrono>
27#include <memory>
28#include <string>
29#include <vector>
30
32
33// ─────────────────────────────────────────────────────
34// Forward Declarations
35// ─────────────────────────────────────────────────────
36
37class query_cache;
38
39// ─────────────────────────────────────────────────────
40// Query Cache Configuration
41// ─────────────────────────────────────────────────────
42
49 std::size_t max_entries{1000};
50
52 std::chrono::seconds ttl{300};
53
55 bool enable_logging{false};
56
58 bool enable_metrics{true};
59
61 std::string cache_name{"cfind_query_cache"};
62};
63
64// ─────────────────────────────────────────────────────
65// Query Result Type
66// ─────────────────────────────────────────────────────
67
77 std::vector<std::uint8_t> data;
78
80 std::uint32_t match_count{0};
81
83 std::chrono::steady_clock::time_point cached_at;
84
86 std::string query_level;
87};
88
89// ─────────────────────────────────────────────────────
90// Query Cache Implementation
91// ─────────────────────────────────────────────────────
92
133public:
134 using key_type = std::string;
136 using size_type = std::size_t;
137
138 // =========================================================================
139 // Construction
140 // =========================================================================
141
146 explicit query_cache(const query_cache_config& config = query_cache_config{});
147
149 query_cache(const query_cache&) = delete;
151
153 query_cache(query_cache&&) noexcept = default;
154 query_cache& operator=(query_cache&&) noexcept = default;
155
156 ~query_cache() = default;
157
158 // =========================================================================
159 // Cache Operations
160 // =========================================================================
161
171 [[nodiscard]] std::optional<cached_query_result> get(const key_type& key);
172
179 void put(const key_type& key, const cached_query_result& result);
180
187 void put(const key_type& key, cached_query_result&& result);
188
195 bool invalidate(const key_type& key);
196
215 size_type invalidate_by_prefix(const std::string& prefix);
216
224
240 template <typename Predicate>
241 size_type invalidate_if(Predicate pred) {
242 return cache_.invalidate_if(std::move(pred));
243 }
244
248 void clear();
249
255
256 // =========================================================================
257 // Cache Information
258 // =========================================================================
259
264 [[nodiscard]] size_type size() const;
265
270 [[nodiscard]] bool empty() const;
271
276 [[nodiscard]] size_type max_size() const noexcept;
277
282 [[nodiscard]] const cache_stats& stats() const noexcept;
283
288 [[nodiscard]] double hit_rate() const noexcept;
289
293 void reset_stats() noexcept;
294
295 // =========================================================================
296 // Key Generation Helpers
297 // =========================================================================
298
310 [[nodiscard]] static std::string build_key(
311 const std::string& query_level,
312 const std::vector<std::pair<std::string, std::string>>& params);
313
324 [[nodiscard]] static std::string build_key_with_ae(
325 const std::string& calling_ae,
326 const std::string& query_level,
327 const std::vector<std::pair<std::string, std::string>>& params);
328
329private:
332};
333
334// ─────────────────────────────────────────────────────
335// Global Query Cache Instance
336// ─────────────────────────────────────────────────────
337
349[[nodiscard]] query_cache& global_query_cache();
350
361
362} // namespace kcenon::pacs::services::cache
DICOM query result cache with monitoring integration.
std::optional< cached_query_result > get(const key_type &key)
Retrieve a cached query result.
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.
size_type invalidate_if(Predicate pred)
void reset_stats() noexcept
Reset cache statistics.
string_lru_cache< cached_query_result > cache_
bool empty() const
Check if the cache is empty.
size_type size() const
Get the current number of cached entries.
void clear()
Remove all entries from the cache.
double hit_rate() const noexcept
Get the cache hit rate.
size_type max_size() const noexcept
Get the maximum cache size.
const cache_stats & stats() const noexcept
Get cache statistics.
bool invalidate(const key_type &key)
Remove a specific entry from the cache.
size_type purge_expired()
Remove all expired entries.
query_cache & operator=(const query_cache &)=delete
query_cache(const query_cache &)=delete
Non-copyable.
void put(const key_type &key, const cached_query_result &result)
Store a query result in the cache.
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.
size_type invalidate_by_prefix(const std::string &prefix)
query_cache(const query_cache_config &config=query_cache_config{})
Construct a query cache with the given configuration.
size_type invalidate_by_query_level(const std::string &query_level)
Remove all entries for a specific query level.
query_cache(query_cache &&) noexcept=default
Movable.
Thread-safe LRU cache with TTL support.
query_cache & global_query_cache()
Get the global query cache instance.
bool configure_global_cache(const query_cache_config &config)
Configure the global query cache.
query_level
DICOM Query/Retrieve level enumeration.
Definition query_scp.h:63
Thread-safe LRU (Least Recently Used) cache for query results.
Statistics for cache performance monitoring.
Wrapper for cached query results.
Definition query_cache.h:75
std::vector< std::uint8_t > data
Serialized query result data.
Definition query_cache.h:77
std::string query_level
Query level (PATIENT, STUDY, SERIES, IMAGE)
Definition query_cache.h:86
std::chrono::steady_clock::time_point cached_at
Timestamp when this result was cached.
Definition query_cache.h:83
std::uint32_t match_count
Number of matching records in this result.
Definition query_cache.h:80
Configuration for the query cache.
Definition query_cache.h:47
bool enable_logging
Enable debug logging for cache operations.
Definition query_cache.h:55
std::size_t max_entries
Maximum number of cached query results.
Definition query_cache.h:49
bool enable_metrics
Enable metrics reporting to pacs_metrics.
Definition query_cache.h:58
std::chrono::seconds ttl
Time-to-live for cached results (default: 5 minutes)
Definition query_cache.h:52
std::string cache_name
Cache identifier for logging and metrics.
Definition query_cache.h:61