MediaWiki  master
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  $this->client->set_debug( true );
55  }
56 
57  protected function doGet( $key, $flags = 0, &$casToken = null ) {
58  $getToken = ( $casToken === self::PASS_BY_REF );
59  $casToken = null;
60 
61  $routeKey = $this->validateKeyAndPrependRoute( $key );
62 
63  // T257003: only require "gets" (instead of "get") when a CAS token is needed
64  return $getToken
65  ? $this->client->get( $routeKey, $casToken )
66  : $this->client->get( $routeKey );
67  }
68 
69  protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
70  $routeKey = $this->validateKeyAndPrependRoute( $key );
71 
72  return $this->client->set( $routeKey, $value, $this->fixExpiry( $exptime ) );
73  }
74 
75  protected function doDelete( $key, $flags = 0 ) {
76  $routeKey = $this->validateKeyAndPrependRoute( $key );
77 
78  return $this->client->delete( $routeKey );
79  }
80 
81  protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
82  $routeKey = $this->validateKeyAndPrependRoute( $key );
83 
84  return $this->client->add( $routeKey, $value, $this->fixExpiry( $exptime ) );
85  }
86 
87  protected function doCas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
88  $routeKey = $this->validateKeyAndPrependRoute( $key );
89 
90  return $this->client->cas( $casToken, $routeKey, $value, $this->fixExpiry( $exptime ) );
91  }
92 
93  public function incr( $key, $value = 1, $flags = 0 ) {
94  $routeKey = $this->validateKeyAndPrependRoute( $key );
95  $n = $this->client->incr( $routeKey, $value );
96 
97  return ( $n !== false && $n !== null ) ? $n : false;
98  }
99 
100  public function decr( $key, $value = 1, $flags = 0 ) {
101  $routeKey = $this->validateKeyAndPrependRoute( $key );
102  $n = $this->client->decr( $routeKey, $value );
103 
104  return ( $n !== false && $n !== null ) ? $n : false;
105  }
106 
107  protected function doChangeTTL( $key, $exptime, $flags ) {
108  $routeKey = $this->validateKeyAndPrependRoute( $key );
109 
110  return $this->client->touch( $routeKey, $this->fixExpiry( $exptime ) );
111  }
112 
113  protected function doGetMulti( array $keys, $flags = 0 ) {
114  $routeKeys = [];
115  foreach ( $keys as $key ) {
116  $routeKeys[] = $this->validateKeyAndPrependRoute( $key );
117  }
118 
119  $resByRouteKey = $this->client->get_multi( $routeKeys );
120 
121  $res = [];
122  foreach ( $resByRouteKey as $routeKey => $value ) {
123  $res[$this->stripRouteFromKey( $routeKey )] = $value;
124  }
125 
126  return $res;
127  }
128 
129  protected function serialize( $value ) {
130  return is_int( $value ) ? $value : $this->client->serialize( $value );
131  }
132 
133  protected function unserialize( $value ) {
134  return $this->isInteger( $value ) ? (int)$value : $this->client->unserialize( $value );
135  }
136 }
MemcachedPhpBagOStuff\serialize
serialize( $value)
Definition: MemcachedPhpBagOStuff.php:129
MediumSpecificBagOStuff\isInteger
isInteger( $value)
Check if a value is an integer.
Definition: MediumSpecificBagOStuff.php:893
MemcachedBagOStuff
Base class for memcached clients.
Definition: MemcachedBagOStuff.php:29
MemcachedPhpBagOStuff\doGet
doGet( $key, $flags=0, &$casToken=null)
Definition: MemcachedPhpBagOStuff.php:57
MemcachedBagOStuff\stripRouteFromKey
stripRouteFromKey( $key)
Definition: MemcachedBagOStuff.php:130
MemcachedPhpBagOStuff\doSet
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
Definition: MemcachedPhpBagOStuff.php:69
MemcachedPhpBagOStuff\decr
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
Definition: MemcachedPhpBagOStuff.php:100
MemcachedPhpBagOStuff\doDelete
doDelete( $key, $flags=0)
Delete an item.
Definition: MemcachedPhpBagOStuff.php:75
MemcachedPhpBagOStuff\__construct
__construct( $params)
Available parameters are:
Definition: MemcachedPhpBagOStuff.php:43
MemcachedPhpBagOStuff\doChangeTTL
doChangeTTL( $key, $exptime, $flags)
Definition: MemcachedPhpBagOStuff.php:107
MemcachedPhpBagOStuff\incr
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
Definition: MemcachedPhpBagOStuff.php:93
MemcachedPhpBagOStuff
A wrapper class for the pure-PHP memcached client, exposing a BagOStuff interface.
Definition: MemcachedPhpBagOStuff.php:29
$res
$res
Definition: testCompression.php:57
MemcachedClient
memcached client class implemented using (p)fsockopen()
Definition: MemcachedClient.php:79
MemcachedPhpBagOStuff\doCas
doCas( $casToken, $key, $value, $exptime=0, $flags=0)
Check and set an item.
Definition: MemcachedPhpBagOStuff.php:87
MemcachedPhpBagOStuff\$client
MemcachedClient $client
Definition: MemcachedPhpBagOStuff.php:31
MemcachedBagOStuff\fixExpiry
fixExpiry( $exptime)
Definition: MemcachedBagOStuff.php:147
MemcachedBagOStuff\validateKeyAndPrependRoute
validateKeyAndPrependRoute( $key)
Definition: MemcachedBagOStuff.php:112
MemcachedPhpBagOStuff\doAdd
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
Definition: MemcachedPhpBagOStuff.php:81
MemcachedPhpBagOStuff\doGetMulti
doGetMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
Definition: MemcachedPhpBagOStuff.php:113
$keys
$keys
Definition: testCompression.php:72
MemcachedPhpBagOStuff\unserialize
unserialize( $value)
Definition: MemcachedPhpBagOStuff.php:133