-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.cpp
210 lines (178 loc) · 3.92 KB
/
cache.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "cache.h"
#include "session.h"
#include "murmur3_hash.h"
#include <assert.h>
#include <algorithm>
#include <stdexcept>
#include <iostream>
using namespace mc;
size_t cache::hasher::operator()(const key& k) const
{
return MurmurHash3_x86_32(k.d_, k.len_);
}
cache::cache(size_t maxmemsize, bool thread_safe)
:maxmemsize_(maxmemsize)
,used_mem_(0)
{
assert(maxmemsize);
if (thread_safe)
m_.reset(new std::mutex);
//some initial hints for the hash
//assuming the average value size is 1% of the max
size_t itemmem = (MAX_VALUELEN + MAX_KEYLEN)/100 + sizeof(protocol_binary_request_header);
if (itemmem) {
size_t items = maxmemsize_/itemmem;
std::clog << "cache params: itemmem=" << itemmem << " maxmemsize=" << maxmemsize_ << " items=" << items << std::endl;
h_.reserve(items); //this is just a hint for the hash table to pre-allocate some buckets
}
}
cache::~cache()
{
}
bool cache::remove(const item& v, uint64_t cas)
{
if (m_) {
std::unique_lock<std::mutex> lock(*m_);
return do_remove(v, cas);
}
else {
return do_remove(v, cas);
}
}
bool cache::cas(item v, uint64_t cas)
{
if (m_) {
std::unique_lock<std::mutex> lock(*m_);
return do_cas(std::move(v), cas);
}
else {
return do_cas(std::move(v), cas);
}
}
void cache::set(item v)
{
if (m_) {
std::unique_lock<std::mutex> lock(*m_);
do_set(std::move(v));
}
else {
do_set(std::move(v));
}
}
std::shared_ptr<cache::item> cache::get(const key& k)
{
if (m_) {
std::unique_lock<std::mutex> lock(*m_);
return do_get(k);
}
else {
return do_get(k);
}
}
bool cache::get_value(std::vector<unsigned char>& v, const key& k)
{
if (m_) {
std::unique_lock<std::mutex> lock(*m_);
return do_get_value(v, k);
}
else {
return do_get_value(v, k);
}
}
bool cache::do_cas(item v, uint64_t cas)
{
//handle cas
std::shared_ptr<item> p = do_get(v.get_key());
if (p && p->h_.request.cas != cas) {
return false;
}
do_set(std::move(v));
return true;
}
void cache::do_set(item v)
{
key k = v.get_key();
auto it = h_.find(k);
if (it != h_.end()) {
delete_item(k);
}
size_t itemmem = (sizeof(v.h_) + v.h_.request.bodylen);
if (itemmem + used_mem_ > maxmemsize_) {
//try to free at least 1% of the max
free_mem(std::max(itemmem*2, maxmemsize_/100));
}
auto lruit = lru_.insert(lru_.end(), k); //add to the LRU
v.set_lru(lruit);
try {
std::shared_ptr<item> pi(new item(std::move(v)));
h_.emplace(std::make_pair(k, pi));
}
catch (const std::exception&)
{
lru_.erase(lruit);
throw;
}
used_mem_ += itemmem;
}
bool cache::do_get_value(std::vector<unsigned char>& v, const key& k)
{
std::shared_ptr<item> p = do_get(k);
if (!p)
return false;
unsigned int value_len = p->get_data_len() - p->h_.request.keylen;
const unsigned char* pd = p->get_data()+p->h_.request.keylen;
v.insert(v.end(), pd, pd + value_len);
return true;
}
std::shared_ptr<cache::item> cache::do_get(const key& k)
{
auto it = h_.find(k);
if (it == h_.end())
return std::shared_ptr<item>();
assert(!lru_.empty());
//refresh in the LRU list
if (it->second->lru_ref_ != --lru_.end()) {
lru_.splice(lru_.end(), lru_, it->second->lru_ref_);
}
return it->second;
}
bool cache::do_remove(const item& v, uint64_t cas)
{
try {
if (cas) {
//handle cas
std::shared_ptr<item> p = do_get(v.get_key());
if (p && p->h_.request.cas != cas) {
return false;
}
}
delete_item(v.get_key());
}
catch (const std::exception&) {
}
return true;
}
void cache::delete_item(key k) //pass by value
{
auto it = h_.find(k);
if (it == h_.end()) {
throw std::runtime_error("cache integrity error");
}
//remove from LRU
lru_.erase(it->second->lru_ref_);
h_.erase(it);
used_mem_ -= k.memsize_;
}
void cache::free_mem(size_t size) //size to free
{
assert(size);
size_t freed = 0;
//remove according to LRU
for (auto it = lru_.begin(); it != lru_.end() && freed < size; ) {
auto n = it->memsize_;
auto tmp = it;
++it;
delete_item(*tmp);
freed += n;
}
}