annotate OrthancFramework/Sources/Cache/MemoryStringCache.h @ 5676:b744a2cf408a find-refactoring tip

shorten ParentRetrieveSpecification/ChildrenRetrieveSpecification as ParentSpecification/ChildrenSpecification
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 06 Jul 2024 15:04:28 +0200
parents f7adfb22e20e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
5640
f7adfb22e20e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5485
diff changeset
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
f7adfb22e20e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5485
diff changeset
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
5485
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5473
diff changeset
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 *
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * This program is free software: you can redistribute it and/or
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4063
diff changeset
10 * modify it under the terms of the GNU Lesser General Public License
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4063
diff changeset
11 * as published by the Free Software Foundation, either version 3 of
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4063
diff changeset
12 * the License, or (at your option) any later version.
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 *
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * This program is distributed in the hope that it will be useful, but
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4063
diff changeset
17 * Lesser General Public License for more details.
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 *
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4063
diff changeset
19 * You should have received a copy of the GNU Lesser General Public
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4063
diff changeset
20 * License along with this program. If not, see
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4063
diff changeset
21 * <http://www.gnu.org/licenses/>.
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 **/
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #pragma once
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
27 #include "../OrthancFramework.h"
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
28 #include "ICacheable.h"
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
29 #include "LeastRecentlyUsedIndex.h"
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
30
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
31 #include <boost/thread/condition_variable.hpp>
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
32 #include <boost/thread/mutex.hpp>
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
33
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 namespace Orthanc
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 {
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 /**
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
38 * Class that caches a dictionary
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 * of strings, using the "fetch/add" paradigm of memcached.
4792
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
40 *
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
41 * Starting from 1.12.2, if multiple clients are trying to access
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
42 * an inexistent item at the same time, only one of them will load it
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
43 * and the others will wait until the first one has loaded the data.
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
44 *
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
45 * The MemoryStringCache is only accessible through an Accessor.
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
46 *
4792
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
47 * Note: this class is thread safe
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 **/
4063
e00f3d089991 shared library of orthanc framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
49 class ORTHANC_PUBLIC MemoryStringCache : public boost::noncopyable
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 {
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
51 public:
5473
d1992a9ec8c3 fix build of Windows shared library
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5460
diff changeset
52 class ORTHANC_PUBLIC Accessor : public boost::noncopyable
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
53 {
5427
111e21b4f8bc fix transcoded instance caching
Alain Mazy <am@osimis.io>
parents: 5426
diff changeset
54 protected:
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
55 MemoryStringCache& cache_;
5427
111e21b4f8bc fix transcoded instance caching
Alain Mazy <am@osimis.io>
parents: 5426
diff changeset
56
111e21b4f8bc fix transcoded instance caching
Alain Mazy <am@osimis.io>
parents: 5426
diff changeset
57 private:
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
58 bool shouldAdd_; // when this accessor is the one who should load and add the data
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
59 std::string keyToAdd_;
5427
111e21b4f8bc fix transcoded instance caching
Alain Mazy <am@osimis.io>
parents: 5426
diff changeset
60
111e21b4f8bc fix transcoded instance caching
Alain Mazy <am@osimis.io>
parents: 5426
diff changeset
61
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
62 public:
5460
1474fd6ea6c6 CppCheck
Alain Mazy <am@osimis.io>
parents: 5431
diff changeset
63 explicit Accessor(MemoryStringCache& cache);
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
64 ~Accessor();
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
65
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
66 bool Fetch(std::string& value, const std::string& key);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
67 void Add(const std::string& key, const std::string& value);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
68 void Add(const std::string& key,const char* buffer, size_t size);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
69 };
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
70
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 private:
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 class StringValue;
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
5431
4be5f117aa0d metrics
Alain Mazy <am@osimis.io>
parents: 5427
diff changeset
74 mutable boost::mutex cacheMutex_; // note: we can not use recursive_mutex with condition_variable
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
75 boost::condition_variable cacheCond_;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
76 std::set<std::string> itemsBeingLoaded_;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
77
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
78 size_t currentSize_;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
79 size_t maxSize_;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
80 LeastRecentlyUsedIndex<std::string, StringValue*> content_;
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
81
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
82 void Recycle(size_t targetSize);
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 public:
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
85 MemoryStringCache();
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
86
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
87 ~MemoryStringCache();
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
88
4279
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
89 size_t GetMaximumSize();
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
4279
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
91 void SetMaximumSize(size_t size);
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
93 void Invalidate(const std::string& key);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
94
5431
4be5f117aa0d metrics
Alain Mazy <am@osimis.io>
parents: 5427
diff changeset
95 size_t GetCurrentSize() const;
4be5f117aa0d metrics
Alain Mazy <am@osimis.io>
parents: 5427
diff changeset
96
4be5f117aa0d metrics
Alain Mazy <am@osimis.io>
parents: 5427
diff changeset
97 size_t GetNumberOfItems() const;
4be5f117aa0d metrics
Alain Mazy <am@osimis.io>
parents: 5427
diff changeset
98
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
99 private:
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 void Add(const std::string& key,
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 const std::string& value);
4792
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
102
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
103 void Add(const std::string& key,
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
104 const void* buffer,
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
105 size_t size);
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
106
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 bool Fetch(std::string& value,
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 const std::string& key);
5420
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
109
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
110 void RemoveFromItemsBeingLoaded(const std::string& key);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
111 void RemoveFromItemsBeingLoadedInternal(const std::string& key);
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
112
d37dff2c0028 Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
113 void AddToItemsBeingLoadedInternal(const std::string& key);
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 };
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }