3 * Object caching using memcached.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * A wrapper class for the pure-PHP memcached client, exposing a BagOStuff interface.
29 class MemcachedPhpBagOStuff
extends MemcachedBagOStuff
{
30 /** @var MemcachedClient */
34 * Available parameters are:
35 * - servers: The list of IP:port combinations holding the memcached servers.
36 * - persistent: Whether to use a persistent connection
37 * - compress_threshold: The minimum size an object must be before it is compressed
38 * - timeout: The read timeout in microseconds
39 * - connect_timeout: The connect timeout in seconds
41 * @param array $params
43 function __construct( $params ) {
44 parent
::__construct( $params );
46 // Default class-specific parameters
48 'compress_threshold' => 1500,
49 'connect_timeout' => 0.5
52 $this->client
= new MemcachedClient( $params );
53 $this->client
->set_servers( $params['servers'] );
56 public function setDebug( $enabled ) {
57 parent
::debug( $enabled );
58 $this->client
->set_debug( $enabled );
61 protected function doGet( $key, $flags = 0, &$casToken = null ) {
64 return $this->client
->get( $this->validateKeyEncoding( $key ), $casToken );
67 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
68 return $this->client
->set(
69 $this->validateKeyEncoding( $key ),
71 $this->fixExpiry( $exptime )
75 protected function doDelete( $key, $flags = 0 ) {
76 return $this->client
->delete( $this->validateKeyEncoding( $key ) );
79 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
80 return $this->client
->add(
81 $this->validateKeyEncoding( $key ),
83 $this->fixExpiry( $exptime )
87 protected function doCas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
88 return $this->client
->cas(
90 $this->validateKeyEncoding( $key ),
92 $this->fixExpiry( $exptime )
96 public function incr( $key, $value = 1, $flags = 0 ) {
97 $n = $this->client
->incr( $this->validateKeyEncoding( $key ), $value );
99 return ( $n !== false && $n !== null ) ?
$n : false;
102 public function decr( $key, $value = 1, $flags = 0 ) {
103 $n = $this->client
->decr( $this->validateKeyEncoding( $key ), $value );
105 return ( $n !== false && $n !== null ) ?
$n : false;
108 protected function doChangeTTL( $key, $exptime, $flags ) {
109 return $this->client
->touch(
110 $this->validateKeyEncoding( $key ),
111 $this->fixExpiry( $exptime )
115 protected function doGetMulti( array $keys, $flags = 0 ) {
116 foreach ( $keys as $key ) {
117 $this->validateKeyEncoding( $key );
120 return $this->client
->get_multi( $keys );
123 protected function serialize( $value ) {
124 return is_int( $value ) ?
$value : $this->client
->serialize( $value );
127 protected function unserialize( $value ) {
128 return $this->isInteger( $value ) ?
(int)$value : $this->client
->unserialize( $value );