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
|
#ifndef ADHOCUTIL_CACHE_IMPL_H
#define ADHOCUTIL_CACHE_IMPL_H
#include "cache.h"
#include <boost/lambda/lambda.hpp>
#include "lockHelpers.h"
template<typename T, typename K>
Cacheable<T, K>::Cacheable(const K & k, time_t vu) :
key(k),
validUntil(vu)
{
}
template<typename T, typename K>
ObjectCacheable<T, K>::ObjectCacheable(const T & t, const K & k, time_t vu) :
Cacheable<T, K>(k, vu),
value(t)
{
}
template<typename T, typename K>
const T &
ObjectCacheable<T, K>::item() const
{
return value;
}
template<typename T, typename K>
CallCacheable<T, K>::CallCacheable(const T & t, const K & k, time_t vu) :
Cacheable<T, K>(k, vu),
value(t)
{
}
template<typename T, typename K>
CallCacheable<T, K>::CallCacheable(const boost::function<T()> & t, const K & k, time_t vu) :
Cacheable<T, K>(k, vu),
value(t)
{
}
template<typename T, typename K>
const T &
CallCacheable<T, K>::item() const
{
Lock(lock);
const T * t = boost::get<T>(&value);
if (t) {
return *t;
}
const boost::function<T()> & f = boost::get<boost::function<T()>>(value);
value = f();
return boost::get<T>(value);
}
template<typename T, typename K>
Cache<T, K>::Cache() :
lastPruneTime(time(NULL))
{
}
template<typename T, typename K>
void
Cache<T, K>::add(const K & k, const T & t, time_t validUntil)
{
Lock(lock);
cached.insert(Element(new ObjectCacheable<T, K>(t, k, validUntil)));
}
template<typename T, typename K>
void
Cache<T, K>::add(const K & k, const boost::function<T()> & tf, time_t validUntil)
{
Lock(lock);
cached.insert(Element(new CallCacheable<T, K>(tf, k, validUntil)));
}
template<typename T, typename K>
typename Cache<T, K>::Element
Cache<T, K>::getItem(const K & k) const
{
{
SharedLock(lock);
auto & collection = cached.template get<byKey>();
auto i = collection.find(k);
if (i == collection.end()) {
return Element();
}
if ((*i)->validUntil > time(NULL)) {
return (*i);
}
}
prune();
return Element();
}
template<typename T, typename K>
const T *
Cache<T, K>::get(const K & k) const
{
auto i = getItem(k);
if (i) {
return &i->item();
}
return nullptr;
}
template<typename T, typename K>
size_t
Cache<T, K>::size() const
{
return cached.size();
}
template<typename T, typename K>
void
Cache<T, K>::prune() const
{
auto now = time(NULL);
if (lastPruneTime < now) {
Lock(lock);
auto & collection = cached.template get<byValidity>();
auto range = collection.range(boost::multi_index::unbounded, boost::lambda::_1 < now);
collection.erase(range.first, range.second);
lastPruneTime = now;
}
}
#endif
|