comparison Core/Cache/MemoryCache.cpp @ 3557:4d809b2e1141

better cache toolbox: MemoryObjectCache
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 04 Nov 2019 15:18:38 +0100
parents 4e43e67f8ecf
children 94f4a18a79cc
comparison
equal deleted inserted replaced
3556:87940f7156e0 3557:4d809b2e1141
36 36
37 #include "../Logging.h" 37 #include "../Logging.h"
38 38
39 namespace Orthanc 39 namespace Orthanc
40 { 40 {
41 MemoryCache::Page& MemoryCache::Load(const std::string& id) 41 namespace Deprecated
42 { 42 {
43 // Reuse the cache entry if it already exists 43 MemoryCache::Page& MemoryCache::Load(const std::string& id)
44 Page* p = NULL;
45 if (index_.Contains(id, p))
46 { 44 {
47 VLOG(1) << "Reusing a cache page"; 45 // Reuse the cache entry if it already exists
48 assert(p != NULL); 46 Page* p = NULL;
49 index_.MakeMostRecent(id); 47 if (index_.Contains(id, p))
48 {
49 VLOG(1) << "Reusing a cache page";
50 assert(p != NULL);
51 index_.MakeMostRecent(id);
52 return *p;
53 }
54
55 // The id is not in the cache yet. Make some room if the cache
56 // is full.
57 if (index_.GetSize() == cacheSize_)
58 {
59 VLOG(1) << "Dropping the oldest cache page";
60 index_.RemoveOldest(p);
61 delete p;
62 }
63
64 // Create a new cache page
65 std::auto_ptr<Page> result(new Page);
66 result->id_ = id;
67 result->content_.reset(provider_.Provide(id));
68
69 // Add the newly create page to the cache
70 VLOG(1) << "Registering new data in a cache page";
71 p = result.release();
72 index_.Add(id, p);
50 return *p; 73 return *p;
51 } 74 }
52 75
53 // The id is not in the cache yet. Make some room if the cache 76 MemoryCache::MemoryCache(ICachePageProvider& provider,
54 // is full. 77 size_t cacheSize) :
55 if (index_.GetSize() == cacheSize_) 78 provider_(provider),
79 cacheSize_(cacheSize)
56 { 80 {
57 VLOG(1) << "Dropping the oldest cache page";
58 index_.RemoveOldest(p);
59 delete p;
60 } 81 }
61 82
62 // Create a new cache page 83 void MemoryCache::Invalidate(const std::string& id)
63 std::auto_ptr<Page> result(new Page); 84 {
64 result->id_ = id; 85 Page* p = NULL;
65 result->content_.reset(provider_.Provide(id)); 86 if (index_.Contains(id, p))
87 {
88 VLOG(1) << "Invalidating a cache page";
89 assert(p != NULL);
90 delete p;
91 index_.Invalidate(id);
92 }
93 }
66 94
67 // Add the newly create page to the cache 95 MemoryCache::~MemoryCache()
68 VLOG(1) << "Registering new data in a cache page"; 96 {
69 p = result.release(); 97 while (!index_.IsEmpty())
70 index_.Add(id, p); 98 {
71 return *p; 99 Page* element = NULL;
72 } 100 index_.RemoveOldest(element);
101 assert(element != NULL);
102 delete element;
103 }
104 }
73 105
74 MemoryCache::MemoryCache(ICachePageProvider& provider, 106 IDynamicObject& MemoryCache::Access(const std::string& id)
75 size_t cacheSize) :
76 provider_(provider),
77 cacheSize_(cacheSize)
78 {
79 }
80
81 void MemoryCache::Invalidate(const std::string& id)
82 {
83 Page* p = NULL;
84 if (index_.Contains(id, p))
85 { 107 {
86 VLOG(1) << "Invalidating a cache page"; 108 return *Load(id).content_;
87 assert(p != NULL);
88 delete p;
89 index_.Invalidate(id);
90 } 109 }
91 } 110 }
92
93 MemoryCache::~MemoryCache()
94 {
95 while (!index_.IsEmpty())
96 {
97 Page* element = NULL;
98 index_.RemoveOldest(element);
99 assert(element != NULL);
100 delete element;
101 }
102 }
103
104 IDynamicObject& MemoryCache::Access(const std::string& id)
105 {
106 return *Load(id).content_;
107 }
108 } 111 }