annotate OrthancFramework/Sources/FileStorage/StorageCache.h @ 5420:d37dff2c0028 am-new-cache

Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
author Alain Mazy <am@osimis.io>
date Mon, 13 Nov 2023 17:01:59 +0100
parents 0ea402b4d901
children c65e036d649b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4792
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
1 /**
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
5185
0ea402b4d901 upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4906
diff changeset
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
0ea402b4d901 upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4906
diff changeset
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
4792
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
7 *
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
8 * This program is free software: you can redistribute it and/or
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public License
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
10 * as published by the Free Software Foundation, either version 3 of
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
11 * the License, or (at your option) any later version.
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
12 *
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
16 * Lesser General Public License for more details.
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
17 *
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
19 * License along with this program. If not, see
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
20 * <http://www.gnu.org/licenses/>.
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
21 **/
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
22
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
23
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
24 #pragma once
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
25
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
26 #include "../Cache/MemoryStringCache.h"
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
27
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
28 #include "../Compatibility.h" // For ORTHANC_OVERRIDE
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
29
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
30 #include <boost/thread/mutex.hpp>
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
31 #include <map>
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
32
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
33 namespace Orthanc
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
34 {
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
35 /**
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
36 * Note: this class is thread safe
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
37 **/
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
38 class ORTHANC_PUBLIC StorageCache : public boost::noncopyable
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
39 {
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
40 public:
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
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 // The StorageCache is only accessible through this 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
43 // It will make sure that only one user will fill load data and fill
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 // the cache if multiple users try to access the same item at the same time.
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 // This scenario happens a lot when multiple workers from a viewer 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
46 // the same file.
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
47 class Accessor : public MemoryStringCache::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
48 {
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
49 public:
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
50 Accessor(StorageCache& cache);
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
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
52 void Add(const std::string& uuid,
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 FileContentType contentType,
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
54 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
55
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
56 void AddStartRange(const std::string& uuid,
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
57 FileContentType contentType,
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 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
59
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
60 void Add(const std::string& uuid,
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
61 FileContentType contentType,
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 const void* buffer,
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
63 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
64
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 bool Fetch(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
66 const std::string& uuid,
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 FileContentType contentType);
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
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 bool FetchStartRange(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
70 const std::string& uuid,
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
71 FileContentType contentType,
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
72 uint64_t end /* exclusive */);
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
73 };
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
74
4900
ea5f1c6ed07e fix cache of storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
75 private:
4792
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
76 MemoryStringCache cache_;
4900
ea5f1c6ed07e fix cache of storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
77
4792
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
78 public:
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
79 void SetMaximumSize(size_t size);
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
80
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
81 void Invalidate(const std::string& uuid,
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 FileContentType contentType);
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
83
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
84 private:
4792
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
85 void Add(const std::string& uuid,
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
86 FileContentType contentType,
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
87 const std::string& value);
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
88
4906
f656fb878b50 reintroduced storage cache for StartRange files
Alain Mazy <am@osimis.io>
parents: 4900
diff changeset
89 void AddStartRange(const std::string& uuid,
f656fb878b50 reintroduced storage cache for StartRange files
Alain Mazy <am@osimis.io>
parents: 4900
diff changeset
90 FileContentType contentType,
f656fb878b50 reintroduced storage cache for StartRange files
Alain Mazy <am@osimis.io>
parents: 4900
diff changeset
91 const std::string& value);
f656fb878b50 reintroduced storage cache for StartRange files
Alain Mazy <am@osimis.io>
parents: 4900
diff changeset
92
4792
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
93 void Add(const std::string& uuid,
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
94 FileContentType contentType,
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
95 const void* buffer,
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
96 size_t size);
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
97
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
98 bool Fetch(std::string& value,
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
99 const std::string& uuid,
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
100 FileContentType contentType);
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
101
4906
f656fb878b50 reintroduced storage cache for StartRange files
Alain Mazy <am@osimis.io>
parents: 4900
diff changeset
102 bool FetchStartRange(std::string& value,
f656fb878b50 reintroduced storage cache for StartRange files
Alain Mazy <am@osimis.io>
parents: 4900
diff changeset
103 const std::string& uuid,
f656fb878b50 reintroduced storage cache for StartRange files
Alain Mazy <am@osimis.io>
parents: 4900
diff changeset
104 FileContentType contentType,
f656fb878b50 reintroduced storage cache for StartRange files
Alain Mazy <am@osimis.io>
parents: 4900
diff changeset
105 uint64_t end /* exclusive */);
f656fb878b50 reintroduced storage cache for StartRange files
Alain Mazy <am@osimis.io>
parents: 4900
diff changeset
106
4792
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
107 };
434843934307 Added a StorageCache in the StorageAccessor
Alain Mazy <am@osimis.io>
parents:
diff changeset
108 }