pictcode / lib / Cake / Test / Case / Cache / Engine / MemcachedEngineTest.php @ f4745fee
履歴 | 表示 | アノテート | ダウンロード (20.845 KB)
| 1 |
<?php
|
|---|---|
| 2 |
/**
|
| 3 |
* MemcachedEngineTest file
|
| 4 |
*
|
| 5 |
* PHP 5
|
| 6 |
*
|
| 7 |
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
| 8 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
| 9 |
*
|
| 10 |
* Licensed under The MIT License
|
| 11 |
* For full copyright and license information, please see the LICENSE.txt
|
| 12 |
* Redistributions of files must retain the above copyright notice
|
| 13 |
*
|
| 14 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
| 15 |
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
| 16 |
* @package Cake.Test.Case.Cache.Engine
|
| 17 |
* @since CakePHP(tm) v 2.5.0
|
| 18 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
| 19 |
*/
|
| 20 |
|
| 21 |
App::uses('Cache', 'Cache'); |
| 22 |
App::uses('MemcachedEngine', 'Cache/Engine'); |
| 23 |
|
| 24 |
/**
|
| 25 |
* Class TestMemcachedEngine
|
| 26 |
*
|
| 27 |
* @package Cake.Test.Case.Cache.Engine
|
| 28 |
*/
|
| 29 |
class TestMemcachedEngine extends MemcachedEngine { |
| 30 |
|
| 31 |
/**
|
| 32 |
* public accessor to _parseServerString
|
| 33 |
*
|
| 34 |
* @param string $server
|
| 35 |
* @return array
|
| 36 |
*/
|
| 37 |
public function parseServerString($server) { |
| 38 |
return $this->_parseServerString($server); |
| 39 |
} |
| 40 |
|
| 41 |
public function setMemcached($memcached) { |
| 42 |
$this->_Memcached = $memcached; |
| 43 |
} |
| 44 |
|
| 45 |
public function getMemcached() { |
| 46 |
return $this->_Memcached; |
| 47 |
} |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
/**
|
| 52 |
* MemcachedEngineTest class
|
| 53 |
*
|
| 54 |
* @package Cake.Test.Case.Cache.Engine
|
| 55 |
*/
|
| 56 |
class MemcachedEngineTest extends CakeTestCase { |
| 57 |
|
| 58 |
/**
|
| 59 |
* setUp method
|
| 60 |
*
|
| 61 |
* @return void
|
| 62 |
*/
|
| 63 |
public function setUp() { |
| 64 |
parent::setUp();
|
| 65 |
$this->skipIf(!class_exists('Memcached'), 'Memcached is not installed or configured properly.'); |
| 66 |
|
| 67 |
Cache::config('memcached', array( |
| 68 |
'engine' => 'Memcached', |
| 69 |
'prefix' => 'cake_', |
| 70 |
'duration' => 3600 |
| 71 |
)); |
| 72 |
} |
| 73 |
|
| 74 |
/**
|
| 75 |
* tearDown method
|
| 76 |
*
|
| 77 |
* @return void
|
| 78 |
*/
|
| 79 |
public function tearDown() { |
| 80 |
parent::tearDown();
|
| 81 |
Cache::drop('memcached'); |
| 82 |
Cache::drop('memcached_groups'); |
| 83 |
Cache::drop('memcached_helper'); |
| 84 |
Cache::config('default'); |
| 85 |
} |
| 86 |
|
| 87 |
/**
|
| 88 |
* testSettings method
|
| 89 |
*
|
| 90 |
* @return void
|
| 91 |
*/
|
| 92 |
public function testSettings() { |
| 93 |
$settings = Cache::settings('memcached'); |
| 94 |
unset($settings['path']); |
| 95 |
$expecting = array( |
| 96 |
'prefix' => 'cake_', |
| 97 |
'duration' => 3600, |
| 98 |
'probability' => 100, |
| 99 |
'servers' => array('127.0.0.1'), |
| 100 |
'persistent' => false, |
| 101 |
'compress' => false, |
| 102 |
'engine' => 'Memcached', |
| 103 |
'login' => null, |
| 104 |
'password' => null, |
| 105 |
'groups' => array(), |
| 106 |
'serialize' => 'php', |
| 107 |
'options' => array() |
| 108 |
); |
| 109 |
$this->assertEquals($expecting, $settings); |
| 110 |
} |
| 111 |
|
| 112 |
/**
|
| 113 |
* testCompressionSetting method
|
| 114 |
*
|
| 115 |
* @return void
|
| 116 |
*/
|
| 117 |
public function testCompressionSetting() { |
| 118 |
$Memcached = new TestMemcachedEngine(); |
| 119 |
$Memcached->init(array( |
| 120 |
'engine' => 'Memcached', |
| 121 |
'servers' => array('127.0.0.1:11211'), |
| 122 |
'compress' => false |
| 123 |
)); |
| 124 |
|
| 125 |
$this->assertFalse($Memcached->getMemcached()->getOption(Memcached::OPT_COMPRESSION)); |
| 126 |
|
| 127 |
$MemcachedCompressed = new TestMemcachedEngine(); |
| 128 |
$MemcachedCompressed->init(array( |
| 129 |
'engine' => 'Memcached', |
| 130 |
'servers' => array('127.0.0.1:11211'), |
| 131 |
'compress' => true |
| 132 |
)); |
| 133 |
|
| 134 |
$this->assertTrue($MemcachedCompressed->getMemcached()->getOption(Memcached::OPT_COMPRESSION)); |
| 135 |
} |
| 136 |
|
| 137 |
/**
|
| 138 |
* test setting options
|
| 139 |
*
|
| 140 |
* @return void
|
| 141 |
*/
|
| 142 |
public function testOptionsSetting() { |
| 143 |
$memcached = new TestMemcachedEngine(); |
| 144 |
$memcached->init(array( |
| 145 |
'engine' => 'Memcached', |
| 146 |
'servers' => array('127.0.0.1:11211'), |
| 147 |
'options' => array( |
| 148 |
Memcached::OPT_BINARY_PROTOCOL => true |
| 149 |
) |
| 150 |
)); |
| 151 |
$this->assertEquals(1, $memcached->getMemcached()->getOption(Memcached::OPT_BINARY_PROTOCOL)); |
| 152 |
} |
| 153 |
|
| 154 |
/**
|
| 155 |
* test accepts only valid serializer engine
|
| 156 |
*
|
| 157 |
* @return void
|
| 158 |
*/
|
| 159 |
public function testInvalidSerializerSetting() { |
| 160 |
$Memcached = new TestMemcachedEngine(); |
| 161 |
$settings = array( |
| 162 |
'engine' => 'Memcached', |
| 163 |
'servers' => array('127.0.0.1:11211'), |
| 164 |
'persistent' => false, |
| 165 |
'serialize' => 'invalid_serializer' |
| 166 |
); |
| 167 |
|
| 168 |
$this->setExpectedException(
|
| 169 |
'CacheException', 'invalid_serializer is not a valid serializer engine for Memcached' |
| 170 |
); |
| 171 |
$Memcached->init($settings); |
| 172 |
} |
| 173 |
|
| 174 |
/**
|
| 175 |
* testPhpSerializerSetting method
|
| 176 |
*
|
| 177 |
* @return void
|
| 178 |
*/
|
| 179 |
public function testPhpSerializerSetting() { |
| 180 |
$Memcached = new TestMemcachedEngine(); |
| 181 |
$settings = array( |
| 182 |
'engine' => 'Memcached', |
| 183 |
'servers' => array('127.0.0.1:11211'), |
| 184 |
'persistent' => false, |
| 185 |
'serialize' => 'php' |
| 186 |
); |
| 187 |
|
| 188 |
$Memcached->init($settings); |
| 189 |
$this->assertEquals(Memcached::SERIALIZER_PHP, $Memcached->getMemcached()->getOption(Memcached::OPT_SERIALIZER)); |
| 190 |
} |
| 191 |
|
| 192 |
/**
|
| 193 |
* testJsonSerializerSetting method
|
| 194 |
*
|
| 195 |
* @return void
|
| 196 |
*/
|
| 197 |
public function testJsonSerializerSetting() { |
| 198 |
$this->skipIf(
|
| 199 |
!Memcached::HAVE_JSON, |
| 200 |
'Memcached extension is not compiled with json support'
|
| 201 |
); |
| 202 |
|
| 203 |
$Memcached = new TestMemcachedEngine(); |
| 204 |
$settings = array( |
| 205 |
'engine' => 'Memcached', |
| 206 |
'servers' => array('127.0.0.1:11211'), |
| 207 |
'persistent' => false, |
| 208 |
'serialize' => 'json' |
| 209 |
); |
| 210 |
|
| 211 |
$Memcached->init($settings); |
| 212 |
$this->assertEquals(Memcached::SERIALIZER_JSON, $Memcached->getMemcached()->getOption(Memcached::OPT_SERIALIZER)); |
| 213 |
} |
| 214 |
|
| 215 |
/**
|
| 216 |
* testIgbinarySerializerSetting method
|
| 217 |
*
|
| 218 |
* @return void
|
| 219 |
*/
|
| 220 |
public function testIgbinarySerializerSetting() { |
| 221 |
$this->skipIf(
|
| 222 |
!Memcached::HAVE_IGBINARY, |
| 223 |
'Memcached extension is not compiled with igbinary support'
|
| 224 |
); |
| 225 |
|
| 226 |
$Memcached = new TestMemcachedEngine(); |
| 227 |
$settings = array( |
| 228 |
'engine' => 'Memcached', |
| 229 |
'servers' => array('127.0.0.1:11211'), |
| 230 |
'persistent' => false, |
| 231 |
'serialize' => 'igbinary' |
| 232 |
); |
| 233 |
|
| 234 |
$Memcached->init($settings); |
| 235 |
$this->assertEquals(Memcached::SERIALIZER_IGBINARY, $Memcached->getMemcached()->getOption(Memcached::OPT_SERIALIZER)); |
| 236 |
} |
| 237 |
|
| 238 |
/**
|
| 239 |
* testMsgpackSerializerSetting method
|
| 240 |
*
|
| 241 |
* @return void
|
| 242 |
*/
|
| 243 |
public function testMsgpackSerializerSetting() { |
| 244 |
$this->skipIf(
|
| 245 |
!defined('Memcached::HAVE_MSGPACK') || !Memcached::HAVE_MSGPACK, |
| 246 |
'Memcached extension is not compiled with msgpack support'
|
| 247 |
); |
| 248 |
|
| 249 |
$Memcached = new TestMemcachedEngine(); |
| 250 |
$settings = array( |
| 251 |
'engine' => 'Memcached', |
| 252 |
'servers' => array('127.0.0.1:11211'), |
| 253 |
'persistent' => false, |
| 254 |
'serialize' => 'msgpack' |
| 255 |
); |
| 256 |
|
| 257 |
$Memcached->init($settings); |
| 258 |
$this->assertEquals(Memcached::SERIALIZER_MSGPACK, $Memcached->getMemcached()->getOption(Memcached::OPT_SERIALIZER)); |
| 259 |
} |
| 260 |
|
| 261 |
/**
|
| 262 |
* testJsonSerializerThrowException method
|
| 263 |
*
|
| 264 |
* @return void
|
| 265 |
*/
|
| 266 |
public function testJsonSerializerThrowException() { |
| 267 |
$this->skipIf(
|
| 268 |
Memcached::HAVE_JSON, |
| 269 |
'Memcached extension is compiled with json support'
|
| 270 |
); |
| 271 |
|
| 272 |
$Memcached = new TestMemcachedEngine(); |
| 273 |
$settings = array( |
| 274 |
'engine' => 'Memcached', |
| 275 |
'servers' => array('127.0.0.1:11211'), |
| 276 |
'persistent' => false, |
| 277 |
'serialize' => 'json' |
| 278 |
); |
| 279 |
|
| 280 |
$this->setExpectedException(
|
| 281 |
'CacheException', 'Memcached extension is not compiled with json support' |
| 282 |
); |
| 283 |
$Memcached->init($settings); |
| 284 |
} |
| 285 |
|
| 286 |
/**
|
| 287 |
* testMsgpackSerializerThrowException method
|
| 288 |
*
|
| 289 |
* @return void
|
| 290 |
*/
|
| 291 |
public function testMsgpackSerializerThrowException() { |
| 292 |
$this->skipIf(
|
| 293 |
defined('Memcached::HAVE_MSGPACK') && Memcached::HAVE_MSGPACK, |
| 294 |
'Memcached extension is compiled with msgpack support'
|
| 295 |
); |
| 296 |
|
| 297 |
$Memcached = new TestMemcachedEngine(); |
| 298 |
$settings = array( |
| 299 |
'engine' => 'Memcached', |
| 300 |
'servers' => array('127.0.0.1:11211'), |
| 301 |
'persistent' => false, |
| 302 |
'serialize' => 'msgpack' |
| 303 |
); |
| 304 |
|
| 305 |
$this->setExpectedException(
|
| 306 |
'CacheException', 'msgpack is not a valid serializer engine for Memcached' |
| 307 |
); |
| 308 |
$Memcached->init($settings); |
| 309 |
} |
| 310 |
|
| 311 |
/**
|
| 312 |
* testIgbinarySerializerThrowException method
|
| 313 |
*
|
| 314 |
* @return void
|
| 315 |
*/
|
| 316 |
public function testIgbinarySerializerThrowException() { |
| 317 |
$this->skipIf(
|
| 318 |
Memcached::HAVE_IGBINARY, |
| 319 |
'Memcached extension is compiled with igbinary support'
|
| 320 |
); |
| 321 |
|
| 322 |
$Memcached = new TestMemcachedEngine(); |
| 323 |
$settings = array( |
| 324 |
'engine' => 'Memcached', |
| 325 |
'servers' => array('127.0.0.1:11211'), |
| 326 |
'persistent' => false, |
| 327 |
'serialize' => 'igbinary' |
| 328 |
); |
| 329 |
|
| 330 |
$this->setExpectedException(
|
| 331 |
'CacheException', 'Memcached extension is not compiled with igbinary support' |
| 332 |
); |
| 333 |
$Memcached->init($settings); |
| 334 |
} |
| 335 |
|
| 336 |
/**
|
| 337 |
* test using authentication without memcached installed with SASL support
|
| 338 |
* throw an exception
|
| 339 |
*
|
| 340 |
* @return void
|
| 341 |
*/
|
| 342 |
public function testSaslAuthException() { |
| 343 |
$Memcached = new TestMemcachedEngine(); |
| 344 |
$settings = array( |
| 345 |
'engine' => 'Memcached', |
| 346 |
'servers' => array('127.0.0.1:11211'), |
| 347 |
'persistent' => false, |
| 348 |
'login' => 'test', |
| 349 |
'password' => 'password' |
| 350 |
); |
| 351 |
|
| 352 |
$this->setExpectedException('PHPUnit_Framework_Error_Warning'); |
| 353 |
$Memcached->init($settings); |
| 354 |
} |
| 355 |
|
| 356 |
/**
|
| 357 |
* testSettings method
|
| 358 |
*
|
| 359 |
* @return void
|
| 360 |
*/
|
| 361 |
public function testMultipleServers() { |
| 362 |
$servers = array('127.0.0.1:11211', '127.0.0.1:11222'); |
| 363 |
$available = true; |
| 364 |
$Memcached = new Memcached(); |
| 365 |
|
| 366 |
foreach ($servers as $server) { |
| 367 |
list($host, $port) = explode(':', $server); |
| 368 |
//@codingStandardsIgnoreStart
|
| 369 |
if (!$Memcached->addServer($host, $port)) { |
| 370 |
$available = false; |
| 371 |
} |
| 372 |
//@codingStandardsIgnoreEnd
|
| 373 |
} |
| 374 |
|
| 375 |
$this->skipIf(!$available, 'Need memcached servers at ' . implode(', ', $servers) . ' to run this test.'); |
| 376 |
|
| 377 |
$Memcached = new MemcachedEngine(); |
| 378 |
$Memcached->init(array('engine' => 'Memcached', 'servers' => $servers)); |
| 379 |
|
| 380 |
$settings = $Memcached->settings(); |
| 381 |
$this->assertEquals($settings['servers'], $servers); |
| 382 |
Cache::drop('dual_server'); |
| 383 |
} |
| 384 |
|
| 385 |
/**
|
| 386 |
* test connecting to an ipv6 server.
|
| 387 |
*
|
| 388 |
* @return void
|
| 389 |
*/
|
| 390 |
public function testConnectIpv6() { |
| 391 |
$Memcached = new MemcachedEngine(); |
| 392 |
$result = $Memcached->init(array( |
| 393 |
'prefix' => 'cake_', |
| 394 |
'duration' => 200, |
| 395 |
'engine' => 'Memcached', |
| 396 |
'servers' => array( |
| 397 |
'[::1]:11211'
|
| 398 |
) |
| 399 |
)); |
| 400 |
$this->assertTrue($result); |
| 401 |
} |
| 402 |
|
| 403 |
/**
|
| 404 |
* test domain starts with u
|
| 405 |
*
|
| 406 |
* @return void
|
| 407 |
*/
|
| 408 |
public function testParseServerStringWithU() { |
| 409 |
$Memcached = new TestMemcachedEngine(); |
| 410 |
$result = $Memcached->parseServerString('udomain.net:13211'); |
| 411 |
$this->assertEquals(array('udomain.net', '13211'), $result); |
| 412 |
} |
| 413 |
|
| 414 |
/**
|
| 415 |
* test non latin domains.
|
| 416 |
*
|
| 417 |
* @return void
|
| 418 |
*/
|
| 419 |
public function testParseServerStringNonLatin() { |
| 420 |
$Memcached = new TestMemcachedEngine(); |
| 421 |
$result = $Memcached->parseServerString('schülervz.net:13211'); |
| 422 |
$this->assertEquals(array('schülervz.net', '13211'), $result); |
| 423 |
|
| 424 |
$result = $Memcached->parseServerString('sülül:1111'); |
| 425 |
$this->assertEquals(array('sülül', '1111'), $result); |
| 426 |
} |
| 427 |
|
| 428 |
/**
|
| 429 |
* test unix sockets.
|
| 430 |
*
|
| 431 |
* @return void
|
| 432 |
*/
|
| 433 |
public function testParseServerStringUnix() { |
| 434 |
$Memcached = new TestMemcachedEngine(); |
| 435 |
$result = $Memcached->parseServerString('unix:///path/to/memcachedd.sock'); |
| 436 |
$this->assertEquals(array('/path/to/memcachedd.sock', 0), $result); |
| 437 |
} |
| 438 |
|
| 439 |
/**
|
| 440 |
* testReadAndWriteCache method
|
| 441 |
*
|
| 442 |
* @return void
|
| 443 |
*/
|
| 444 |
public function testReadAndWriteCache() { |
| 445 |
Cache::set(array('duration' => 1), null, 'memcached'); |
| 446 |
|
| 447 |
$result = Cache::read('test', 'memcached'); |
| 448 |
$expecting = ''; |
| 449 |
$this->assertEquals($expecting, $result); |
| 450 |
|
| 451 |
$data = 'this is a test of the emergency broadcasting system'; |
| 452 |
$result = Cache::write('test', $data, 'memcached'); |
| 453 |
$this->assertTrue($result); |
| 454 |
|
| 455 |
$result = Cache::read('test', 'memcached'); |
| 456 |
$expecting = $data; |
| 457 |
$this->assertEquals($expecting, $result); |
| 458 |
|
| 459 |
Cache::delete('test', 'memcached'); |
| 460 |
} |
| 461 |
|
| 462 |
/**
|
| 463 |
* testExpiry method
|
| 464 |
*
|
| 465 |
* @return void
|
| 466 |
*/
|
| 467 |
public function testExpiry() { |
| 468 |
Cache::set(array('duration' => 1), 'memcached'); |
| 469 |
|
| 470 |
$result = Cache::read('test', 'memcached'); |
| 471 |
$this->assertFalse($result); |
| 472 |
|
| 473 |
$data = 'this is a test of the emergency broadcasting system'; |
| 474 |
$result = Cache::write('other_test', $data, 'memcached'); |
| 475 |
$this->assertTrue($result); |
| 476 |
|
| 477 |
sleep(2); |
| 478 |
$result = Cache::read('other_test', 'memcached'); |
| 479 |
$this->assertFalse($result); |
| 480 |
|
| 481 |
Cache::set(array('duration' => "+1 second"), 'memcached'); |
| 482 |
|
| 483 |
$data = 'this is a test of the emergency broadcasting system'; |
| 484 |
$result = Cache::write('other_test', $data, 'memcached'); |
| 485 |
$this->assertTrue($result); |
| 486 |
|
| 487 |
sleep(3); |
| 488 |
$result = Cache::read('other_test', 'memcached'); |
| 489 |
$this->assertFalse($result); |
| 490 |
|
| 491 |
Cache::config('memcached', array('duration' => '+1 second')); |
| 492 |
|
| 493 |
$result = Cache::read('other_test', 'memcached'); |
| 494 |
$this->assertFalse($result); |
| 495 |
|
| 496 |
Cache::config('memcached', array('duration' => '+29 days')); |
| 497 |
$data = 'this is a test of the emergency broadcasting system'; |
| 498 |
$result = Cache::write('long_expiry_test', $data, 'memcached'); |
| 499 |
$this->assertTrue($result); |
| 500 |
|
| 501 |
sleep(2); |
| 502 |
$result = Cache::read('long_expiry_test', 'memcached'); |
| 503 |
$expecting = $data; |
| 504 |
$this->assertEquals($expecting, $result); |
| 505 |
|
| 506 |
Cache::config('memcached', array('duration' => 3600)); |
| 507 |
} |
| 508 |
|
| 509 |
/**
|
| 510 |
* testDeleteCache method
|
| 511 |
*
|
| 512 |
* @return void
|
| 513 |
*/
|
| 514 |
public function testDeleteCache() { |
| 515 |
$data = 'this is a test of the emergency broadcasting system'; |
| 516 |
$result = Cache::write('delete_test', $data, 'memcached'); |
| 517 |
$this->assertTrue($result); |
| 518 |
|
| 519 |
$result = Cache::delete('delete_test', 'memcached'); |
| 520 |
$this->assertTrue($result); |
| 521 |
} |
| 522 |
|
| 523 |
/**
|
| 524 |
* testDecrement method
|
| 525 |
*
|
| 526 |
* @return void
|
| 527 |
*/
|
| 528 |
public function testDecrement() { |
| 529 |
$result = Cache::write('test_decrement', 5, 'memcached'); |
| 530 |
$this->assertTrue($result); |
| 531 |
|
| 532 |
$result = Cache::decrement('test_decrement', 1, 'memcached'); |
| 533 |
$this->assertEquals(4, $result); |
| 534 |
|
| 535 |
$result = Cache::read('test_decrement', 'memcached'); |
| 536 |
$this->assertEquals(4, $result); |
| 537 |
|
| 538 |
$result = Cache::decrement('test_decrement', 2, 'memcached'); |
| 539 |
$this->assertEquals(2, $result); |
| 540 |
|
| 541 |
$result = Cache::read('test_decrement', 'memcached'); |
| 542 |
$this->assertEquals(2, $result); |
| 543 |
|
| 544 |
Cache::delete('test_decrement', 'memcached'); |
| 545 |
} |
| 546 |
|
| 547 |
/**
|
| 548 |
* test decrementing compressed keys
|
| 549 |
*
|
| 550 |
* @return void
|
| 551 |
*/
|
| 552 |
public function testDecrementCompressedKeys() { |
| 553 |
Cache::config('compressed_memcached', array( |
| 554 |
'engine' => 'Memcached', |
| 555 |
'duration' => '+2 seconds', |
| 556 |
'servers' => array('127.0.0.1:11211'), |
| 557 |
'compress' => true |
| 558 |
)); |
| 559 |
|
| 560 |
$result = Cache::write('test_decrement', 5, 'compressed_memcached'); |
| 561 |
$this->assertTrue($result); |
| 562 |
|
| 563 |
$result = Cache::decrement('test_decrement', 1, 'compressed_memcached'); |
| 564 |
$this->assertEquals(4, $result); |
| 565 |
|
| 566 |
$result = Cache::read('test_decrement', 'compressed_memcached'); |
| 567 |
$this->assertEquals(4, $result); |
| 568 |
|
| 569 |
$result = Cache::decrement('test_decrement', 2, 'compressed_memcached'); |
| 570 |
$this->assertEquals(2, $result); |
| 571 |
|
| 572 |
$result = Cache::read('test_decrement', 'compressed_memcached'); |
| 573 |
$this->assertEquals(2, $result); |
| 574 |
|
| 575 |
Cache::delete('test_decrement', 'compressed_memcached'); |
| 576 |
} |
| 577 |
|
| 578 |
/**
|
| 579 |
* testIncrement method
|
| 580 |
*
|
| 581 |
* @return void
|
| 582 |
*/
|
| 583 |
public function testIncrement() { |
| 584 |
$result = Cache::write('test_increment', 5, 'memcached'); |
| 585 |
$this->assertTrue($result); |
| 586 |
|
| 587 |
$result = Cache::increment('test_increment', 1, 'memcached'); |
| 588 |
$this->assertEquals(6, $result); |
| 589 |
|
| 590 |
$result = Cache::read('test_increment', 'memcached'); |
| 591 |
$this->assertEquals(6, $result); |
| 592 |
|
| 593 |
$result = Cache::increment('test_increment', 2, 'memcached'); |
| 594 |
$this->assertEquals(8, $result); |
| 595 |
|
| 596 |
$result = Cache::read('test_increment', 'memcached'); |
| 597 |
$this->assertEquals(8, $result); |
| 598 |
|
| 599 |
Cache::delete('test_increment', 'memcached'); |
| 600 |
} |
| 601 |
|
| 602 |
/**
|
| 603 |
* test incrementing compressed keys
|
| 604 |
*
|
| 605 |
* @return void
|
| 606 |
*/
|
| 607 |
public function testIncrementCompressedKeys() { |
| 608 |
Cache::config('compressed_memcached', array( |
| 609 |
'engine' => 'Memcached', |
| 610 |
'duration' => '+2 seconds', |
| 611 |
'servers' => array('127.0.0.1:11211'), |
| 612 |
'compress' => true |
| 613 |
)); |
| 614 |
|
| 615 |
$result = Cache::write('test_increment', 5, 'compressed_memcached'); |
| 616 |
$this->assertTrue($result); |
| 617 |
|
| 618 |
$result = Cache::increment('test_increment', 1, 'compressed_memcached'); |
| 619 |
$this->assertEquals(6, $result); |
| 620 |
|
| 621 |
$result = Cache::read('test_increment', 'compressed_memcached'); |
| 622 |
$this->assertEquals(6, $result); |
| 623 |
|
| 624 |
$result = Cache::increment('test_increment', 2, 'compressed_memcached'); |
| 625 |
$this->assertEquals(8, $result); |
| 626 |
|
| 627 |
$result = Cache::read('test_increment', 'compressed_memcached'); |
| 628 |
$this->assertEquals(8, $result); |
| 629 |
|
| 630 |
Cache::delete('test_increment', 'compressed_memcached'); |
| 631 |
} |
| 632 |
|
| 633 |
/**
|
| 634 |
* test that configurations don't conflict, when a file engine is declared after a memcached one.
|
| 635 |
*
|
| 636 |
* @return void
|
| 637 |
*/
|
| 638 |
public function testConfigurationConflict() { |
| 639 |
Cache::config('long_memcached', array( |
| 640 |
'engine' => 'Memcached', |
| 641 |
'duration' => '+2 seconds', |
| 642 |
'servers' => array('127.0.0.1:11211'), |
| 643 |
)); |
| 644 |
Cache::config('short_memcached', array( |
| 645 |
'engine' => 'Memcached', |
| 646 |
'duration' => '+1 seconds', |
| 647 |
'servers' => array('127.0.0.1:11211'), |
| 648 |
)); |
| 649 |
Cache::config('some_file', array('engine' => 'File')); |
| 650 |
|
| 651 |
$this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcached')); |
| 652 |
$this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcached')); |
| 653 |
|
| 654 |
$this->assertEquals('yay', Cache::read('duration_test', 'long_memcached'), 'Value was not read %s'); |
| 655 |
$this->assertEquals('boo', Cache::read('short_duration_test', 'short_memcached'), 'Value was not read %s'); |
| 656 |
|
| 657 |
sleep(1); |
| 658 |
$this->assertEquals('yay', Cache::read('duration_test', 'long_memcached'), 'Value was not read %s'); |
| 659 |
|
| 660 |
sleep(2); |
| 661 |
$this->assertFalse(Cache::read('short_duration_test', 'short_memcached'), 'Cache was not invalidated %s'); |
| 662 |
$this->assertFalse(Cache::read('duration_test', 'long_memcached'), 'Value did not expire %s'); |
| 663 |
|
| 664 |
Cache::delete('duration_test', 'long_memcached'); |
| 665 |
Cache::delete('short_duration_test', 'short_memcached'); |
| 666 |
} |
| 667 |
|
| 668 |
/**
|
| 669 |
* test clearing memcached.
|
| 670 |
*
|
| 671 |
* @return void
|
| 672 |
*/
|
| 673 |
public function testClear() { |
| 674 |
Cache::config('memcached2', array( |
| 675 |
'engine' => 'Memcached', |
| 676 |
'prefix' => 'cake2_', |
| 677 |
'duration' => 3600 |
| 678 |
)); |
| 679 |
|
| 680 |
Cache::write('some_value', 'cache1', 'memcached'); |
| 681 |
$result = Cache::clear(true, 'memcached'); |
| 682 |
$this->assertTrue($result); |
| 683 |
$this->assertEquals('cache1', Cache::read('some_value', 'memcached')); |
| 684 |
|
| 685 |
Cache::write('some_value', 'cache2', 'memcached2'); |
| 686 |
$result = Cache::clear(false, 'memcached'); |
| 687 |
$this->assertTrue($result); |
| 688 |
$this->assertFalse(Cache::read('some_value', 'memcached')); |
| 689 |
$this->assertEquals('cache2', Cache::read('some_value', 'memcached2')); |
| 690 |
|
| 691 |
Cache::clear(false, 'memcached2'); |
| 692 |
} |
| 693 |
|
| 694 |
/**
|
| 695 |
* test that a 0 duration can successfully write.
|
| 696 |
*
|
| 697 |
* @return void
|
| 698 |
*/
|
| 699 |
public function testZeroDuration() { |
| 700 |
Cache::config('memcached', array('duration' => 0)); |
| 701 |
$result = Cache::write('test_key', 'written!', 'memcached'); |
| 702 |
|
| 703 |
$this->assertTrue($result); |
| 704 |
$result = Cache::read('test_key', 'memcached'); |
| 705 |
$this->assertEquals('written!', $result); |
| 706 |
} |
| 707 |
|
| 708 |
/**
|
| 709 |
* test that durations greater than 30 days never expire
|
| 710 |
*
|
| 711 |
* @return void
|
| 712 |
*/
|
| 713 |
public function testLongDurationEqualToZero() { |
| 714 |
$this->markTestSkipped('Cannot run as Memcached cannot be reflected'); |
| 715 |
|
| 716 |
$memcached = new TestMemcachedEngine(); |
| 717 |
$memcached->settings['compress'] = false; |
| 718 |
|
| 719 |
$mock = $this->getMock('Memcached'); |
| 720 |
$memcached->setMemcached($mock); |
| 721 |
$mock->expects($this->once()) |
| 722 |
->method('set')
|
| 723 |
->with('key', 'value', 0); |
| 724 |
|
| 725 |
$value = 'value'; |
| 726 |
$memcached->write('key', $value, 50 * DAY); |
| 727 |
} |
| 728 |
|
| 729 |
/**
|
| 730 |
* Tests that configuring groups for stored keys return the correct values when read/written
|
| 731 |
* Shows that altering the group value is equivalent to deleting all keys under the same
|
| 732 |
* group
|
| 733 |
*
|
| 734 |
* @return void
|
| 735 |
*/
|
| 736 |
public function testGroupReadWrite() { |
| 737 |
Cache::config('memcached_groups', array( |
| 738 |
'engine' => 'Memcached', |
| 739 |
'duration' => 3600, |
| 740 |
'groups' => array('group_a', 'group_b'), |
| 741 |
'prefix' => 'test_' |
| 742 |
)); |
| 743 |
Cache::config('memcached_helper', array( |
| 744 |
'engine' => 'Memcached', |
| 745 |
'duration' => 3600, |
| 746 |
'prefix' => 'test_' |
| 747 |
)); |
| 748 |
$this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups')); |
| 749 |
$this->assertEquals('value', Cache::read('test_groups', 'memcached_groups')); |
| 750 |
|
| 751 |
Cache::increment('group_a', 1, 'memcached_helper'); |
| 752 |
$this->assertFalse(Cache::read('test_groups', 'memcached_groups')); |
| 753 |
$this->assertTrue(Cache::write('test_groups', 'value2', 'memcached_groups')); |
| 754 |
$this->assertEquals('value2', Cache::read('test_groups', 'memcached_groups')); |
| 755 |
|
| 756 |
Cache::increment('group_b', 1, 'memcached_helper'); |
| 757 |
$this->assertFalse(Cache::read('test_groups', 'memcached_groups')); |
| 758 |
$this->assertTrue(Cache::write('test_groups', 'value3', 'memcached_groups')); |
| 759 |
$this->assertEquals('value3', Cache::read('test_groups', 'memcached_groups')); |
| 760 |
} |
| 761 |
|
| 762 |
/**
|
| 763 |
* Tests that deleteing from a groups-enabled config is possible
|
| 764 |
*
|
| 765 |
* @return void
|
| 766 |
*/
|
| 767 |
public function testGroupDelete() { |
| 768 |
Cache::config('memcached_groups', array( |
| 769 |
'engine' => 'Memcached', |
| 770 |
'duration' => 3600, |
| 771 |
'groups' => array('group_a', 'group_b') |
| 772 |
)); |
| 773 |
$this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups')); |
| 774 |
$this->assertEquals('value', Cache::read('test_groups', 'memcached_groups')); |
| 775 |
$this->assertTrue(Cache::delete('test_groups', 'memcached_groups')); |
| 776 |
|
| 777 |
$this->assertFalse(Cache::read('test_groups', 'memcached_groups')); |
| 778 |
} |
| 779 |
|
| 780 |
/**
|
| 781 |
* Test clearing a cache group
|
| 782 |
*
|
| 783 |
* @return void
|
| 784 |
*/
|
| 785 |
public function testGroupClear() { |
| 786 |
Cache::config('memcached_groups', array( |
| 787 |
'engine' => 'Memcached', |
| 788 |
'duration' => 3600, |
| 789 |
'groups' => array('group_a', 'group_b') |
| 790 |
)); |
| 791 |
|
| 792 |
$this->assertTrue(Cache::write('test_groups', 'value', 'memcached_groups')); |
| 793 |
$this->assertTrue(Cache::clearGroup('group_a', 'memcached_groups')); |
| 794 |
$this->assertFalse(Cache::read('test_groups', 'memcached_groups')); |
| 795 |
|
| 796 |
$this->assertTrue(Cache::write('test_groups', 'value2', 'memcached_groups')); |
| 797 |
$this->assertTrue(Cache::clearGroup('group_b', 'memcached_groups')); |
| 798 |
$this->assertFalse(Cache::read('test_groups', 'memcached_groups')); |
| 799 |
} |
| 800 |
} |