comparison UnitTestsSources/MemoryCacheTests.cpp @ 3557:4d809b2e1141

better cache toolbox: MemoryObjectCache
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 04 Nov 2019 15:18:38 +0100
parents db71bd11affc
children 94f4a18a79cc
comparison
equal deleted inserted replaced
3556:87940f7156e0 3557:4d809b2e1141
38 #include <algorithm> 38 #include <algorithm>
39 #include <boost/thread.hpp> 39 #include <boost/thread.hpp>
40 #include <boost/lexical_cast.hpp> 40 #include <boost/lexical_cast.hpp>
41 41
42 #include "../Core/Cache/MemoryCache.h" 42 #include "../Core/Cache/MemoryCache.h"
43 #include "../Core/Cache/MemoryStringCache.h"
43 #include "../Core/Cache/SharedArchive.h" 44 #include "../Core/Cache/SharedArchive.h"
44 #include "../Core/IDynamicObject.h" 45 #include "../Core/IDynamicObject.h"
45 #include "../Core/Logging.h" 46 #include "../Core/Logging.h"
46 47
47 48
210 LOG(INFO) << "Removing cache entry for " << value_; 211 LOG(INFO) << "Removing cache entry for " << value_;
211 log_ += boost::lexical_cast<std::string>(value_) + " "; 212 log_ += boost::lexical_cast<std::string>(value_) + " ";
212 } 213 }
213 }; 214 };
214 215
215 class IntegerProvider : public Orthanc::ICachePageProvider 216 class IntegerProvider : public Orthanc::Deprecated::ICachePageProvider
216 { 217 {
217 public: 218 public:
218 std::string log_; 219 std::string log_;
219 220
220 virtual Orthanc::IDynamicObject* Provide(const std::string& s) ORTHANC_OVERRIDE 221 virtual Orthanc::IDynamicObject* Provide(const std::string& s) ORTHANC_OVERRIDE
229 TEST(MemoryCache, Basic) 230 TEST(MemoryCache, Basic)
230 { 231 {
231 IntegerProvider provider; 232 IntegerProvider provider;
232 233
233 { 234 {
234 Orthanc::MemoryCache cache(provider, 3); 235 Orthanc::Deprecated::MemoryCache cache(provider, 3);
235 cache.Access("42"); // 42 -> exit 236 cache.Access("42"); // 42 -> exit
236 cache.Access("43"); // 43, 42 -> exit 237 cache.Access("43"); // 43, 42 -> exit
237 cache.Access("45"); // 45, 43, 42 -> exit 238 cache.Access("45"); // 45, 43, 42 -> exit
238 cache.Access("42"); // 42, 45, 43 -> exit 239 cache.Access("42"); // 42, 45, 43 -> exit
239 cache.Access("43"); // 43, 42, 45 -> exit 240 cache.Access("43"); // 43, 42, 45 -> exit
315 } 316 }
316 } 317 }
317 318
318 ASSERT_EQ(2u, count); 319 ASSERT_EQ(2u, count);
319 } 320 }
321
322
323 TEST(MemoryStringCache, Basic)
324 {
325 Orthanc::MemoryStringCache c;
326 ASSERT_THROW(c.SetMaximumSize(0), Orthanc::OrthancException);
327
328 c.SetMaximumSize(2);
329
330 std::string v;
331 ASSERT_FALSE(c.Fetch(v, "hello"));
332
333 c.Add("hello", "a");
334 ASSERT_TRUE(c.Fetch(v, "hello")); ASSERT_EQ("a", v);
335 ASSERT_FALSE(c.Fetch(v, "hello2"));
336 ASSERT_FALSE(c.Fetch(v, "hello3"));
337
338 c.Add("hello2", "b");
339 ASSERT_TRUE(c.Fetch(v, "hello")); ASSERT_EQ("a", v);
340 ASSERT_TRUE(c.Fetch(v, "hello2")); ASSERT_EQ("b", v);
341 ASSERT_FALSE(c.Fetch(v, "hello3"));
342
343 c.Add("hello3", "too large value");
344 ASSERT_TRUE(c.Fetch(v, "hello")); ASSERT_EQ("a", v);
345 ASSERT_TRUE(c.Fetch(v, "hello2")); ASSERT_EQ("b", v);
346 ASSERT_FALSE(c.Fetch(v, "hello3"));
347
348 c.Add("hello3", "c");
349 ASSERT_FALSE(c.Fetch(v, "hello")); // Recycled
350 ASSERT_TRUE(c.Fetch(v, "hello2")); ASSERT_EQ("b", v);
351 ASSERT_TRUE(c.Fetch(v, "hello3")); ASSERT_EQ("c", v);
352 }
353
354
355 TEST(MemoryStringCache, Invalidate)
356 {
357 Orthanc::MemoryStringCache c;
358 c.Add("hello", "a");
359 c.Add("hello2", "b");
360
361 std::string v;
362 ASSERT_TRUE(c.Fetch(v, "hello")); ASSERT_EQ("a", v);
363 ASSERT_TRUE(c.Fetch(v, "hello2")); ASSERT_EQ("b", v);
364
365 c.Invalidate("hello");
366 ASSERT_FALSE(c.Fetch(v, "hello"));
367 ASSERT_TRUE(c.Fetch(v, "hello2")); ASSERT_EQ("b", v);
368 }