# HG changeset patch # User Alain Mazy # Date 1561981011 -7200 # Node ID 733c6db3e5a37996bccd07254b8493716a172cc9 # Parent 23701fbf228ec71aa2fe1ea7a3bc113c76aba38c limiting the size of the cache diff -r 23701fbf228e -r 733c6db3e5a3 Framework/Deprecated/Toolbox/BaseWebService.cpp --- a/Framework/Deprecated/Toolbox/BaseWebService.cpp Mon Jun 24 16:29:49 2019 +0200 +++ b/Framework/Deprecated/Toolbox/BaseWebService.cpp Mon Jul 01 13:36:51 2019 +0200 @@ -27,6 +27,7 @@ #include #include +#include namespace Deprecated { @@ -101,6 +102,15 @@ } else { + // put the uri on top of the most recently accessed list + std::deque::iterator it = std::find(orderedCacheKeys_.begin(), orderedCacheKeys_.end(), uri); + if (it != orderedCacheKeys_.end()) + { + std::string uri = *it; + orderedCacheKeys_.erase(it); + orderedCacheKeys_.push_front(uri); + } + // create a command and "post" it to the Oracle so it is executed and commited "later" NotifyHttpSuccessLater(cache_[uri], payload, successCallback); } @@ -123,7 +133,23 @@ void BaseWebService::CacheAndNotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message) { + while (cacheCurrentSize_ + message.GetAnswerSize() > cacheMaxSize_ && orderedCacheKeys_.size() > 0) + { + const std::string& oldestUri = orderedCacheKeys_.back(); + HttpCache::iterator it = cache_.find(oldestUri); + if (it != cache_.end()) + { + cacheCurrentSize_ -= it->second->GetAnswerSize(); + cache_.erase(it); + } + orderedCacheKeys_.pop_back(); + + } + cache_[message.GetUri()] = boost::shared_ptr(new CachedHttpRequestSuccessMessage(message)); + orderedCacheKeys_.push_front(message.GetUri()); + cacheCurrentSize_ += message.GetAnswerSize(); + NotifyHttpSuccess(message); } diff -r 23701fbf228e -r 733c6db3e5a3 Framework/Deprecated/Toolbox/BaseWebService.h --- a/Framework/Deprecated/Toolbox/BaseWebService.h Mon Jun 24 16:29:49 2019 +0200 +++ b/Framework/Deprecated/Toolbox/BaseWebService.h Mon Jul 01 13:36:51 2019 +0200 @@ -25,6 +25,7 @@ #include #include +#include namespace Deprecated { @@ -81,14 +82,21 @@ class BaseWebServicePayload; bool cacheEnabled_; - std::map > cache_; // TODO: this is currently an infinite cache ! + size_t cacheCurrentSize_; + size_t cacheMaxSize_; + + typedef std::map > HttpCache; + HttpCache cache_; + std::deque orderedCacheKeys_; public: BaseWebService(OrthancStone::MessageBroker& broker) : IWebService(broker), IObserver(broker), - cacheEnabled_(true) + cacheEnabled_(true), + cacheCurrentSize_(0), + cacheMaxSize_(100*1024*1024) { }