-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
187 lines (139 loc) · 4.92 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <functional>
#include <string>
#include <memory>
#include <thread>
#include "lru.hpp"
/*
Basic {int,string} cache example
*/
void simpleTest()
{
std::cout << std::endl << "--- simpleTest() ---" << std::endl;
std::cout << "Initializing cache with max elems = 3" << std::endl;
SynchronizedLRUCacheMap<int, std::string> cache(3);
std::cout << "Inserting {1, \"foo\"} ..." << std::endl;
cache.insert(1, "foo");
std::cout << "Inserting {2, \"bar\"} ..." << std::endl;
cache.insert(2, "bar");
std::cout << std::endl << "Current cache state: ";
cache.printState();
std::cout << std::endl;
std::cout << "cache.access(1) = " << cache.access(1) << std::endl;
std::cout << std::endl << "Current cache state: ";
cache.printState();
std::cout << std::endl;
std::cout << "Inserting {3, \"asdf\"} ..." << std::endl;
cache.insert(3, "asdf");
std::cout << "Accessing Key=2 ..." << std::endl;
cache.access(2);
std::cout << "Inserting {4, \"qwer\"} ..." << std::endl;
cache.insert(4, "qwer");
std::cout << std::endl << "Current cache state: ";
cache.printState();
std::cout << std::endl;
std::cout << "cache.access(3) = " << cache.access(3) << std::endl;
std::cout << "Resizing cache to max elems = 1 ..." << std::endl;
cache.resize(1);
std::cout << "Updating value keyed by 3 ..." << std::endl;
cache.update(3, "zxcv");
std::cout << std::endl << "Current cache state: ";
cache.printState();
std::cout << std::endl;
}
/*
Example for caching custom data structs
*/
class SomeCachedObject
{
const int id;
std::vector<double> data;
friend std::ostream& operator<<(std::ostream& os, const SomeCachedObject& rhs);
public:
SomeCachedObject(int id) : id(id) {};
int getId() const {return id;};
void someMethod() {};
};
std::ostream& operator<<(std::ostream& os, const SomeCachedObject& rhs)
{
os << "SomeCachedObject<id=" << rhs.id << ">";
return os;
}
void ptrTest()
{
std::cout << std::endl << "--- ptrTest() ---" << std::endl;
std::cout << "Initializing cache with max elems = 3" << std::endl;
SynchronizedLRUCacheMap<std::string, std::shared_ptr<SomeCachedObject>> cache(3);
std::function<std::shared_ptr<SomeCachedObject>(int)> make_obj = [](int id){
return std::make_shared<SomeCachedObject>(id);
};
std::cout << "Inserting {\"one\", *SomeCachedObject(1)} ..." << std::endl;
cache.insert("one", make_obj(1));
std::cout << "Inserting {\"two\", *SomeCachedObject(10)} ..." << std::endl;
cache.insert("two", make_obj(10));
std::cout << "*cache.access(\"one\") = " << *cache.access("one") << std::endl;
std::cout << std::endl << "Current cache state: ";
cache.printState();
std::cout << std::endl;
std::cout << "Inserting {\"three\", *SomeCachedObject(11)} ..." << std::endl;
cache.insert("three", make_obj(11));
std::cout << "Inserting {\"four\", *SomeCachedObject(100)} ..." << std::endl;
cache.insert("four", make_obj(11));
std::cout << "*cache.access(\"one\") = " << *cache.access("one") << std::endl;
if (cache.has("two"))
std::cout << "*cache.access(\"two\") = " << *cache.access("two") << std::endl;
else
std::cout << "Key \"two\" no longer in cache!" << std::endl;
std::cout << std::endl << "Current cache state: ";
cache.printState();
std::cout << std::endl;
}
/*
Example cache insert/access over multiple threads
*/
void threadedTest()
{
std::cout << std::endl << "--- threadedTest() ---" << std::endl;
std::cout << "Initializing cache with max elems = 2" << std::endl;
SynchronizedLRUCacheMap<std::string, std::thread::id> cache(2);
std::thread t1([&cache](){
std::cout << "Inserting \"t1\" ..." << std::endl;
cache.insert("t1", std::this_thread::get_id());
std::this_thread::sleep_for (std::chrono::milliseconds(15));
});
std::thread t2([&cache](){
std::cout << "Inserting \"t2\" ..." << std::endl;
std::this_thread::sleep_for (std::chrono::milliseconds(10));
cache.insert("t2", std::this_thread::get_id());
});
std::thread t3([&cache](){
std::cout << "Inserting \"t3\" ..." << std::endl;
cache.insert("t3", std::this_thread::get_id());
std::this_thread::sleep_for (std::chrono::milliseconds(10));
});
t1.join();
t2.join();
t3.join();
std::cout << std::endl << "Current cache state: ";
cache.printState();
std::cout << std::endl;
std::cout << "Accessing keys \"t2\", \"t3\" in separate threads ..." << std::endl;
std::thread t4([&cache](){
cache.access("t3");
});
std::thread t5([&cache](){
cache.access("t2");
});
t4.join();
t5.join();
std::cout << std::endl << "Current cache state: ";
cache.printState();
std::cout << std::endl;
}
int main()
{
simpleTest();
ptrTest();
threadedTest();
return 0;
}