MediaWiki REL1_35
MemcachedPhpBagOStuff.php
Go to the documentation of this file.
1<?php
31 protected $client;
32
43 public function __construct( $params ) {
44 parent::__construct( $params );
45
46 // Default class-specific parameters
47 $params += [
48 'compress_threshold' => 1500,
49 'connect_timeout' => 0.5
50 ];
51
52 $this->client = new MemcachedClient( $params );
53 $this->client->set_servers( $params['servers'] );
54 }
55
56 public function setDebug( $enabled ) {
57 parent::debug( $enabled );
58 $this->client->set_debug( $enabled );
59 }
60
61 protected function doGet( $key, $flags = 0, &$casToken = null ) {
62 $casToken = null;
63
64 return $this->client->get( $this->validateKeyEncoding( $key ), $casToken );
65 }
66
67 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
68 return $this->client->set(
69 $this->validateKeyEncoding( $key ),
70 $value,
71 $this->fixExpiry( $exptime )
72 );
73 }
74
75 protected function doDelete( $key, $flags = 0 ) {
76 return $this->client->delete( $this->validateKeyEncoding( $key ) );
77 }
78
79 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
80 return $this->client->add(
81 $this->validateKeyEncoding( $key ),
82 $value,
83 $this->fixExpiry( $exptime )
84 );
85 }
86
87 protected function doCas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
88 return $this->client->cas(
89 $casToken,
90 $this->validateKeyEncoding( $key ),
91 $value,
92 $this->fixExpiry( $exptime )
93 );
94 }
95
96 public function incr( $key, $value = 1, $flags = 0 ) {
97 $n = $this->client->incr( $this->validateKeyEncoding( $key ), $value );
98
99 return ( $n !== false && $n !== null ) ? $n : false;
100 }
101
102 public function decr( $key, $value = 1, $flags = 0 ) {
103 $n = $this->client->decr( $this->validateKeyEncoding( $key ), $value );
104
105 return ( $n !== false && $n !== null ) ? $n : false;
106 }
107
108 protected function doChangeTTL( $key, $exptime, $flags ) {
109 return $this->client->touch(
110 $this->validateKeyEncoding( $key ),
111 $this->fixExpiry( $exptime )
112 );
113 }
114
115 protected function doGetMulti( array $keys, $flags = 0 ) {
116 foreach ( $keys as $key ) {
117 $this->validateKeyEncoding( $key );
118 }
119
120 return $this->client->get_multi( $keys );
121 }
122
123 protected function serialize( $value ) {
124 return is_int( $value ) ? $value : $this->client->serialize( $value );
125 }
126
127 protected function unserialize( $value ) {
128 return $this->isInteger( $value ) ? (int)$value : $this->client->unserialize( $value );
129 }
130}
isInteger( $value)
Check if a value is an integer.
Base class for memcached clients.
validateKeyEncoding( $key)
Ensure that a key is safe to use (contains no control characters and no characters above the ASCII ra...
memcached client class implemented using (p)fsockopen()
A wrapper class for the pure-PHP memcached client, exposing a BagOStuff interface.
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
doChangeTTL( $key, $exptime, $flags)
__construct( $params)
Available parameters are:
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
doCas( $casToken, $key, $value, $exptime=0, $flags=0)
Check and set an item.
doGet( $key, $flags=0, &$casToken=null)
doGetMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
doDelete( $key, $flags=0)
Delete an item.