changeset 115:c8ca47a67bf3

automatic clearing of the cache
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 07 Dec 2015 17:08:51 +0100
parents 628697fdfcbd
children 1eee3fa64670
files Plugin/Cache/CacheManager.cpp Plugin/Cache/CacheManager.h Plugin/Cache/CacheScheduler.cpp Plugin/Cache/CacheScheduler.h Plugin/Plugin.cpp
diffstat 5 files changed, 126 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/Plugin/Cache/CacheManager.cpp	Mon Dec 07 16:19:22 2015 +0100
+++ b/Plugin/Cache/CacheManager.cpp	Mon Dec 07 17:08:51 2015 +0100
@@ -302,6 +302,12 @@
       pimpl_->db_.Execute("CREATE INDEX CacheIndex ON Cache(bundle, item);");
     }
 
+    if (!pimpl_->db_.DoesTableExist("CacheProperties"))
+    {
+      printf("ICI\n");
+      pimpl_->db_.Execute("CREATE TABLE CacheProperties(property INTEGER PRIMARY KEY, value TEXT);");
+    }
+
     // Performance tuning of SQLite with PRAGMAs
     // http://www.sqlite.org/pragma.html
     pimpl_->db_.Execute("PRAGMA SYNCHRONOUS=OFF;");
@@ -589,4 +595,34 @@
     ReadBundleStatistics();
     SanityCheck();
   }
+
+
+  void CacheManager::SetProperty(CacheProperty property,
+                                 const std::string& value)
+  {
+    Orthanc::SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE,
+                                 "INSERT OR REPLACE INTO CacheProperties VALUES(?, ?)");
+    s.BindInt(0, property);
+    s.BindString(1, value);
+    s.Run();
+  }
+
+
+  bool CacheManager::LookupProperty(std::string& target,
+                                    CacheProperty property)
+  {
+    Orthanc::SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, 
+                                 "SELECT value FROM CacheProperties WHERE property=?");
+    s.BindInt(0, property);
+
+    if (!s.Step())
+    {
+      return false;
+    }
+    else
+    {
+      target = s.ColumnString(0);
+      return true;
+    }
+  }
 }
--- a/Plugin/Cache/CacheManager.h	Mon Dec 07 16:19:22 2015 +0100
+++ b/Plugin/Cache/CacheManager.h	Mon Dec 07 17:08:51 2015 +0100
@@ -25,6 +25,13 @@
 
 namespace OrthancPlugins
 {
+  enum CacheProperty
+  {
+    CacheProperty_OrthancVersion,
+    CacheProperty_WebViewerVersion
+  };
+
+
   class CacheManager : public boost::noncopyable
   {
   private:
@@ -92,5 +99,10 @@
                const std::string& item,
                const std::string& content);
 
+    void SetProperty(CacheProperty property,
+                     const std::string& value);
+
+    bool LookupProperty(std::string& target,
+                        CacheProperty property);
   };
 }
--- a/Plugin/Cache/CacheScheduler.cpp	Mon Dec 07 16:19:22 2015 +0100
+++ b/Plugin/Cache/CacheScheduler.cpp	Mon Dec 07 17:08:51 2015 +0100
@@ -411,4 +411,27 @@
   {
     return GetBundleScheduler(bundle).GetFactory();
   }
+
+
+  void CacheScheduler::SetProperty(CacheProperty property,
+                   const std::string& value)
+  {
+    boost::mutex::scoped_lock lock(cacheMutex_);
+    cache_.SetProperty(property, value);
+  }
+
+  
+  bool CacheScheduler::LookupProperty(std::string& target,
+                                      CacheProperty property)
+  {
+    boost::mutex::scoped_lock lock(cacheMutex_);
+    return cache_.LookupProperty(target, property);
+  }
+
+
+  void CacheScheduler::Clear()
+  {
+    boost::mutex::scoped_lock lock(cacheMutex_);
+    return cache_.Clear();
+  }
 }
--- a/Plugin/Cache/CacheScheduler.h	Mon Dec 07 16:19:22 2015 +0100
+++ b/Plugin/Cache/CacheScheduler.h	Mon Dec 07 17:08:51 2015 +0100
@@ -81,5 +81,13 @@
                   const std::string& item);
 
     ICacheFactory& GetFactory(int bundle);
