MemcachedCacheTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Doctrine\Tests\Common\Cache;
  3. use Doctrine\Common\Cache\MemcachedCache;
  4. use Memcached;
  5. class MemcachedCacheTest extends CacheTest
  6. {
  7. private $memcached;
  8. public function setUp()
  9. {
  10. if ( ! extension_loaded('memcached')) {
  11. $this->markTestSkipped('The ' . __CLASS__ .' requires the use of memcached');
  12. }
  13. $this->memcached = new Memcached();
  14. $this->memcached->setOption(Memcached::OPT_COMPRESSION, false);
  15. $this->memcached->addServer('127.0.0.1', 11211);
  16. if (@fsockopen('127.0.0.1', 11211) === false) {
  17. unset($this->memcached);
  18. $this->markTestSkipped('The ' . __CLASS__ .' cannot connect to memcache');
  19. }
  20. }
  21. public function tearDown()
  22. {
  23. if ($this->memcached instanceof Memcached) {
  24. $this->memcached->flush();
  25. }
  26. }
  27. public function testNoExpire()
  28. {
  29. $cache = $this->_getCacheDriver();
  30. $cache->save('noexpire', 'value', 0);
  31. sleep(1);
  32. $this->assertTrue($cache->contains('noexpire'), 'Memcache provider should support no-expire');
  33. }
  34. public function testLongLifetime()
  35. {
  36. $cache = $this->_getCacheDriver();
  37. $cache->save('key', 'value', 30 * 24 * 3600 + 1);
  38. $this->assertTrue($cache->contains('key'), 'Memcache provider should support TTL > 30 days');
  39. }
  40. protected function _getCacheDriver()
  41. {
  42. $driver = new MemcachedCache();
  43. $driver->setMemcached($this->memcached);
  44. return $driver;
  45. }
  46. }