changeset 872:733c6db3e5a3 am-dev

limiting the size of the cache
author Alain Mazy <alain@mazy.be>
date Mon, 01 Jul 2019 13:36:51 +0200
parents 23701fbf228e
children 78f4317eb94b
files Framework/Deprecated/Toolbox/BaseWebService.cpp Framework/Deprecated/Toolbox/BaseWebService.h
diffstat 2 files changed, 36 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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 <Core/OrthancException.h>
 
 #include <boost/shared_ptr.hpp>
+#include <algorithm>
 
 namespace Deprecated
 {
@@ -101,6 +102,15 @@
     }
     else
     {
+      // put the uri on top of the most recently accessed list
+      std::deque<std::string>::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<CachedHttpRequestSuccessMessage>(new CachedHttpRequestSuccessMessage(message));
+    orderedCacheKeys_.push_front(message.GetUri());
+    cacheCurrentSize_ += message.GetAnswerSize();
+
     NotifyHttpSuccess(message);
   }
 
--- 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 <string>
 #include <map>
+#include <deque>
 
 namespace Deprecated
 {
@@ -81,14 +82,21 @@
     class BaseWebServicePayload;
 
     bool          cacheEnabled_;
-    std::map<std::string, boost::shared_ptr<CachedHttpRequestSuccessMessage> > cache_;  // TODO: this is currently an infinite cache !
+    size_t        cacheCurrentSize_;
+    size_t        cacheMaxSize_;
+
+    typedef std::map<std::string, boost::shared_ptr<CachedHttpRequestSuccessMessage> > HttpCache;
+    HttpCache cache_;
+    std::deque<std::string> orderedCacheKeys_;
 
   public:
 
     BaseWebService(OrthancStone::MessageBroker& broker) :
       IWebService(broker),
       IObserver(broker),
-      cacheEnabled_(true)
+      cacheEnabled_(true),
+      cacheCurrentSize_(0),
+      cacheMaxSize_(100*1024*1024)
     {
     }