changeset 875:200b4e0dddfc am-dev

BaseWebService: disable cache + correct handling of cache
author Alain Mazy <alain@mazy.be>
date Wed, 03 Jul 2019 10:12:09 +0200
parents debb70fa10b5
children 580dd82e13f5
files Framework/Deprecated/Toolbox/BaseWebService.cpp Framework/Deprecated/Toolbox/BaseWebService.h
diffstat 2 files changed, 20 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Deprecated/Toolbox/BaseWebService.cpp	Wed Jul 03 10:11:22 2019 +0200
+++ b/Framework/Deprecated/Toolbox/BaseWebService.cpp	Wed Jul 03 10:12:09 2019 +0200
@@ -28,6 +28,7 @@
 
 #include <boost/shared_ptr.hpp>
 #include <algorithm>
+#include <Core/Logging.h>
 
 namespace Deprecated
 {
@@ -90,7 +91,7 @@
                                 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback,
                                 unsigned int timeoutInSeconds)
   {
-    if (cache_.find(uri) == cache_.end())
+    if (!cacheEnabled_ || cache_.find(uri) == cache_.end())
     {
       GetAsyncInternal(uri, headers,
                        new BaseWebService::BaseWebServicePayload(successCallback, failureCallback, payload), // ownership is transfered
@@ -133,22 +134,27 @@
 
   void BaseWebService::CacheAndNotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message)
   {
-    while (cacheCurrentSize_ + message.GetAnswerSize() > cacheMaxSize_ && orderedCacheKeys_.size() > 0)
+    if (cacheEnabled_)
     {
-      const std::string& oldestUri = orderedCacheKeys_.back();
-      HttpCache::iterator it = cache_.find(oldestUri);
-      if (it != cache_.end())
+      while (cacheCurrentSize_ + message.GetAnswerSize() > cacheMaxSize_ && orderedCacheKeys_.size() > 0)
       {
-        cacheCurrentSize_ -= it->second->GetAnswerSize();
-        cache_.erase(it);
-      }
-      orderedCacheKeys_.pop_back();
+        VLOG(1) << "BaseWebService: clearing cache: " << cacheCurrentSize_ << "/" << cacheMaxSize_ << "(" << message.GetAnswerSize() << ")";
+        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();
+      boost::shared_ptr<CachedHttpRequestSuccessMessage> cachedMessage(new CachedHttpRequestSuccessMessage(message));
+      cache_[message.GetUri()] = cachedMessage;
+      orderedCacheKeys_.push_front(message.GetUri());
+      cacheCurrentSize_ += message.GetAnswerSize();
+    }
 
     NotifyHttpSuccess(message);
   }
--- a/Framework/Deprecated/Toolbox/BaseWebService.h	Wed Jul 03 10:11:22 2019 +0200
+++ b/Framework/Deprecated/Toolbox/BaseWebService.h	Wed Jul 03 10:12:09 2019 +0200
@@ -94,7 +94,7 @@
     BaseWebService(OrthancStone::MessageBroker& broker) :
       IWebService(broker),
       IObserver(broker),
-      cacheEnabled_(true),
+      cacheEnabled_(false),
       cacheCurrentSize_(0),
       cacheMaxSize_(100*1024*1024)
     {