MemcacheCacheTest.php 1.4 KB

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