annotate OrthancFramework/Sources/Cache/MemoryStringCache.cpp @ 4792:434843934307 storage-cache

Added a StorageCache in the StorageAccessor
author Alain Mazy <am@osimis.io>
date Thu, 30 Sep 2021 12:14:19 +0200
parents d9473bd5ed43
children 7053502fbf97
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
4437
d9473bd5ed43 upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * 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
8 * 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
9 * 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
10 * the License, or (at your option) any later version.
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * 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
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * 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
15 * Lesser General Public License for more details.
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
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
17 * 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
18 * 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
19 * <http://www.gnu.org/licenses/>.
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
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 #include "../PrecompiledHeaders.h"
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "MemoryStringCache.h"
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 namespace Orthanc
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 {
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 class MemoryStringCache::StringValue : public ICacheable
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 {
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 private:
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 std::string content_;
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 public:
4199
fbc49a65340a cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
34 explicit StringValue(const std::string& content) :
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 content_(content)
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 }
4792
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
38
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
39 explicit StringValue(const char* buffer, size_t size) :
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
40 content_(buffer, size)
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
41 {
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
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 const std::string& GetContent() const
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 {
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 return content_;
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 }
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 virtual size_t GetMemoryUsage() const
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 {
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 return content_.size();
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 }
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
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4279
diff changeset
55 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
56 {
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4199
diff changeset
57 return cache_.GetMaximumSize();
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4199
diff changeset
58 }
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 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
61 {
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4199
diff changeset
62 cache_.SetMaximumSize(size);
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4199
diff changeset
63 }
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 void MemoryStringCache::Add(const std::string& key,
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 const std::string& value)
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 {
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 cache_.Acquire(key, new StringValue(value));
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 }
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
4792
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
71 void MemoryStringCache::Add(const std::string& key,
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
72 const void* buffer,
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
73 size_t size)
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
74 {
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
75 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
76 }
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
77
4279
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4199
diff changeset
78 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
79 {
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4199
diff changeset
80 cache_.Invalidate(key);
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4199
diff changeset
81 }
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 bool MemoryStringCache::Fetch(std::string& value,
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 const std::string& key)
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 {
3563
4812825e69fc accessor to the cache can now require to be unique
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3557
diff changeset
86 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
87
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 if (reader.IsValid())
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 {
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 value = dynamic_cast<StringValue&>(reader.GetValue()).GetContent();
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 return true;
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 }
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 else
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 {
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 return false;
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
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 }