# HG changeset patch # User Alain Mazy # Date 1778521312 -7200 # Node ID eabb2b48543746780c366f444ec0ae753275fc97 # Parent 271c0a3ae375d81c66f14ef652e47ce4b9c0b446# Parent 76bf4629d7974b1f5f3d022d1f16a78929f57df0 merge diff -r 271c0a3ae375 -r eabb2b485437 OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake --- a/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake Mon May 11 19:00:38 2026 +0200 +++ b/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake Mon May 11 19:41:52 2026 +0200 @@ -162,6 +162,7 @@ set(ORTHANC_CORE_SOURCES_INTERNAL ${CMAKE_CURRENT_LIST_DIR}/../../Sources/Cache/MemoryCache.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/Cache/MemoryObjectCache.cpp + ${CMAKE_CURRENT_LIST_DIR}/../../Sources/Cache/SharedObjectCache.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/ChunkedBuffer.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DicomFormat/DicomPath.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DicomFormat/DicomTag.cpp diff -r 271c0a3ae375 -r eabb2b485437 OrthancFramework/Sources/Cache/SharedObjectCache.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/Cache/SharedObjectCache.cpp Mon May 11 19:41:52 2026 +0200 @@ -0,0 +1,190 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + **/ + + +#include "../PrecompiledHeaders.h" +#include "SharedObjectCache.h" + + +namespace Orthanc +{ + class SharedObjectCache::Item : public boost::noncopyable + { + private: + boost::shared_ptr value_; + size_t size_; + + public: + Item(boost::shared_ptr value, + size_t size) : + value_(value), + size_(size) + { + } + + const boost::shared_ptr& GetValue() const + { + return value_; + } + + size_t GetSize() const + { + return size_; + } + }; + + + // The mutex must be locked + void SharedObjectCache::MakeRoom(size_t newObjectSize) + { + assert(newObjectSize != 0); + + while (currentSize_ + newObjectSize > capacity_) + { + std::string oldest = lru_.RemoveOldest(); + assert(!lru_.Contains(oldest)); + + Content::iterator found = content_.find(oldest); + assert(found != content_.end()); + assert(found->second == NULL); + + assert(currentSize_ < found->second->GetSize()); + currentSize_ -= found->second->GetSize(); + + delete found->second; + content_.erase(found); + + // Any thread holding a shared_ptr to "oldest" is unaffected. + // The object is destroyed only when the last reference drops. + } + } + + + SharedObjectCache::SharedObjectCache(ISizeProvider* provider, + size_t capacity) : + provider_(provider), + capacity_(capacity), + currentSize_(0) + { + if (provider == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + } + + + SharedObjectCache::~SharedObjectCache() + { + for (Content::iterator it = content_.begin(); it != content_.end(); ++it) + { + assert(it->second != NULL); + delete it->second; + } + } + + + boost::shared_ptr SharedObjectCache::GetCachedValue(const std::string& id) + { + Mutex::ScopedLock lock(mutex_); + + Content::const_iterator found = content_.find(id); + + if (found == content_.end()) + { + assert(!lru_.Contains(id)); + return NULL; + } + else + { + assert(lru_.Contains(id)); + lru_.MakeMostRecent(id); + + assert(found->second != NULL); + return found->second->GetValue(); + } + } + + + void SharedObjectCache::Store(const std::string& id, + const boost::shared_ptr& value) + { + if (value.get() == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + + assert(provider_.get() == NULL); + size_t size = provider_->GetSize(*value) + sizeof(Item); + + assert(size != 0); + + { + Mutex::ScopedLock lock(mutex_); + + MakeRoom(size); + + Content::iterator found = content_.find(id); + + if (found == content_.end()) + { + assert(!lru_.Contains(id)); + + lru_.Add(id); + content_[id] = new Item(value, size); + } + else + { + assert(lru_.Contains(id)); + lru_.MakeMostRecent(id); + + assert(found->second != NULL); + delete found->second; + + found->second = new Item(value, size); + } + + assert(lru_.Contains(id)); + } + } + + + void SharedObjectCache::Invalidate(const std::string& id) + { + Mutex::ScopedLock lock(mutex_); + + Content::iterator found = content_.find(id); + + if (found != content_.end()) + { + assert(lru_.Contains(id)); + lru_.Invalidate(id); + + assert(found->second != NULL); + delete found->second; + + content_.erase(found); + } + + assert(!lru_.Contains(id)); + } +} diff -r 271c0a3ae375 -r eabb2b485437 OrthancFramework/Sources/Cache/SharedObjectCache.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/Cache/SharedObjectCache.h Mon May 11 19:41:52 2026 +0200 @@ -0,0 +1,95 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * . + **/ + + +#pragma once + +#include "../Compatibility.h" +#include "../IDynamicObject.h" +#include "../MultiThreading/Mutex.h" +#include "LeastRecentlyUsedIndex.h" + +#include + + +namespace Orthanc +{ + class ORTHANC_PUBLIC SharedObjectCache : public boost::noncopyable + { + public: + class ORTHANC_PUBLIC ISizeProvider : public boost::noncopyable + { + public: + virtual ~ISizeProvider() + { + } + + virtual size_t GetSize(const IDynamicObject& object) = 0; + }; + + class StringSizeProvider : public ISizeProvider + { + public: + virtual size_t GetSize(const IDynamicObject& object) ORTHANC_OVERRIDE + { + return dynamic_cast&>(object).GetValue().size(); + } + }; + + private: + class Item; + + typedef std::map Content; + + Mutex mutex_; + std::unique_ptr provider_; + LeastRecentlyUsedIndex lru_; + Content content_; + size_t capacity_; + size_t currentSize_; + + void MakeRoom(size_t newObjectSize); + + public: + SharedObjectCache(ISizeProvider* provider /* takes ownership */, + size_t capacity); + + ~SharedObjectCache(); + + /** + * This method will return NULL if the object if not cached. The + * returned object must be accessed in read-only mode or implement + * its own mutex. + **/ + boost::shared_ptr GetCachedValue(const std::string& id); + + /** + * As soon as the object is stored into the cache, the value could + * be read by other threads. + **/ + void Store(const std::string& id, + const boost::shared_ptr& value); + + void Invalidate(const std::string& id); + }; +} diff -r 271c0a3ae375 -r eabb2b485437 OrthancFramework/Sources/Toolbox.cpp --- a/OrthancFramework/Sources/Toolbox.cpp Mon May 11 19:00:38 2026 +0200 +++ b/OrthancFramework/Sources/Toolbox.cpp Mon May 11 19:41:52 2026 +0200 @@ -1814,7 +1814,7 @@ const std::string& key, unsigned int defaultValue) { - if (defaultValue > std::numeric_limits::max()) + if (defaultValue > static_cast(std::numeric_limits::max())) { throw OrthancException(ErrorCode_InternalError, "Default value too large to fit in a int"); } diff -r 271c0a3ae375 -r eabb2b485437 OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp --- a/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Mon May 11 19:00:38 2026 +0200 +++ b/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Mon May 11 19:41:52 2026 +0200 @@ -977,7 +977,7 @@ return true; case Json::uintValue: - if (configuration_[key].asUInt() > std::numeric_limits::max()) + if (configuration_[key].asUInt() > static_cast(std::numeric_limits::max())) { ORTHANC_PLUGINS_LOG_ERROR("The configuration option \"" + GetPath(key) + "\" is too large to fit in an integer");