diff OrthancFramework/Sources/FileStorage/StorageCache.h @ 5420:d37dff2c0028 am-new-cache

Optimized the MemoryStringCache to prevent loading the same file multiple times if multiple users request the same file at the same time
author Alain Mazy <am@osimis.io>
date Mon, 13 Nov 2023 17:01:59 +0100
parents 0ea402b4d901
children c65e036d649b
line wrap: on
line diff
--- a/OrthancFramework/Sources/FileStorage/StorageCache.h	Thu Nov 09 08:51:01 2023 +0100
+++ b/OrthancFramework/Sources/FileStorage/StorageCache.h	Mon Nov 13 17:01:59 2023 +0100
@@ -37,12 +37,51 @@
    **/
    class ORTHANC_PUBLIC StorageCache : public boost::noncopyable
     {
+    public:
+
+      // The StorageCache is only accessible through this accessor.
+      // It will make sure that only one user will fill load data and fill
+      // the cache if multiple users try to access the same item at the same time.
+      // This scenario happens a lot when multiple workers from a viewer access 
+      // the same file.
+      class Accessor : public MemoryStringCache::Accessor
+      {
+      public:
+        Accessor(StorageCache& cache);
+
+        void Add(const std::string& uuid, 
+                FileContentType contentType,
+                const std::string& value);
+
+        void AddStartRange(const std::string& uuid, 
+                          FileContentType contentType,
+                          const std::string& value);
+
+        void Add(const std::string& uuid, 
+                FileContentType contentType,
+                const void* buffer,
+                size_t size);
+
+        bool Fetch(std::string& value, 
+                  const std::string& uuid,
+                  FileContentType contentType);
+
+        bool FetchStartRange(std::string& value, 
+                            const std::string& uuid,
+                            FileContentType contentType,
+                            uint64_t end /* exclusive */);
+      };
+
     private:
       MemoryStringCache   cache_;
       
     public:
       void SetMaximumSize(size_t size);
 
+      void Invalidate(const std::string& uuid,
+                      FileContentType contentType);
+
+    private:
       void Add(const std::string& uuid, 
                FileContentType contentType,
                const std::string& value);
@@ -56,9 +95,6 @@
                const void* buffer,
                size_t size);
 
-      void Invalidate(const std::string& uuid,
-                      FileContentType contentType);
-
       bool Fetch(std::string& value, 
                  const std::string& uuid,
                  FileContentType contentType);