This repository was archived by the owner on Jun 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFastcache.h
More file actions
388 lines (272 loc) · 7.7 KB
/
Fastcache.h
File metadata and controls
388 lines (272 loc) · 7.7 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/functional/hash.hpp>
#include <boost/detail/atomic_count.hpp>
#include <vector>
#include <exception>
//#include <iterator>
#include <map>
#include <iostream>
//#include <utility>
#include <time.h>
// Shard size. This should be much larger than the number of threads likely to access the cache at any one time.
#ifndef FASTCACHE_SHARDSIZE
#define FASTCACHE_SHARDSIZE 256u
#endif
#ifndef FASTCACHE_CURATOR_SLEEP_MS
#define FASTCACHE_CURATOR_SLEEP_MS 30000u
#endif
using boost::shared_ptr;
using boost::mutex;
namespace active911 {
// Write modes
enum fastcache_writemode {
FASTCACHE_WRITEMODE_WRITE_ALWAYS,
FASTCACHE_WRITEMODE_ONLY_WRITE_IF_SET,
FASTCACHE_WRITEMODE_ONLY_WRITE_IF_NOT_SET
};
struct FastcacheObjectLocked : std::exception {
char const* what() const throw() {
return "Object is currently locked";
};
};
template <class Key, class T>
class Fastcache {
/**
* CacheItem
* A wrapper class for cache values
*/
template <class W>
class CacheItem {
public:
CacheItem(shared_ptr<T> data, time_t expiration){
this->data=data;
this->expiration=expiration;
};
/**
* Have we expired?
*
* @retval bool
*/
bool expired(){
// If we have no expiration, the answer is easy
if(this->expiration==0) {
return false;
}
// Get the time and compare
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
return (time.tv_sec > this->expiration);
};
shared_ptr<T> data;
time_t expiration;
};
template <class S> // Keep compiler happy... really will be T
class Shard {
public:
Shard(){
this->guard=shared_ptr<mutex>(new mutex());
};
void cull_expired_keys() {
// Iterate map. Somehow this is ok to do even though we are deleting stuff?
for(typename std::map<Key,shared_ptr<CacheItem<T> > >::iterator it=this->map.begin();it != this->map.end();/* no increment */){
shared_ptr<CacheItem<T> >item=it->second;
if(item->expired()){
this->map.erase(it++);
} else {
++it;
}
}
}
shared_ptr<mutex> guard;
std::map<Key,shared_ptr<CacheItem<T> > > map;
};
public:
Fastcache(){
// We are making a new cache. Init our shards.
this->shards.reserve(FASTCACHE_SHARDSIZE);
for(unsigned int n=0; n<FASTCACHE_SHARDSIZE; n++) {
shared_ptr<Shard<T> >p (new Shard<T>());
this->shards.push_back(p);
}
// Start up the curator thread
this->curator_run=shared_ptr<boost::detail::atomic_count>(new boost::detail::atomic_count(1));
this->curator=shared_ptr<boost::thread>(new boost::thread(&Fastcache::curate, this));
};
~Fastcache(){
// Retire the curator
--(*this->curator_run);
this->curator->interrupt();
this->curator->join();
};
/**
* Get some metrics
*
* @retval
*/
size_t metrics() {
size_t total_size=0;
// Iterate all objects in cache
for(typename std::vector<shared_ptr<Shard<T> > >::iterator it=this->shards.begin(); it != this->shards.end(); ++it) {
shared_ptr<Shard<T> >shard=*it;
{ // Scope for lock
mutex::scoped_lock lock(*shard->guard);
// tally
total_size+=shard->map.size();
}
}
return total_size;
};
/**
* Set a value into the cache
*
* @param id the key
* @param val shared_ptr to the object to set
* @param expiration UNIX timestamp
* @param mode the write mode
* @retval number of items written
*/
size_t set(Key id, shared_ptr<T> val, time_t expiration=0, const fastcache_writemode mode=FASTCACHE_WRITEMODE_WRITE_ALWAYS){
// Get shard
size_t index=this->calc_index(id);
shared_ptr<Shard<T> >shard=this->shards.at(index);
shared_ptr<CacheItem<T> >item=shared_ptr<CacheItem<T> >(new CacheItem<T>(val, expiration));
// Lock and write
mutex::scoped_lock lock(*shard->guard);
#ifdef FASTCACHE_SLOW
sleep(1);
#endif
if(mode==FASTCACHE_WRITEMODE_ONLY_WRITE_IF_SET) {
if(shard->map.find(id) == shard->map.end()) {
// Key not found. Return.
return 0;
} else {
// Erase it so we can set it below
shard->map.erase(id);
}
}
std::pair<typename std::map<Key, shared_ptr<CacheItem<T> > >::iterator,bool>result;
result=shard->map.insert(std::pair<Key,shared_ptr<CacheItem<T> > >(id,item)); //TODO... use .emplace() once we have C++11 !! (may be faster)
if(!result.second) {
// Key exists, so nothing was written
if(mode==FASTCACHE_WRITEMODE_ONLY_WRITE_IF_NOT_SET){
return 0;
} else {
// Erase and re-write
shard->map.erase(id);
shard->map.insert(std::pair<Key,shared_ptr<CacheItem<T> > >(id,item));
return 1;
}
}
return 1;
};
/**
* Find if a key exists
*
* @param id the key
* @retval 1 if the key exists, 0 otherwise
*/
size_t exists(Key id){
return (this->get(id))?1:0; // So we don't get false positives on expired keys
};
/**
* Delete a value from the cache
*
* @param id the key
* @retval the number of items erased
*/
size_t del(Key id){
// Get shard
size_t index=this->calc_index(id);
shared_ptr<Shard<T> >shard=this->shards.at(index);
// Lock and erase
mutex::scoped_lock lock(*shard->guard);
return shard->map.erase(id);
};
/**
* Get a value from the cache
*
* Does not throw for invalid keys (returns empty pointer)
*
* @param id the key
* @retval boost::shared_ptr<T>. ==empty pointer if nonexistent or expired.
* @throws FastcacheObjectLocked if #FASTCACHE_MUTABLE_DATA is set and object is in use
*/
shared_ptr<T> get(Key id){
// Get shard
size_t index=this->calc_index(id);
shared_ptr<Shard<T> >shard=this->shards.at(index);
// Lock
mutex::scoped_lock lock(*shard->guard);
// Delay if in slow mode...
#ifdef FASTCACHE_SLOW
sleep(1);
#endif
// OK, we now have exclusive access to the shard. So no race condition is possible for the affections of this item...
shared_ptr<CacheItem<T> >item;
try {
item=shard->map.at(id); // Will throw std::out_of_range if not there!
// Check for expired
if(item->expired()){
// It's expired. Erase it and return empty.
shard->map.erase(id);
return shared_ptr<T>();
}
} catch (std::exception& e) {
return shared_ptr<T>(); // Return empty since it wasn't found
}
// If we are allowing mutables, make sure no one else is using this data!
#ifdef FASTCACHE_MUTABLE_DATA
if(!item->data.unique()){
throw FastcacheObjectLocked();
}
#endif
return item->data;
};
protected:
/**
* We are the curator
*
* Purge expired keys, etc
*/
void curate(){
while(*this->curator_run) {
try {
// Snooze a little
boost::this_thread::sleep(boost::posix_time::milliseconds(FASTCACHE_CURATOR_SLEEP_MS));
// Iterate all objects in cache, checking for expired objects
for(typename std::vector<shared_ptr<Shard<T> > >::iterator it=this->shards.begin(); it != this->shards.end(); ++it) {
shared_ptr<Shard<T> >shard=*it;
{ // Scope for lock
mutex::scoped_lock lock(*shard->guard);
// Cull expired keys
shard->cull_expired_keys();
}
}
} catch(boost::thread_interrupted& e) {
// We were asked to leave?
return;
} catch(std::exception& e) {
// TODO... something bad happened in the curator thread
}
}
};
/**
* Calculate the shard index
*
* It is important that this function has a repeatable but otherwise randomish (uniform) output
* We use the boost hash, "a TR1 compliant hash function object"
*
* @param id
*/
size_t calc_index(Key id){
return (size_t) this->hash(id) % FASTCACHE_SHARDSIZE;
};
boost::hash<Key>hash;
std::vector<shared_ptr<Shard<T> > > shards;
shared_ptr<boost::thread>curator;
shared_ptr<boost::detail::atomic_count>curator_run;
};
}