Mercurial > hg > orthanc
changeset 6769:76bf4629d797
created class SharedObjectCache
| author | Sebastien Jodogne <s.jodogne@gmail.com> |
|---|---|
| date | Mon, 11 May 2026 18:48:47 +0200 |
| parents | ca9a86f8755b |
| children | eabb2b485437 |
| files | OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake OrthancFramework/Sources/Cache/SharedObjectCache.cpp OrthancFramework/Sources/Cache/SharedObjectCache.h |
| diffstat | 3 files changed, 286 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake Mon May 11 18:48:32 2026 +0200 +++ b/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake Mon May 11 18:48:47 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/Cache/SharedObjectCache.cpp Mon May 11 18:48:47 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 + * <http://www.gnu.org/licenses/>. + **/ + + +#include "../PrecompiledHeaders.h" +#include "SharedObjectCache.h" + + +namespace Orthanc +{ + class SharedObjectCache::Item : public boost::noncopyable + { + private: + boost::shared_ptr<IDynamicObject> value_; + size_t size_; + + public: + Item(boost::shared_ptr<IDynamicObject> value, + size_t size) : + value_(value), + size_(size) + { + } + + const boost::shared_ptr<IDynamicObject>& 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<IDynamicObject> 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<IDynamicObject>& 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)); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/Cache/SharedObjectCache.h Mon May 11 18:48:47 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 + * <http://www.gnu.org/licenses/>. + **/ + + +#pragma once + +#include "../Compatibility.h" +#include "../IDynamicObject.h" +#include "../MultiThreading/Mutex.h" +#include "LeastRecentlyUsedIndex.h" + +#include <boost/shared_ptr.hpp> + + +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<const SingleValueObject<std::string>&>(object).GetValue().size(); + } + }; + + private: + class Item; + + typedef std::map<std::string, Item*> Content; + + Mutex mutex_; + std::unique_ptr<ISizeProvider> provider_; + LeastRecentlyUsedIndex<std::string> 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<IDynamicObject> 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<IDynamicObject>& value); + + void Invalidate(const std::string& id); + }; +}
