1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MatthiasMullie\Scrapbook\Buffered; |
4
|
|
|
|
5
|
|
|
use MatthiasMullie\Scrapbook\Buffered\Utils\Buffer; |
6
|
|
|
use MatthiasMullie\Scrapbook\Buffered\Utils\Transaction; |
7
|
|
|
use MatthiasMullie\Scrapbook\KeyValueStore; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This class will serve as a local buffer to the real cache: anything read from |
11
|
|
|
* & written to the real cache will be stored in memory, so if any of those keys |
12
|
|
|
* is again requested in the same request, we can just grab it from memory |
13
|
|
|
* instead of having to get it over the wire. |
14
|
|
|
* |
15
|
|
|
* @author Matthias Mullie <[email protected]> |
16
|
|
|
* @copyright Copyright (c) 2014, Matthias Mullie. All rights reserved |
17
|
|
|
* @license LICENSE MIT |
18
|
|
|
*/ |
19
|
|
|
class BufferedStore implements KeyValueStore |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Transaction will already buffer all writes (until the transaction |
23
|
|
|
* has been committed/rolled back). As long as we immediately commit |
24
|
|
|
* to real store, it'll look as if no transaction is in progress & |
25
|
|
|
* all we'll be left with is the local copy of all data that can act |
26
|
|
|
* as buffer for follow-up requests. |
27
|
|
|
* All we'll need to add is also buffering non-write results. |
28
|
|
|
* |
29
|
|
|
* @var Transaction |
30
|
|
|
*/ |
31
|
|
|
protected $transaction; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Local in-memory storage, for the data we've already requested from |
35
|
|
|
* or written to the real cache. |
36
|
|
|
* |
37
|
|
|
* @var Buffer |
38
|
|
|
*/ |
39
|
|
|
protected $local; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var BufferedStore[] |
43
|
|
|
*/ |
44
|
|
|
protected $collections = array(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param KeyValueStore $cache The real cache we'll buffer for |
48
|
|
|
*/ |
49
|
|
|
public function __construct(KeyValueStore $cache) |
50
|
|
|
{ |
51
|
|
|
$this->local = new Buffer(); |
52
|
|
|
$this->transaction = new Transaction($this->local, $cache); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* In addition to all writes being stored to $local, we'll also |
57
|
|
|
* keep get() values around ;). |
58
|
|
|
* |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function get($key, &$token = null) |
62
|
|
|
{ |
63
|
|
|
$value = $this->transaction->get($key, $token); |
64
|
|
|
|
65
|
|
|
// only store if we managed to retrieve a value (valid token) and it's |
66
|
|
|
// not already in cache (or we may mess up tokens) |
67
|
|
|
if (false !== $value && false === $this->local->get($key, $localToken) && null === $localToken) { |
68
|
|
|
$this->local->set($key, $value); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* In addition to all writes being stored to $local, we'll also |
76
|
|
|
* keep get() values around ;). |
77
|
|
|
* |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function getMulti(array $keys, array &$tokens = null) |
81
|
|
|
{ |
82
|
|
|
$values = $this->transaction->getMulti($keys, $tokens); |
83
|
|
|
|
84
|
|
|
$missing = array_diff_key($values, $this->local->getMulti($keys)); |
85
|
|
|
if (!empty($missing)) { |
86
|
|
|
$this->local->setMulti($missing); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $values; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function set($key, $value, $expire = 0) |
96
|
|
|
{ |
97
|
|
|
$result = $this->transaction->set($key, $value, $expire); |
98
|
|
|
$this->transaction->commit(); |
99
|
|
|
|
100
|
|
|
return $result; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function setMulti(array $items, $expire = 0) |
107
|
|
|
{ |
108
|
|
|
$result = $this->transaction->setMulti($items, $expire); |
109
|
|
|
$this->transaction->commit(); |
110
|
|
|
|
111
|
|
|
return $result; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function delete($key) |
118
|
|
|
{ |
119
|
|
|
$result = $this->transaction->delete($key); |
120
|
|
|
$this->transaction->commit(); |
121
|
|
|
|
122
|
|
|
return $result; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function deleteMulti(array $keys) |
129
|
|
|
{ |
130
|
|
|
$result = $this->transaction->deleteMulti($keys); |
131
|
|
|
$this->transaction->commit(); |
132
|
|
|
|
133
|
|
|
return $result; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
public function add($key, $value, $expire = 0) |
140
|
|
|
{ |
141
|
|
|
$result = $this->transaction->add($key, $value, $expire); |
142
|
|
|
$this->transaction->commit(); |
143
|
|
|
|
144
|
|
|
return $result; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
public function replace($key, $value, $expire = 0) |
151
|
|
|
{ |
152
|
|
|
$result = $this->transaction->replace($key, $value, $expire); |
153
|
|
|
$this->transaction->commit(); |
154
|
|
|
|
155
|
|
|
return $result; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function cas($token, $key, $value, $expire = 0) |
162
|
|
|
{ |
163
|
|
|
$result = $this->transaction->cas($token, $key, $value, $expire); |
164
|
|
|
$this->transaction->commit(); |
165
|
|
|
|
166
|
|
|
return $result; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function increment($key, $offset = 1, $initial = 0, $expire = 0) |
173
|
|
|
{ |
174
|
|
|
$result = $this->transaction->increment($key, $offset, $initial, $expire); |
175
|
|
|
$this->transaction->commit(); |
176
|
|
|
|
177
|
|
|
return $result; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
|
|
public function decrement($key, $offset = 1, $initial = 0, $expire = 0) |
184
|
|
|
{ |
185
|
|
|
$result = $this->transaction->decrement($key, $offset, $initial, $expire); |
186
|
|
|
$this->transaction->commit(); |
187
|
|
|
|
188
|
|
|
return $result; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function touch($key, $expire) |
195
|
|
|
{ |
196
|
|
|
$result = $this->transaction->touch($key, $expire); |
197
|
|
|
$this->transaction->commit(); |
198
|
|
|
|
199
|
|
|
return $result; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* {@inheritdoc} |
204
|
|
|
*/ |
205
|
|
|
public function flush() |
206
|
|
|
{ |
207
|
|
|
foreach ($this->collections as $collection) { |
208
|
|
|
$collection->flush(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$result = $this->transaction->flush(); |
212
|
|
|
$this->transaction->commit(); |
213
|
|
|
|
214
|
|
|
return $result; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
|
|
public function getCollection($name) |
221
|
|
|
{ |
222
|
|
|
if (!isset($this->collections[$name])) { |
223
|
|
|
$collection = $this->transaction->getCollection($name); |
224
|
|
|
$this->collections[$name] = new static($collection); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $this->collections[$name]; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|