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

#include "cache.h" // IWYU pragma: export
#include "lockHelpers.h"
#include <boost/lambda/lambda.hpp>
#include <mutex>
#include <shared_mutex>

namespace AdHoc {

	/// @cond
	template<typename T, typename K> Cacheable<T, K>::Cacheable(K k, time_t vu) : key(std::move(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(std::make_shared<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(std::move(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 = std::get_if<0>(&value)) {
			return *t;
		}
		const Factory & f = std::get<Factory>(value);
		value = std::make_shared<T>(f());
		return std::get<0>(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 = std::get_if<0>(&value)) {
			return *t;
		}
		const Factory & f = std::get<Factory>(value);
		value = f();
		return std::get<0>(value);
	}

	template<typename T, typename K> Cache<T, K>::Cache() : lastPruneTime(time(nullptr)) { }

	template<typename T, typename K>
	void
	Cache<T, K>::add(const K & k, const T & t, time_t validUntil)
	{
		Lock(lock);
		cached.insert(std::make_shared<ObjectCacheable<T, K>>(t, k, validUntil));
	}

	template<typename T, typename K>
	void
	Cache<T, K>::addPointer(const K & k, Value & t, time_t validUntil)
	{
		Lock(lock);
		cached.insert(std::make_shared<ObjectCacheable<T, K>>(t, k, validUntil));
	}

	template<typename T, typename K>
	void
	Cache<T, K>::addFactory(const K & k, const Factory & tf, time_t validUntil)
	{
		Lock(lock);
		cached.insert(std::make_shared<CallCacheable<T, K>>(tf, k, validUntil));
	}

	template<typename T, typename K>
	void
	Cache<T, K>::addPointerFactory(const K & k, const PointerFactory & tf, time_t validUntil)
	{
		Lock(lock);
		cached.insert(std::make_shared<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(nullptr)) {
				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(nullptr);
		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