MediaWiki REL1_40
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 'timeout' => 500000,
51 ];
52
53 $this->client = new MemcachedClient( $params );
54 $this->client->set_servers( $params['servers'] );
55 $this->client->set_debug( true );
56 }
57
58 protected function doGet( $key, $flags = 0, &$casToken = null ) {
59 $getToken = ( $casToken === self::PASS_BY_REF );
60 $casToken = null;
61
62 $routeKey = $this->validateKeyAndPrependRoute( $key );
63
64 // T257003: only require "gets" (instead of "get") when a CAS token is needed
65 $res = $getToken
66 // @phan-suppress-next-line PhanTypeMismatchArgument False positive
67 ? $this->client->get( $routeKey, $casToken )
68 : $this->client->get( $routeKey );
69
70 if ( $this->client->_last_cmd_status !== self::ERR_NONE ) {
71 $this->setLastError( $this->client->_last_cmd_status );
72 }
73
74 return $res;
75 }
76
77 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
78 $routeKey = $this->validateKeyAndPrependRoute( $key );
79
80 $res = $this->client->set( $routeKey, $value, $this->fixExpiry( $exptime ) );
81
82 if ( $this->client->_last_cmd_status !== self::ERR_NONE ) {
83 $this->setLastError( $this->client->_last_cmd_status );
84 }
85
86 return $res;
87 }
88
89 protected function doDelete( $key, $flags = 0 ) {
90 $routeKey = $this->validateKeyAndPrependRoute( $key );
91
92 $res = $this->client->delete( $routeKey );
93
94 if ( $this->client->_last_cmd_status !== self::ERR_NONE ) {
95 $this->setLastError( $this->client->_last_cmd_status );
96 }
97
98 return $res;
99 }
100
101 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
102 $routeKey = $this->validateKeyAndPrependRoute( $key );
103
104 $res = $this->client->add( $routeKey, $value, $this->fixExpiry( $exptime ) );
105
106 if ( $this->client->_last_cmd_status !== self::ERR_NONE ) {
107 $this->setLastError( $this->client->_last_cmd_status );
108 }
109
110 return $res;
111 }
112
113 protected function doCas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
114 $routeKey = $this->validateKeyAndPrependRoute( $key );
115
116 $res = $this->client->cas( $casToken, $routeKey, $value, $this->fixExpiry( $exptime ) );
117
118 if ( $this->client->_last_cmd_status !== self::ERR_NONE ) {
119 $this->setLastError( $this->client->_last_cmd_status );
120 }
121
122 return $res;
123 }
124
125 protected function doIncrWithInitAsync( $key, $exptime, $step, $init ) {
126 $routeKey = $this->validateKeyAndPrependRoute( $key );
127 $watchPoint = $this->watchErrors();
128 $this->client->add( $routeKey, $init - $step, $this->fixExpiry( $exptime ) );
129 $this->client->incr( $routeKey, $step );
130 return !$this->getLastError( $watchPoint );
131 }
132
133 protected function doIncrWithInitSync( $key, $exptime, $step, $init ) {
134 $routeKey = $this->validateKeyAndPrependRoute( $key );
135
136 $watchPoint = $this->watchErrors();
137 $newValue = $this->client->incr( $routeKey, $step ) ?? false;
138 if ( $newValue === false && !$this->getLastError( $watchPoint ) ) {
139 // No key set; initialize
140 $success = $this->client->add( $routeKey, $init, $this->fixExpiry( $exptime ) );
141 $newValue = $success ? $init : false;
142 if ( $newValue === false && !$this->getLastError( $watchPoint ) ) {
143 // Raced out initializing; increment
144 $newValue = $this->client->incr( $routeKey, $step ) ?? false;
145 }
146 }
147
148 return $newValue;
149 }
150
151 protected function doChangeTTL( $key, $exptime, $flags ) {
152 $routeKey = $this->validateKeyAndPrependRoute( $key );
153
154 $res = $this->client->touch( $routeKey, $this->fixExpiry( $exptime ) );
155
156 if ( $this->client->_last_cmd_status !== self::ERR_NONE ) {
157 $this->setLastError( $this->client->_last_cmd_status );
158 }
159
160 return $res;
161 }
162
163 protected function doGetMulti( array $keys, $flags = 0 ) {
164 $routeKeys = [];
165 foreach ( $keys as $key ) {
166 $routeKeys[] = $this->validateKeyAndPrependRoute( $key );
167 }
168
169 $resByRouteKey = $this->client->get_multi( $routeKeys );
170
171 $res = [];
172 foreach ( $resByRouteKey as $routeKey => $value ) {
173 $res[$this->stripRouteFromKey( $routeKey )] = $value;
174 }
175
176 if ( $this->client->_last_cmd_status !== self::ERR_NONE ) {
177 $this->setLastError( $this->client->_last_cmd_status );
178 }
179
180 return $res;
181 }
182
183 protected function serialize( $value ) {
184 return is_int( $value ) ? $value : $this->client->serialize( $value );
185 }
186
187 protected function unserialize( $value ) {
188 return $this->isInteger( $value ) ? (int)$value : $this->client->unserialize( $value );
189 }
190}
getLastError( $watchPoint=0)
Get the "last error" registry.
setLastError( $error)
Set the "last error" registry due to a problem encountered during an attempted operation.
watchErrors()
Get a "watch point" token that can be used to get the "last error" to occur after now.
isInteger( $value)
Check if a value is an integer.
Base class for memcached clients.
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:
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
doCas( $casToken, $key, $value, $exptime=0, $flags=0)
Set an item if the current CAS token matches the provided CAS token.
doGet( $key, $flags=0, &$casToken=null)
Get an item.
doIncrWithInitAsync( $key, $exptime, $step, $init)
doGetMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
doIncrWithInitSync( $key, $exptime, $step, $init)
doDelete( $key, $flags=0)
Delete an item.