Mercurial > hg > orthanc
diff OrthancFramework/Sources/FileStorage/StorageCache.cpp @ 4906:f656fb878b50
reintroduced storage cache for StartRange files
author | Alain Mazy <am@osimis.io> |
---|---|
date | Mon, 21 Feb 2022 10:11:38 +0100 |
parents | ea5f1c6ed07e |
children | 0ea402b4d901 |
line wrap: on
line diff
--- a/OrthancFramework/Sources/FileStorage/StorageCache.cpp Sun Feb 20 16:57:43 2022 +0100 +++ b/OrthancFramework/Sources/FileStorage/StorageCache.cpp Mon Feb 21 10:11:38 2022 +0100 @@ -33,12 +33,17 @@ namespace Orthanc { - static std::string GetCacheKey(const std::string& uuid, - FileContentType contentType) + static std::string GetCacheKeyFullFile(const std::string& uuid, + FileContentType contentType) { - return uuid + ":" + boost::lexical_cast<std::string>(contentType); + return uuid + ":" + boost::lexical_cast<std::string>(contentType) + ":1"; } + static std::string GetCacheKeyStartRange(const std::string& uuid, + FileContentType contentType) + { + return uuid + ":" + boost::lexical_cast<std::string>(contentType) + ":0"; + } void StorageCache::SetMaximumSize(size_t size) { @@ -50,7 +55,7 @@ FileContentType contentType, const std::string& value) { - const std::string key = GetCacheKey(uuid, contentType); + const std::string key = GetCacheKeyFullFile(uuid, contentType); cache_.Add(key, value); } @@ -60,16 +65,29 @@ const void* buffer, size_t size) { - const std::string key = GetCacheKey(uuid, contentType); + const std::string key = GetCacheKeyFullFile(uuid, contentType); cache_.Add(key, buffer, size); } - + + + void StorageCache::AddStartRange(const std::string& uuid, + FileContentType contentType, + const std::string& value) + { + const std::string key = GetCacheKeyStartRange(uuid, contentType); + cache_.Add(key, value); + } + void StorageCache::Invalidate(const std::string& uuid, FileContentType contentType) { - const std::string key = GetCacheKey(uuid, contentType); - cache_.Invalidate(key); + // invalidate both full file + start range file + const std::string keyFullFile = GetCacheKeyFullFile(uuid, contentType); + cache_.Invalidate(keyFullFile); + + const std::string keyPartialFile = GetCacheKeyStartRange(uuid, contentType); + cache_.Invalidate(keyPartialFile); } @@ -77,7 +95,7 @@ const std::string& uuid, FileContentType contentType) { - const std::string key = GetCacheKey(uuid, contentType); + const std::string key = GetCacheKeyFullFile(uuid, contentType); if (cache_.Fetch(value, key)) { LOG(INFO) << "Read attachment \"" << uuid << "\" with content type " @@ -89,4 +107,42 @@ return false; } } + + bool StorageCache::FetchStartRange(std::string& value, + const std::string& uuid, + FileContentType contentType, + uint64_t end) + { + // first try to get the start of file only from cache + const std::string keyPartialFile = GetCacheKeyStartRange(uuid, contentType); + if (cache_.Fetch(value, keyPartialFile) && value.size() >= end) + { + if (value.size() > end) // the start range that has been cached is larger than the requested value + { + value.resize(end); + } + + LOG(INFO) << "Read start of attachment \"" << uuid << "\" with content type " + << boost::lexical_cast<std::string>(contentType) << " from cache"; + return true; + } + else + { + // try to get the full file from cache + if (Fetch(value, uuid, contentType)) + { + if (value.size() < end) + { + throw OrthancException(ErrorCode_CorruptedFile); + } + + value.resize(end); + return true; + } + else + { + return false; + } + } + } }