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