comparison 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
comparison
equal deleted inserted replaced
5419:ac4e9fb87615 5420:d37dff2c0028
35 /** 35 /**
36 * Note: this class is thread safe 36 * Note: this class is thread safe
37 **/ 37 **/
38 class ORTHANC_PUBLIC StorageCache : public boost::noncopyable 38 class ORTHANC_PUBLIC StorageCache : public boost::noncopyable
39 { 39 {
40 public:
41
42 // The StorageCache is only accessible through this accessor.
43 // It will make sure that only one user will fill load data and fill
44 // the cache if multiple users try to access the same item at the same time.
45 // This scenario happens a lot when multiple workers from a viewer access
46 // the same file.
47 class Accessor : public MemoryStringCache::Accessor
48 {
49 public:
50 Accessor(StorageCache& cache);
51
52 void Add(const std::string& uuid,
53 FileContentType contentType,
54 const std::string& value);
55
56 void AddStartRange(const std::string& uuid,
57 FileContentType contentType,
58 const std::string& value);
59
60 void Add(const std::string& uuid,
61 FileContentType contentType,
62 const void* buffer,
63 size_t size);
64
65 bool Fetch(std::string& value,
66 const std::string& uuid,
67 FileContentType contentType);
68
69 bool FetchStartRange(std::string& value,
70 const std::string& uuid,
71 FileContentType contentType,
72 uint64_t end /* exclusive */);
73 };
74
40 private: 75 private:
41 MemoryStringCache cache_; 76 MemoryStringCache cache_;
42 77
43 public: 78 public:
44 void SetMaximumSize(size_t size); 79 void SetMaximumSize(size_t size);
45 80
81 void Invalidate(const std::string& uuid,
82 FileContentType contentType);
83
84 private:
46 void Add(const std::string& uuid, 85 void Add(const std::string& uuid,
47 FileContentType contentType, 86 FileContentType contentType,
48 const std::string& value); 87 const std::string& value);
49 88
50 void AddStartRange(const std::string& uuid, 89 void AddStartRange(const std::string& uuid,
54 void Add(const std::string& uuid, 93 void Add(const std::string& uuid,
55 FileContentType contentType, 94 FileContentType contentType,
56 const void* buffer, 95 const void* buffer,
57 size_t size); 96 size_t size);
58 97
59 void Invalidate(const std::string& uuid,
60 FileContentType contentType);
61
62 bool Fetch(std::string& value, 98 bool Fetch(std::string& value,
63 const std::string& uuid, 99 const std::string& uuid,
64 FileContentType contentType); 100 FileContentType contentType);
65 101
66 bool FetchStartRange(std::string& value, 102 bool FetchStartRange(std::string& value,