+
+    void SetProperty(CacheProperty property,
+                     const std::string& value);
+
+    bool LookupProperty(std::string& target,
+                        CacheProperty property);
+
+    void Clear();
   };
 }
--- a/Plugin/Plugin.cpp	Mon Dec 07 16:19:22 2015 +0100
+++ b/Plugin/Plugin.cpp	Mon Dec 07 17:08:51 2015 +0100
@@ -448,20 +448,60 @@
    
       /* Create the cache */
       cache_ = new CacheContext(cachePath.string());
-      cache_->GetScheduler().RegisterPolicy(new ViewerPrefetchPolicy(context_));
-      cache_->GetScheduler().Register(CacheBundle_SeriesInformation, 
-                                      new SeriesInformationAdapter(context_, cache_->GetScheduler()), 1);
-      cache_->GetScheduler().Register(CacheBundle_DecodedImage, 
-                                      new DecodedImageAdapter(context_), decodingThreads);
+      CacheScheduler& scheduler = cache_->GetScheduler();
+
+
+      /* Look for a change in the versions */
+      std::string orthancVersion("unknown"), webViewerVersion("unknown");
+      bool clear = false;
+      if (!scheduler.LookupProperty(orthancVersion, CacheProperty_OrthancVersion) ||
+          orthancVersion != std::string(context_->orthancVersion))
+      {
+        std::string s = ("The version of Orthanc has changed from \"" + orthancVersion + "\" to \"" + 
+                         std::string(context_->orthancVersion) + "\": The cache of the Web viewer will be cleared");
+        OrthancPluginLogWarning(context_, s.c_str());
+        clear = true;
+      }
+
+      if (!scheduler.LookupProperty(webViewerVersion, CacheProperty_WebViewerVersion) ||
+          webViewerVersion != std::string(ORTHANC_WEBVIEWER_VERSION))
+      {
+        std::string s = ("The version of the Web viewer plugin has changed from \"" + webViewerVersion + "\" to \"" + 
+                         std::string(ORTHANC_WEBVIEWER_VERSION) + "\": The cache of the Web viewer will be cleared");
+        OrthancPluginLogWarning(context_, s.c_str());
+        clear = true;
+      }
+
+
+      /* Clear the cache if needed */
+      if (clear)
+      {
+        OrthancPluginLogWarning(context_, "Clearing the cache of the Web viewer");
+        scheduler.Clear();
+        scheduler.SetProperty(CacheProperty_OrthancVersion, context_->orthancVersion);
+        scheduler.SetProperty(CacheProperty_WebViewerVersion, ORTHANC_WEBVIEWER_VERSION);
+      }
+      else
+      {
+        OrthancPluginLogInfo(context_, "No change in the versions, no need to clear the cache of the Web viewer");
+      }
+
+
+      /* Configure the cache */
+      scheduler.RegisterPolicy(new ViewerPrefetchPolicy(context_));
+      scheduler.Register(CacheBundle_SeriesInformation, 
+                         new SeriesInformationAdapter(context_, scheduler), 1);
+      scheduler.Register(CacheBundle_DecodedImage, 
+                         new DecodedImageAdapter(context_), decodingThreads);
 
 
       /* Set the quotas */
-      cache_->GetScheduler().SetQuota(CacheBundle_SeriesInformation, 1000, 0);    // Keep info about 1000 series
+      scheduler.SetQuota(CacheBundle_SeriesInformation, 1000, 0);    // Keep info about 1000 series
       
       message = "Web viewer using a cache of " + boost::lexical_cast<std::string>(cacheSize) + " MB";
       OrthancPluginLogWarning(context_, message.c_str());
 
-      cache_->GetScheduler().SetQuota(CacheBundle_DecodedImage, 0, static_cast<uint64_t>(cacheSize) * 1024 * 1024);
+      scheduler.SetQuota(CacheBundle_DecodedImage, 0, static_cast<uint64_t>(cacheSize) * 1024 * 1024);
     }
     catch (std::runtime_error& e)
     {