-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathMemcache.php
More file actions
384 lines (352 loc) · 12.3 KB
/
Copy pathMemcache.php
File metadata and controls
384 lines (352 loc) · 12.3 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
/**
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Interface for the "memcache" PHP extension.
*
* Implementation of the interface for the "memcache" PHP extension (see
* http://php.net/manual/en/book.memcache.php) using the App Engine memcache
* API).
*
* User provided "flags" arguments are currently ignored and many methods are
* no-ops.
*/
namespace Google\AppEngine\Api\Memcache;
use google\appengine\MemcacheDeleteRequest;
use google\appengine\MemcacheDeleteResponse;
use google\appengine\MemcacheDeleteResponse\DeleteStatusCode;
use google\appengine\MemcacheFlushRequest;
use google\appengine\MemcacheFlushResponse;
use google\appengine\MemcacheGetRequest;
use google\appengine\MemcacheGetResponse;
use google\appengine\MemcacheIncrementRequest;
use google\appengine\MemcacheIncrementResponse;
use google\appengine\MemcacheIncrementResponse\IncrementStatusCode;
use google\appengine\MemcacheSetRequest;
use google\appengine\MemcacheSetRequest\SetPolicy;
use google\appengine\MemcacheSetResponse;
use google\appengine\MemcacheSetResponse\SetStatusCode;
use Google\AppEngine\Runtime\ApiProxy;
use Google\AppEngine\Runtime\Error;
use Google\AppEngine\Runtime\MemcacheUtils;
/**
* An interface to the App Engine memory cache with an interface compatible with
* the "memcache" PHP extension (see http://php.net/manual/en/book.memcache.php)
*
* All instances of this class use the same memory pool for their keys and
* values.
*/
class Memcache {
/**
* Adds a new item to the cache. Will fail if the key is already present in
* the cache.
*
* @param string $key The key associated with the value added to the cache.
*
* @param mixed $value The value to add to the cache.
*
* @param int $flag This parameter is present only for compatibility and is
* ignored.
*
* @param int $expire The delay before the item is removed from the cache. If
* $expire <= 2592000 then it is interpreted as the number
* of seconds from the time of the call to wait before
* removing the item from the cache. If $expire > 2592000
* then it is interpreted as the absolute Unix epoch time
* when the value will expire.
*
* @return bool true if the item was successfully added to the cache, false
* otherwise.
*/
public function add($key, $value, $flag = null, $expire = 0) {
// Sending of a key of 'null' or an unset value is a failure.
if (is_null($key)) {
return false;
}
try {
$set_results = MemcacheUtils::setMultiWithPolicy(array($key => $value),
$expire,
SetPolicy::ADD);
} catch (Error $e) {
return false;
}
return $set_results[0] == SetStatusCode::STORED;
}
/**
* This function is present only for compatibility and does nothing.
*/
public function addServer($host) {
return true;
}
/**
* This function is present only for compatibility and does nothing.
*/
public function close() {
return true;
}
/**
* This function is present only for compatibility and does nothing.
*/
public function connect($host, $port = null, $timeout = 1) {
return true;
}
/**
* Decrements a cached item's value. The value must be a int, float or string
* representing an integer e.g. 5, 5.0 or "5" or the call with fail.
*
* @param string $key The key associated with the value to decrement.
*
* @param int $value The amount to decrement the value.
*
* @return mixed On success, the new value of the item is returned. On
* failure, false is returned.
*/
public function decrement($key, $value = 1) {
return $this->incrementInternal($key, $value, false);
}
/**
* Deletes an item from the cache.
*
* @param string $key The key associated with the item to delete.
*
* @return bool true if the item was successfully deleted from the cache,
* false otherwise. Note that this will return false if $key is
* not present in the cache.
*/
public function delete($key) {
// Sending of a key of 'null' or an unset value is a failure.
if (is_null($key)) {
return false;
}
$request = new MemcacheDeleteRequest();
$response = new MemcacheDeleteResponse();
$request->addItem()->setKey($key);
try {
ApiProxy::makeSyncCall('memcache', 'Delete', $request, $response);
} catch (Error $e) {
return false;
}
$status_list = $response->getDeleteStatusList();
return $status_list[0] == DeleteStatusCode::DELETED;
}
/**
* Removes all items from cache.
*
* @return bool true if all items were removed, false otherwise.
*/
public function flush() {
$request = new MemcacheFlushRequest();
$response = new MemcacheFlushResponse();
try {
ApiProxy::makeSyncCall('memcache', 'FlushAll', $request, $response);
} catch (Error $e) {
return false;
}
return true;
}
private function getMulti($keys, $flags = null) {
$request = new MemcacheGetRequest();
$response = new MemcacheGetResponse();
foreach ($keys as $key) {
$request->addKey($key);
}
ApiProxy::makeSyncCall('memcache', 'Get', $request, $response);
$return_value = array();
foreach ($response->getItemList() as $item) {
try {
$return_value[$item->getKey()] = MemcacheUtils::deserializeValue(
$item->getValue(), $item->getFlags());
} catch (\UnexpectedValueException $e) {
// Skip entries that cannot be deserialized.
}
}
return $return_value;
}
/**
* Fetches previously stored data from the cache.
*
* @param string|string[] $keys The key associated with the value to fetch, or
* an array of keys if fetching multiple values.
*
* @param int $flags This parameter is present only for compatibility and is
* ignored. It should return the stored flag value.
*
* @return mixed On success, the string associated with the key, or an array
* of key-value pairs when $keys is an array. On failure, false
* is returned.
*/
public function get($keys, $flags = null) {
if (is_array($keys)) {
$return_value = $this->getMulti($keys, $flags);
if (empty($return_value)) {
return false;
} else {
return $return_value;
}
} else {
try {
$return_value = $this->getMulti(array($keys), array($flags));
} catch (Error $e) {
return false;
}
if (array_key_exists($keys, $return_value)) {
return $return_value[$keys];
} else {
return false;
}
}
}
// Not implemented:
// getExtendedStats
// getServerStatus
// getStats
// getVersion
/**
* Increments a cached item's value. The value must be a int, float or string
* representing an integer e.g. 5, 5.0 or "5" or the call with fail.
*
* @param string $key The key associated with the value to increment.
*
* @param int $value The amount to increment the value.
*
* @return mixed On success, the new value of the item is returned. On
* failure, false is returned.
*/
public function increment($key, $value = 1) {
return $this->incrementInternal($key, $value, true);
}
/**
* Internal implementation of increment (and decrement).
*
* @param string $key The key associated with the value to increment.
*
* @param int $value The amount to increment the value.
*
* @param bool $is_incr Whether to perform an increment or decrement.
*
* @return mixed On success, the new value of the item is returned. On
* failure, false is returned.
*/
private function incrementInternal($key, $value, $is_incr) {
// Sending of a key of 'null' or an unset value is a failure.
if (is_null($key)) {
return false;
}
$request = new MemcacheIncrementRequest();
$response = new MemcacheIncrementResponse();
$request->setKey($key);
$request->setDelta($value);
if (!$is_incr) {
$request->setDirection(MemcacheIncrementRequest\Direction::DECREMENT);
}
try {
ApiProxy::makeSyncCall('memcache', 'Increment', $request, $response);
} catch (Exception $e) {
return false;
}
if ($response->hasNewValue()) {
return $response->getNewValue();
} else {
return false;
}
}
/**
* This function is present only for compatibility and does nothing.
*/
public function pconnect($host, $port = null, $timeout = 1) {
return true;
}
/**
* Replaces an existing item in the cache. Will fail if the key is not already
* present in the cache.
*
* @param string $key The key associated with the value that will be replaced
* in the cache.
*
* @param mixed $value The new cache value.
*
* @param int $flag This parameter is present only for compatibility and is
* ignored.
*
* @param int $expire The delay before the item is removed from the cache. If
* $expire <= 2592000 then it is interpreted as the number
* of seconds from the time of the call to wait before
* removing the item from the cache. If $expire > 2592000
* then it is interpreted as the absolute Unix epoch time
* when the value will expire.
*
* @return bool true if the item was successfully replaced in the cache,
* false otherwise.
*/
public function replace($key, $value, $flag = null, $expire = 0) {
// Sending of a key of 'null' or an unset value is a failure.
if (is_null($key)) {
return false;
}
try {
$set_results = MemcacheUtils::setMultiWithPolicy(array($key => $value),
$expire,
SetPolicy::REPLACE);
} catch (Error $e) {
return false;
}
return $set_results[0] == SetStatusCode::STORED;
}
/**
* Sets the value of a key in the cache regardless of whether it is currently
* present or not.
*
* @param string $key The key associated with the value that will be replaced
* in the cache.
*
* @param mixed $value The new cache value.
*
* @param int $flag This parameter is present only for compatibility and is
* ignored.
*
* @param int $expire The delay before the item is removed from the cache. If
* $expire <= 2592000 then it is interpreted as the number
* of seconds from the time of the call to wait before
* removing the item from the cache. If $expire > 2592000
* then it is interpreted as the absolute Unix epoch time
* when the value will expire.
*
* @return bool true if the item was successfully replaced the cache, false
* otherwise.
*/
public function set($key, $value, $flag = null, $expire = 0) {
// Sending of a key of 'null' or an unset value is a failure.
if (is_null($key)) {
return false;
}
try {
$set_results = MemcacheUtils::setMultiWithPolicy(array($key => $value),
$expire,
SetPolicy::SET);
} catch (Error $e) {
return false;
}
return $set_results[0] == SetStatusCode::STORED;
}
/**
* This function is present only for compatibility and does nothing.
*/
public function setCompressThreshold($threshold, $min_savings = 0.2) {
// Compression is not supported.
return false;
}
// setServerParams not implemented.
}