summaryrefslogtreecommitdiff
path: root/libadhocutil/cache.impl.h
blob: dffed7f853bb5441888dfe6e92f946639f1921a2 (plain)
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
#ifndef ADHOCUTIL_CACHE_IMPL_H
#define ADHOCUTIL_CACHE_IMPL_H

#include "cache.h"
#include <boost/lambda/lambda.hpp>
#include "lockHelpers.h"

namespace AdHoc {

/// @cond
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(new T(t))
{
}

template<typename T, typename K>
ObjectCacheable<T, K>::ObjectCacheable(typename Cacheable<T, K>::Value & t, const K & k, time_t vu) :
	Cacheable<T, K>(k, vu),
	value(t)
{
}

template<typename T, typename K>
typename Cacheable<T, K>::Value
ObjectCacheable<T, K>::item() const
{
	return value;
}

template<typename T, typename K>
CallCacheable<T, K>::CallCacheable(const Factory & t, const K & k, time_t vu) :
	Cacheable<T, K>(k, vu),
	value(t)
{
}

template<typename T, typename K>
typename Cacheable<T, K>::Value
CallCacheable<T, K>::item() const
{
	Lock(lock);
	if (auto t = boost::get<typename Cacheable<T, K>::Value>(&value)) {
		return *t;
	}
	const Factory & f = boost::get<Factory>(value);
	value = typename Cacheable<T, K>::Value(new T(f()));
	return boost::get<typename Cacheable<T, K>::Value>(value);
}


template<typename T, typename K>
PointerCallCacheable<T, K>::PointerCallCacheable(const Factory & t, const K & k, time_t vu) :
	Cacheable<T, K>(k, vu),
	value(t)
{
}

template<typename T, typename K>
typename Cacheable<T, K>::Value
PointerCallCacheable<T, K>::item() const
{
	Lock(lock);
	if (auto t = boost::get<typename Cacheable<T, K>::Value>(&value)) {
		return *t;
	}
	const Factory & f = boost::get<Factory>(value);
	value = f();
	return boost::get<typename Cacheable<T, K>::Value>(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, Value & 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 Factory & tf, time_t validUntil)
{
	Lock(lock);
	cached.insert(Element(new CallCacheable<T, K>(tf, k, validUntil)));
}

template<typename T, typename K>
void
Cache<T, K>::add(const K & k, const PointerFactory & tf, time_t validUntil)
{
	Lock(lock);
	cached.insert(Element(new PointerCallCacheable<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>
typename Cache<T, K>::Value
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>::remove(const K & k)
{
	Lock(lock);
	cached.template get<byKey>().erase(k);
}

template<typename T, typename K>
void
Cache<T, K>::clear()
{
	Lock(lock);
	cached.clear();
}

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;
	}
}
/// @endcond

}

#endif