-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.h
151 lines (124 loc) · 2.97 KB
/
cache.h
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
#ifndef MC_CACHE_H
#define MC_CACHE_H
#include <assert.h>
#include <unordered_map>
#include <vector>
#include <list>
#include <string.h>
#include <functional>
#include <mutex>
#include <memory>
#include "protocol_binary.h"
#include "config.h"
namespace mc
{
struct cache
{
struct key
{
const unsigned char* d_; //key data
size_t len_; //key len
size_t memsize_; //size of key+value in memory (bytes)
explicit key(const unsigned char* d, size_t len, size_t memsize)
:d_(d)
,len_(len)
,memsize_(memsize)
{}
explicit key(const unsigned char* d, size_t len)
:d_(d)
,len_(len)
{}
bool operator==(const key& k) const
{
if (this == &k)
return true;
if (len_ != k.len_)
return false;
if (len_ == 0)
return true;
return ::memcmp(d_, k.d_, len_) == 0;
}
};
typedef std::list<key> lru; //LRU linked list
struct item
{
typedef std::vector<unsigned char> data;
data d_;
protocol_binary_request_header h_;
lru::iterator lru_ref_; //location in LRU list (list iterators are valid till deleted)
explicit item(data d, const protocol_binary_request_header& h)
:d_(std::move(d))
,h_(h)
{
assert(d_.size() >= h_.request.extlen + sizeof(h_));
}
item(item&& v)
:d_(std::move(v.d_))
,h_(v.h_)
,lru_ref_(v.lru_ref_)
{}
key get_key() const
{
return key(
d_.data() + sizeof(h_) + h_.request.extlen
,h_.request.keylen
,d_.size()
);
}
const unsigned char* get_data() const
{
return d_.data() + h_.request.extlen + sizeof(h_);
}
const unsigned char* get_value() const
{
return get_data() + h_.request.keylen;
}
const size_t get_data_len() const
{
return d_.size() - h_.request.extlen - sizeof(h_);
}
const size_t get_value_len() const
{
return get_data_len() - h_.request.keylen;
}
void set_lru(lru::iterator it)
{
lru_ref_ = it;
}
private:
//never copy data around, move only
item(const item&) = delete;
item& operator=(const item&) = delete;
};
struct hasher
{
size_t operator()(const key& k) const;
};
typedef std::unordered_map<key, std::shared_ptr<item>, hasher> hash;
cache(size_t maxmemsize, bool thread_safe);
~cache();
//may throw
void set(item v);
bool cas(item v, uint64_t cas);
bool remove(const item& v, uint64_t cas);
std::shared_ptr<item> get(const key& k);
bool get_value(std::vector<unsigned char>& v, const key& k);
private:
std::unique_ptr<std::mutex> m_;
size_t maxmemsize_;
size_t used_mem_;
hash h_;
lru lru_;
void do_set(item v);
bool do_cas(item v, uint64_t cas);
bool do_remove(const item& v, uint64_t cas);
bool do_get_value(std::vector<unsigned char>& v, const key& k);
std::shared_ptr<item> do_get_item(const key& k);
std::shared_ptr<item> do_get(const key& k);
void delete_item(key k);
void free_mem(size_t size);
cache(const cache&) = delete;
cache& operator=(cache&) = delete;
};
}
#endif