comparison OrthancFramework/Sources/FileStorage/StorageCache.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
children 6c276fac0cc0
comparison
equal deleted inserted replaced
4790:9754d5f2f38a 4792:434843934307
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program. If not, see
19 * <http://www.gnu.org/licenses/>.
20 **/
21
22
23 #include "../PrecompiledHeaders.h"
24 #include "StorageCache.h"
25
26 #include "../Compatibility.h"
27 #include "../OrthancException.h"
28
29
30
31 namespace Orthanc
32 {
33 bool IsAcceptedContentType(FileContentType contentType)
34 {
35 return contentType == FileContentType_Dicom ||
36 contentType == FileContentType_DicomUntilPixelData ||
37 contentType == FileContentType_DicomAsJson;
38 }
39
40 const char* ToString(FileContentType contentType)
41 {
42 switch (contentType)
43 {
44 case FileContentType_Dicom:
45 return "dicom";
46 case FileContentType_DicomUntilPixelData:
47 return "dicom-header";
48 case FileContentType_DicomAsJson:
49 return "dicom-json";
50 default:
51 throw OrthancException(ErrorCode_InternalError,
52 "ContentType not supported in StorageCache");
53 }
54 }
55
56 void GetCacheKey(std::string& key, const std::string& uuid, FileContentType contentType)
57 {
58 key = uuid + ":" + std::string(ToString(contentType));
59 }
60
61 void StorageCache::SetMaximumSize(size_t size)
62 {
63 cache_.SetMaximumSize(size);
64 }
65
66 void StorageCache::Add(const std::string& uuid,
67 FileContentType contentType,
68 const std::string& value)
69 {
70 if (!IsAcceptedContentType(contentType))
71 {
72 return;
73 }
74
75 std::string key;
76 GetCacheKey(key, uuid, contentType);
77 cache_.Add(key, value);
78 }
79
80 void StorageCache::Add(const std::string& uuid,
81 FileContentType contentType,
82 const void* buffer,
83 size_t size)
84 {
85 if (!IsAcceptedContentType(contentType))
86 {
87 return;
88 }
89
90 std::string key;
91 GetCacheKey(key, uuid, contentType);
92 cache_.Add(key, buffer, size);
93 }
94
95 void StorageCache::Invalidate(const std::string& uuid, FileContentType contentType)
96 {
97 std::string key;
98 GetCacheKey(key, uuid, contentType);
99 cache_.Invalidate(key);
100 }
101
102 bool StorageCache::Fetch(std::string& value,
103 const std::string& uuid,
104 FileContentType contentType)
105 {
106 if (!IsAcceptedContentType(contentType))
107 {
108 return false;
109 }
110
111 std::string key;
112 GetCacheKey(key, uuid, contentType);
113
114 return cache_.Fetch(value, key);
115 }
116
117
118 }