Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Cache/MemoryStringCache.cpp @ 5344:f41e1f635c4d
cppcheck
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 28 Jun 2023 12:24:52 +0200 |
parents | 0ea402b4d901 |
children | d37dff2c0028 |
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 |
5185
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4870
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4870
diff
changeset
|
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
3557
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 * |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 * 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:
4044
diff
changeset
|
9 * 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:
4044
diff
changeset
|
10 * 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:
4044
diff
changeset
|
11 * the License, or (at your option) any later version. |
3557
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 * |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 * 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
|
14 * WITHOUT ANY WARRANTY; without even the implied warranty of |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 * 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:
4044
diff
changeset
|
16 * Lesser General Public License for more details. |
3557
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * 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:
4044
diff
changeset
|
19 * 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:
4044
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
3557
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 **/ |
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 #include "../PrecompiledHeaders.h" |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 #include "MemoryStringCache.h" |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 namespace Orthanc |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 { |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 class MemoryStringCache::StringValue : public ICacheable |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 { |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 private: |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 std::string content_; |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 public: |
4199 | 35 explicit StringValue(const std::string& content) : |
3557
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 content_(content) |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 { |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 } |
4792
434843934307
Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
4437
diff
changeset
|
39 |
434843934307
Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
4437
diff
changeset
|
40 explicit StringValue(const char* buffer, size_t size) : |
434843934307
Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
4437
diff
changeset
|
41 content_(buffer, size) |
434843934307
Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
4437
diff
changeset
|
42 { |
434843934307
Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
4437
diff
changeset
|
43 } |
434843934307
Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
4437
diff
changeset
|
44 |
3557
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 const std::string& GetContent() const |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 { |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 return content_; |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 } |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 virtual size_t GetMemoryUsage() const |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 { |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 return content_.size(); |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 } |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
54 }; |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 |
4297 | 56 size_t MemoryStringCache::GetMaximumSize() |
4279
ab4d015af660
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
57 { |
ab4d015af660
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
58 return cache_.GetMaximumSize(); |
ab4d015af660
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
59 } |
ab4d015af660
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
60 |
ab4d015af660
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
61 void MemoryStringCache::SetMaximumSize(size_t size) |
ab4d015af660
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
62 { |
ab4d015af660
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
63 cache_.SetMaximumSize(size); |
ab4d015af660
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
64 } |
3557
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
65 |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
66 void MemoryStringCache::Add(const std::string& key, |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
67 const std::string& value) |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 { |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
69 cache_.Acquire(key, new StringValue(value)); |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
70 } |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
71 |
4792
434843934307
Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
4437
diff
changeset
|
72 void MemoryStringCache::Add(const std::string& key, |
434843934307
Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
4437
diff
changeset
|
73 const void* buffer, |
434843934307
Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
4437
diff
changeset
|
74 size_t size) |
434843934307
Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
4437
diff
changeset
|
75 { |
434843934307
Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
4437
diff
changeset
|
76 cache_.Acquire(key, new StringValue(reinterpret_cast<const char*>(buffer), size)); |
434843934307
Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
4437
diff
changeset
|
77 } |
434843934307
Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
4437
diff
changeset
|
78 |
4279
ab4d015af660
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
79 void MemoryStringCache::Invalidate(const std::string &key) |
ab4d015af660
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
80 { |
ab4d015af660
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
81 cache_.Invalidate(key); |
ab4d015af660
moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4199
diff
changeset
|
82 } |
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 bool MemoryStringCache::Fetch(std::string& value, |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
85 const std::string& key) |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
86 { |
3563
4812825e69fc
accessor to the cache can now require to be unique
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3557
diff
changeset
|
87 MemoryObjectCache::Accessor reader(cache_, key, false /* multiple readers are allowed */); |
3557
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
88 |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
89 if (reader.IsValid()) |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
90 { |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
91 value = dynamic_cast<StringValue&>(reader.GetValue()).GetContent(); |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
92 return true; |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
93 } |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
94 else |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
95 { |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
96 return false; |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
97 } |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
98 } |
4d809b2e1141
better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
99 } |