annotate UnitTestsSources/MemoryCache.cpp @ 689:2d0a347e8cfc

switch to 2014
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 03 Feb 2014 16:06:58 +0100
parents 17815b9d4280
children 3d6f9b7d0add
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #include "gtest/gtest.h"
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
3 #include <glog/logging.h>
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 #include <memory>
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
5 #include <boost/thread.hpp>
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
6 #include <boost/lexical_cast.hpp>
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 #include "../Core/IDynamicObject.h"
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 283
diff changeset
8 #include "../Core/Cache/MemoryCache.h"
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10
505
f59e4518fd57 rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 285
diff changeset
11 TEST(LRU, Basic)
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 {
505
f59e4518fd57 rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 285
diff changeset
13 Orthanc::LeastRecentlyUsedIndex<std::string> r;
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 r.Add("d");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 r.Add("a");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 r.Add("c");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 r.Add("b");
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19
509
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
20 r.MakeMostRecent("a");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
21 r.MakeMostRecent("d");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
22 r.MakeMostRecent("b");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
23 r.MakeMostRecent("c");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
24 r.MakeMostRecent("d");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
25 r.MakeMostRecent("c");
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
27 ASSERT_EQ("a", r.GetOldest());
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 ASSERT_EQ("a", r.RemoveOldest());
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
29 ASSERT_EQ("b", r.GetOldest());
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 ASSERT_EQ("b", r.RemoveOldest());
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
31 ASSERT_EQ("d", r.GetOldest());
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 ASSERT_EQ("d", r.RemoveOldest());
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
33 ASSERT_EQ("c", r.GetOldest());
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 ASSERT_EQ("c", r.RemoveOldest());
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 ASSERT_TRUE(r.IsEmpty());
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
37
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
38 ASSERT_THROW(r.GetOldest(), Orthanc::OrthancException);
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
39 ASSERT_THROW(r.RemoveOldest(), Orthanc::OrthancException);
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 }
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
505
f59e4518fd57 rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 285
diff changeset
43 TEST(LRU, Payload)
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
505
f59e4518fd57 rename CacheIndex as LeastRecentlyUsedIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 285
diff changeset
45 Orthanc::LeastRecentlyUsedIndex<std::string, int> r;
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 r.Add("a", 420);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 r.Add("b", 421);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 r.Add("c", 422);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 r.Add("d", 423);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
509
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
52 r.MakeMostRecent("a");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
53 r.MakeMostRecent("d");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
54 r.MakeMostRecent("b");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
55 r.MakeMostRecent("c");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
56 r.MakeMostRecent("d");
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
57 r.MakeMostRecent("c");
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 ASSERT_TRUE(r.Contains("b"));
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 ASSERT_EQ(421, r.Invalidate("b"));
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 ASSERT_FALSE(r.Contains("b"));
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 int p;
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
64 ASSERT_TRUE(r.Contains("a", p)); ASSERT_EQ(420, p);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
65 ASSERT_TRUE(r.Contains("c", p)); ASSERT_EQ(422, p);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
66 ASSERT_TRUE(r.Contains("d", p)); ASSERT_EQ(423, p);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
67
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
68 ASSERT_EQ("a", r.GetOldest());
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
69 ASSERT_EQ(420, r.GetOldestPayload());
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 ASSERT_EQ("a", r.RemoveOldest(p)); ASSERT_EQ(420, p);
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
71
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
72 ASSERT_EQ("d", r.GetOldest());
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
73 ASSERT_EQ(423, r.GetOldestPayload());
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 ASSERT_EQ("d", r.RemoveOldest(p)); ASSERT_EQ(423, p);
507
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
75
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
76 ASSERT_EQ("c", r.GetOldest());
c4122c3a47c1 access to oldest item in LRUCache
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 505
diff changeset
77 ASSERT_EQ(422, r.GetOldestPayload());
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 ASSERT_EQ("c", r.RemoveOldest(p)); ASSERT_EQ(422, p);
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 ASSERT_TRUE(r.IsEmpty());
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 }
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
509
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
84 TEST(LRU, PayloadUpdate)
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
85 {
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
86 Orthanc::LeastRecentlyUsedIndex<std::string, int> r;
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
87
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
88 r.Add("a", 420);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
89 r.Add("b", 421);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
90 r.Add("d", 423);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
91
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
92 r.MakeMostRecent("a", 424);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
93 r.MakeMostRecent("d", 421);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
94
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
95 ASSERT_EQ("b", r.GetOldest());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
96 ASSERT_EQ(421, r.GetOldestPayload());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
97 r.RemoveOldest();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
98
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
99 ASSERT_EQ("a", r.GetOldest());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
100 ASSERT_EQ(424, r.GetOldestPayload());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
101 r.RemoveOldest();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
102
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
103 ASSERT_EQ("d", r.GetOldest());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
104 ASSERT_EQ(421, r.GetOldestPayload());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
105 r.RemoveOldest();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
106
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
107 ASSERT_TRUE(r.IsEmpty());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
108 }
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
109
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
110
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
111
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
112 TEST(LRU, PayloadUpdateBis)
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
113 {
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
114 Orthanc::LeastRecentlyUsedIndex<std::string, int> r;
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
115
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
116 r.AddOrMakeMostRecent("a", 420);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
117 r.AddOrMakeMostRecent("b", 421);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
118 r.AddOrMakeMostRecent("d", 423);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
119 r.AddOrMakeMostRecent("a", 424);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
120 r.AddOrMakeMostRecent("d", 421);
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
121
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
122 ASSERT_EQ("b", r.GetOldest());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
123 ASSERT_EQ(421, r.GetOldestPayload());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
124 r.RemoveOldest();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
125
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
126 ASSERT_EQ("a", r.GetOldest());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
127 ASSERT_EQ(424, r.GetOldestPayload());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
128 r.RemoveOldest();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
129
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
130 ASSERT_EQ("d", r.GetOldest());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
131 ASSERT_EQ(421, r.GetOldestPayload());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
132 r.RemoveOldest();
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
133
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
134 ASSERT_TRUE(r.IsEmpty());
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
135 }
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
136
e7841864c97c StableResourcesMonitor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 507
diff changeset
137
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
138
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
139
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
140 namespace
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
141 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
142 class Integer : public Orthanc::IDynamicObject
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
143 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
144 private:
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
145 std::string& log_;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
146 int value_;
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 public:
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
149 Integer(std::string& log, int v) : log_(log), value_(v)
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
150 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
151 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
152
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
153 virtual ~Integer()
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
154 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
155 LOG(INFO) << "Removing cache entry for " << value_;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
156 log_ += boost::lexical_cast<std::string>(value_) + " ";
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
157 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
158
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
159 int GetValue() const
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
160 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
161 return value_;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
162 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
163 };
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
164
284
06aa7b7b6723 implementation of a single-threaded cache mechanism
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 283
diff changeset
165 class IntegerProvider : public Orthanc::ICachePageProvider
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
166 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
167 public:
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
168 std::string log_;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
169
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
170 Orthanc::IDynamicObject* Provide(const std::string& s)
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
171 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
172 LOG(INFO) << "Providing " << s;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
173 return new Integer(log_, boost::lexical_cast<int>(s));
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
174 }
282
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 };
915ed24547ea cache lru policy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 }
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
177
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
178
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
179 TEST(MemoryCache, Basic)
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
180 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
181 IntegerProvider provider;
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
182
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
183 {
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
184 Orthanc::MemoryCache cache(provider, 3);
285
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
185 cache.Access("42"); // 42 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
186 cache.Access("43"); // 43, 42 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
187 cache.Access("45"); // 45, 43, 42 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
188 cache.Access("42"); // 42, 45, 43 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
189 cache.Access("43"); // 43, 42, 45 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
190 cache.Access("47"); // 45 is removed; 47, 43, 42 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
191 cache.Access("44"); // 42 is removed; 44, 47, 43 -> exit
4031f73fe0e4 access to the raw dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 284
diff changeset
192 cache.Access("42"); // 43 is removed; 42, 44, 47 -> exit
283
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
193 // Closing the cache: 47, 44, 42 are successively removed
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
194 }
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
195
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
196 ASSERT_EQ("45 42 43 47 44 42 ", provider.log_);
c9977db00e1d caching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 282
diff changeset
197 }