annotate OrthancFramework/Sources/Cache/MemoryCache.cpp @ 5640:f7adfb22e20e

updated copyright, as Orthanc Team now replaces Osimis
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 30 May 2024 21:19:57 +0200
parents 48b8dae6dc77
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1486
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1288
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
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: 5185
diff changeset
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 *
06aa7b7b6723 implementation of a single-threaded cache mechanism
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: 4044
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: 4044
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: 4044
diff changeset
12 * the License, or (at your option) any later version.
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 *
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * This program is distributed in the hope that it will be useful, but
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
06aa7b7b6723 implementation of a single-threaded cache mechanism
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: 4044
diff changeset
17 * Lesser General Public License for more details.
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
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: 4044
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: 4044
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: 4044
diff changeset
21 * <http://www.gnu.org/licenses/>.
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 **/
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
824
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
25 #include "../PrecompiledHeaders.h"
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include "MemoryCache.h"
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
1486
f967bdf8534e refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
28 #include "../Logging.h"
288
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 285
diff changeset
29
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 namespace Orthanc
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 {
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
32 namespace Deprecated
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 {
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
34 MemoryCache::Page& MemoryCache::Load(const std::string& id)
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 {
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
36 // Reuse the cache entry if it already exists
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
37 Page* p = NULL;
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
38 if (index_.Contains(id, p))
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
39 {
4268
0ae2ca210077 new macro TLOG() to replace VLOG() for trace logs with a category
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
40 LOG(TRACE) << "Reusing a cache page";
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
41 assert(p != NULL);
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
42 index_.MakeMostRecent(id);
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
43 return *p;
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
44 }
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
45
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
46 // The id is not in the cache yet. Make some room if the cache
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
47 // is full.
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
48 if (index_.GetSize() == cacheSize_)
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
49 {
4268
0ae2ca210077 new macro TLOG() to replace VLOG() for trace logs with a category
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
50 LOG(TRACE) << "Dropping the oldest cache page";
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
51 index_.RemoveOldest(p);
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
52 delete p;
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
53 }
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
54
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
55 // Create a new cache page
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
56 std::unique_ptr<Page> result(new Page);
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
57 result->id_ = id;
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
58 result->content_.reset(provider_.Provide(id));
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
59
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
60 // Add the newly create page to the cache
4268
0ae2ca210077 new macro TLOG() to replace VLOG() for trace logs with a category
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
61 LOG(TRACE) << "Registering new data in a cache page";
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
62 p = result.release();
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
63 index_.Add(id, p);
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 return *p;
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 }
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
67 MemoryCache::MemoryCache(ICachePageProvider& provider,
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
68 size_t cacheSize) :
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
69 provider_(provider),
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
70 cacheSize_(cacheSize)
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 {
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 }
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
74 void MemoryCache::Invalidate(const std::string& id)
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
75 {
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
76 Page* p = NULL;
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
77 if (index_.Contains(id, p))
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
78 {
4268
0ae2ca210077 new macro TLOG() to replace VLOG() for trace logs with a category
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
79 LOG(TRACE) << "Invalidating a cache page";
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
80 assert(p != NULL);
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
81 delete p;
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
82 index_.Invalidate(id);
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
83 }
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
84 }
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
86 MemoryCache::~MemoryCache()
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
87 {
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
88 while (!index_.IsEmpty())
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
89 {
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
90 Page* element = NULL;
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
91 index_.RemoveOldest(element);
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
92 assert(element != NULL);
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
93 delete element;
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
94 }
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
95 }
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
97 IDynamicObject& MemoryCache::Access(const std::string& id)
2507
99116ed6f38c invalidate DicomCache when deleting instance
amazy
parents: 2447
diff changeset
98 {
3557
4d809b2e1141 better cache toolbox: MemoryObjectCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
99 return *Load(id).content_;
2507
99116ed6f38c invalidate DicomCache when deleting instance
amazy
parents: 2447
diff changeset
100 }
99116ed6f38c invalidate DicomCache when deleting instance
amazy
parents: 2447
diff changeset
101 }
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 }