App Engine Python SDK version 1.8.1
[gae.git] / python / php / sdk / google / appengine / runtime / Memcache.php
blob7e6f110145acbea1891b8be4dce55c814966972c
1 <?php
2 /**
3 * Copyright 2007 Google Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 /**
18 * Interface for the "memcache" PHP extension.
20 * Implementation of the interface for the "memcache" PHP extension (see
21 * http://php.net/manual/en/book.memcache.php) using the App Engine memcache
22 * API).
24 * User provided "flags" arguments are currently ignored and many methods are
25 * no-ops.
27 use \google\appengine\MemcacheDeleteRequest;
28 use \google\appengine\MemcacheDeleteResponse;
29 use \google\appengine\MemcacheDeleteResponse\DeleteStatusCode;
30 use \google\appengine\MemcacheFlushRequest;
31 use \google\appengine\MemcacheFlushResponse;
32 use \google\appengine\MemcacheGetRequest;
33 use \google\appengine\MemcacheGetResponse;
34 use \google\appengine\MemcacheIncrementRequest;
35 use \google\appengine\MemcacheIncrementResponse;
36 use \google\appengine\MemcacheIncrementResponse\IncrementStatusCode;
37 use \google\appengine\MemcacheSetRequest;
38 use \google\appengine\MemcacheSetRequest\SetPolicy;
39 use \google\appengine\MemcacheSetResponse;
40 use \google\appengine\MemcacheSetResponse\SetStatusCode;
41 use \google\appengine\runtime\ApiProxy;
42 use \google\appengine\runtime\Error;
43 use \google\appengine\runtime\MemcacheUtils;
45 require_once 'google/appengine/api/memcache/memcache_service_pb.php';
46 require_once 'google/appengine/runtime/MemcacheUtils.php';
47 require_once 'google/appengine/runtime/ApiProxy.php';
48 require_once 'google/appengine/runtime/Error.php';
50 /**
51 * Adds a new item to the cache. Will fail if the key is already present in the
52 * cache.
54 * @param Memcache $memcache_obj The cache instance to add item to.
56 * @param string $key The key associated with the value added to the cache.
58 * @param mixed $value The value to add to the cache.
60 * @param int $flag This parameter is present only for compatibility and is
61 * ignored.
63 * @param int $expire The delay before the item is removed from the cache. If
64 * $expire <= 2592000 then it is interpreted as the number
65 * of seconds from the time of the call to wait before
66 * removing the item from the cache. If $expire > 2592000
67 * then it is interpreted as the absolute Unix epoch time
68 * when the value will expire.
70 * @return bool true if the item was successfully added to the cache, false
71 * otherwise.
73 function memcache_add($memcache_obj, $key, $value, $flag = null, $expire = 0) {
74 return $memcache_obj->add($key, $value, $flag, $expire);
77 /**
78 * This function is present only for compatibility and does nothing.
80 function memcache_add_server($memcache_obj, $host) {
81 return $memcache_obj->addServer($host);
84 /**
85 * This function is present only for compatibility and does nothing.
87 function memcache_close($memcache_obj) {
88 return $memcache_obj->close();
91 /**
92 * This function is present only for compatibility and does nothing.
94 function memcache_connect($host, $port = null, $timeout = 1) {
95 $memcache_obj = new Memcache();
96 if (!$memcache_obj->connect($host, $port, $timeout)) {
97 return false;
98 } else {
99 return $memcache_obj;
104 * Decrements a cached item's value. The value must be a int, float or string
105 * representing an integer e.g. 5, 5.0 or "5" or the call with fail.
107 * @param Memcache $memcache_obj The cache instance to decrement the value in.
109 * @param string $key The key associated with the value to decrement.
111 * @param int $value The amount to decrement the value.
113 * @return mixed On success, the new value of the item is returned. On
114 * failure, false is returned.
116 function memcache_decrement($memcache_obj, $key, $value = 1) {
117 return $memcache_obj->decrement($key, $value);
121 * Deletes an item from the cache.
123 * @param Memcache $memcache_obj The cache instance to delete the item from.
125 * @param string $key The key associated with the item to delete.
127 * @return bool true if the item was successfully deleted from the cache,
128 * false otherwise. Note that this will return false if $key is
129 * not present in the cache.
131 function memcache_delete($memcache_obj, $key) {
132 return $memcache_obj->delete($key);
136 * Removes all items from cache.
138 * @param Memcache $memcache_obj The cache instance to flush.
140 * @return bool true if all items were removed, false otherwise.
142 function memcache_flush($memcache_obj) {
143 return $memcache_obj->flush();
147 * Fetches previously stored data from the cache.
149 * @param string|string[] $keys The key associated with the value to fetch, or
150 * an array of keys if fetching multiple values.
152 * @param Memcache $memcache_obj The cache instance to get the item from.
154 * @param int $flags This parameter is present only for compatibility and is
155 * ignored. It should return the stored flag value.
157 * @return mixed On success, the string associated with the key, or an array
158 * of key-value pairs when $keys is an array. On failure, false
159 * is returned.
161 function memcache_get($memcache_obj, $keys, $flags = null) {
162 return $memcache_obj->get($keys, $flags);
166 * Increments a cached item's value. The value must be a int, float or string
167 * representing an integer e.g. 5, 5.0 or "5" or the call with fail.
169 * @param Memcache $memcache_obj The cache instance to increment the value in.
171 * @param string $key The key associated with the value to decrement.
173 * @param int $value The amount to increment the value.
175 * @return mixed On success, the new value of the item is returned. On
176 * failure, false is returned.
178 function memcache_increment($memcache_obj, $key, $value = 1) {
179 return $memcache_obj->increment($key, $value);
183 * This function is present only for compatibility and does nothing.
185 function memcache_pconnect($memcache_obj, $host, $port = null, $timeout = 1) {
186 $memcache_obj = new Memcache();
187 if (!$memcache_obj->connect($host, $port, $timeout)) {
188 return false;
189 } else {
190 return $memcache_obj;
195 * Replaces an existing item in the cache. Will fail if the key is not already
196 * present in the cache.
198 * @param Memcache $memcache_obj The cache instance to store the item in.
200 * @param string $key The key associated with the value that will be replaced in
201 * the cache.
203 * @param mixed $value The new cache value.
205 * @param int $flag This parameter is present only for compatibility and is
206 * ignored.
208 * @param int $expire The delay before the item is removed from the cache. If
209 * $expire <= 2592000 then it is interpreted as the number
210 * of seconds from the time of the call to wait before
211 * removing the item from the cache. If $expire > 2592000
212 * then it is interpreted as the absolute Unix epoch time
213 * when the value will expire.
215 * @return bool true if the item was successfully replaced in the cache,
216 * false otherwise.
218 function memcache_replace($memcache_obj,
219 $key,
220 $value,
221 $flag = null,
222 $expire = 0) {
223 return $memcache_obj->replace($key, $value, $flag, $expire);
227 * Sets the value of a key in the cache regardless of whether it is currently
228 * present or not.
230 * @param Memcache $memcache_obj The cache instance to store the item in.
232 * @param string $key The key associated with the value that will be replaced in
233 * the cache.
235 * @param mixed $value The new cache value.
237 * @param int $flag This parameter is present only for compatibility and is
238 * ignored.
240 * @param int $expire The delay before the item is removed from the cache. If
241 * $expire <= 2592000 then it is interpreted as the number
242 * of seconds from the time of the call to wait before
243 * removing the item from the cache. If $expire > 2592000
244 * then it is interpreted as the absolute Unix epoch time
245 * when the value will expire.
247 * @return bool true if the item was successfully replaced the cache, false
248 * otherwise.
250 function memcache_set($memcache_obj, $key, $value, $flag = null, $expire = 0) {
251 return $memcache_obj->set($key, $value, $flag, $expire);
255 * This function is present only for compatibility and does nothing.
257 function memcache_set_compress_threshold($memcache_obj,
258 $threshold,
259 $min_savings = 0.2) {
260 $memcache_obj->setCompressThreshold($threshold, $min_savings);
264 * An interface to the App Engine memory cache with an interface compatible with
265 * the "memcache" PHP extension (see http://php.net/manual/en/book.memcache.php)
267 * All instances of this class use the same memory pool for their keys and
268 * values.
270 class Memcache {
273 * Adds a new item to the cache. Will fail if the key is already present in
274 * the cache.
276 * @param string $key The key associated with the value added to the cache.
278 * @param mixed $value The value to add to the cache.
280 * @param int $flag This parameter is present only for compatibility and is
281 * ignored.
283 * @param int $expire The delay before the item is removed from the cache. If
284 * $expire <= 2592000 then it is interpreted as the number
285 * of seconds from the time of the call to wait before
286 * removing the item from the cache. If $expire > 2592000
287 * then it is interpreted as the absolute Unix epoch time
288 * when the value will expire.
290 * @return bool true if the item was successfully added to the cache, false
291 * otherwise.
293 public function add($key, $value, $flag = null, $expire = 0) {
294 // Sending of a key of 'null' or an unset value is a failure.
295 if (is_null($key)) {
296 return false;
299 try {
300 $set_results = MemcacheUtils::setMultiWithPolicy(array($key => $value),
301 $expire,
302 SetPolicy::ADD);
303 } catch (Error $e) {
304 return false;
306 return $set_results[0] == SetStatusCode::STORED;
310 * This function is present only for compatibility and does nothing.
312 public function addServer($host) {
313 return true;
317 * This function is present only for compatibility and does nothing.
319 public function close() {
320 return true;
324 * This function is present only for compatibility and does nothing.
326 public function connect($host, $port = null, $timeout = 1) {
327 return true;
331 * Decrements a cached item's value. The value must be a int, float or string
332 * representing an integer e.g. 5, 5.0 or "5" or the call with fail.
334 * @param string $key The key associated with the value to decrement.
336 * @param int $value The amount to decrement the value.
338 * @return mixed On success, the new value of the item is returned. On
339 * failure, false is returned.
341 public function decrement($key, $value = 1) {
342 return $this->increment($key, -$value);
346 * Deletes an item from the cache.
348 * @param string $key The key associated with the item to delete.
350 * @return bool true if the item was successfully deleted from the cache,
351 * false otherwise. Note that this will return false if $key is
352 * not present in the cache.
354 public function delete($key) {
355 // Sending of a key of 'null' or an unset value is a failure.
356 if (is_null($key)) {
357 return false;
360 $request = new MemcacheDeleteRequest();
361 $response = new MemcacheDeleteResponse();
363 $request->addItem()->setKey($key);
365 try {
366 ApiProxy::makeSyncCall('memcache', 'Delete', $request, $response);
367 } catch (Error $e) {
368 return false;
370 $status_list = $response->getDeleteStatusList();
371 return $status_list[0] == DeleteStatusCode::DELETED;
375 * Removes all items from cache.
377 * @return bool true if all items were removed, false otherwise.
379 public function flush() {
380 $request = new MemcacheFlushRequest();
381 $response = new MemcacheFlushResponse();
383 try {
384 ApiProxy::makeSyncCall('memcache', 'FlushAll', $request, $response);
385 } catch (Error $e) {
386 return false;
388 return true;
391 private function getMulti($keys, $flags = null) {
392 $request = new MemcacheGetRequest();
393 $response = new MemcacheGetResponse();
395 foreach ($keys as $key) {
396 $request->addKey($key);
399 ApiProxy::makeSyncCall('memcache', 'Get', $request, $response);
401 $return_value = array();
402 foreach ($response->getItemList() as $item) {
403 $return_value[$item->getKey()] = MemcacheUtils::deserializeValue(
404 $item->getValue(), $item->getFlags());
406 return $return_value;
410 * Fetches previously stored data from the cache.
412 * @param string|string[] $keys The key associated with the value to fetch, or
413 * an array of keys if fetching multiple values.
415 * @param int $flags This parameter is present only for compatibility and is
416 * ignored. It should return the stored flag value.
418 * @return mixed On success, the string associated with the key, or an array
419 * of key-value pairs when $keys is an array. On failure, false
420 * is returned.
422 public function get($keys, $flags = null) {
423 if (is_array($keys)) {
424 $return_value = $this->getMulti($keys, $flags);
425 if (empty($return_value)) {
426 return false;
427 } else {
428 return $return_value;
430 } else {
431 try {
432 $return_value = $this->getMulti(array($keys), array($flags));
433 } catch (Error $e) {
434 return false;
436 if (array_key_exists($keys, $return_value)) {
437 return $return_value[$keys];
438 } else {
439 return false;
444 // Not implemented:
445 // getExtendedStats
446 // getServerStatus
447 // getStats
448 // getVersion
451 * Increments a cached item's value. The value must be a int, float or string
452 * representing an integer e.g. 5, 5.0 or "5" or the call with fail.
454 * @param string $key The key associated with the value to decrement.
456 * @param int $value The amount to increment the value.
458 * @return mixed On success, the new value of the item is returned. On
459 * failure, false is returned.
461 public function increment($key, $value = 1) {
462 // Sending of a key of 'null' or an unset value is a failure.
463 if (is_null($key)) {
464 return false;
467 $request = new MemcacheIncrementRequest();
468 $response = new MemcacheIncrementResponse();
469 $request->setKey($key);
470 $request->setDelta($value);
472 try {
473 ApiProxy::makeSyncCall('memcache', 'Increment', $request, $response);
474 } catch (Exception $e) {
475 return false;
477 if ($response->hasNewValue()) {
478 return $response->getNewValue();
479 } else {
480 return false;
485 * This function is present only for compatibility and does nothing.
487 public function pconnect($host, $port = null, $timeout = 1) {
488 return true;
492 * Replaces an existing item in the cache. Will fail if the key is not already
493 * present in the cache.
495 * @param string $key The key associated with the value that will be replaced
496 * in the cache.
498 * @param mixed $value The new cache value.
500 * @param int $flag This parameter is present only for compatibility and is
501 * ignored.
503 * @param int $expire The delay before the item is removed from the cache. If
504 * $expire <= 2592000 then it is interpreted as the number
505 * of seconds from the time of the call to wait before
506 * removing the item from the cache. If $expire > 2592000
507 * then it is interpreted as the absolute Unix epoch time
508 * when the value will expire.
510 * @return bool true if the item was successfully replaced in the cache,
511 * false otherwise.
513 public function replace($key, $value, $flag = null, $expire = 0) {
514 // Sending of a key of 'null' or an unset value is a failure.
515 if (is_null($key)) {
516 return false;
519 try {
520 $set_results = MemcacheUtils::setMultiWithPolicy(array($key => $value),
521 $expire,
522 SetPolicy::REPLACE);
523 } catch (Error $e) {
524 return false;
526 return $set_results[0] == SetStatusCode::STORED;
530 * Sets the value of a key in the cache regardless of whether it is currently
531 * present or not.
533 * @param string $key The key associated with the value that will be replaced
534 * in the cache.
536 * @param mixed $value The new cache value.
538 * @param int $flag This parameter is present only for compatibility and is
539 * ignored.
541 * @param int $expire The delay before the item is removed from the cache. If
542 * $expire <= 2592000 then it is interpreted as the number
543 * of seconds from the time of the call to wait before
544 * removing the item from the cache. If $expire > 2592000
545 * then it is interpreted as the absolute Unix epoch time
546 * when the value will expire.
548 * @return bool true if the item was successfully replaced the cache, false
549 * otherwise.
551 public function set($key, $value, $flag = null, $expire = 0) {
552 // Sending of a key of 'null' or an unset value is a failure.
553 if (is_null($key)) {
554 return false;
557 try {
558 $set_results = MemcacheUtils::setMultiWithPolicy(array($key => $value),
559 $expire,
560 SetPolicy::SET);
561 } catch (Error $e) {
562 return false;
564 return $set_results[0] == SetStatusCode::STORED;
568 * This function is present only for compatibility and does nothing.
570 public function setCompressThreshold($threshold, $min_savings = 0.2) {
571 // Compression is not supported.
572 return false;
575 // setServerParams not implemented.