sfEAcceleratorCache.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Cache class that stores cached content in EAccelerator.
  11. *
  12. * @package symfony
  13. * @subpackage cache
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfEAcceleratorCache.class.php 6365 2007-12-07 16:03:14Z fabien $
  16. */
  17. class sfEAcceleratorCache extends sfCache
  18. {
  19. /**
  20. * Initializes this sfCache instance.
  21. *
  22. * Available options:
  23. *
  24. * * see sfCache for options available for all drivers
  25. *
  26. * @see sfCache
  27. */
  28. public function initialize($options = array())
  29. {
  30. parent::initialize($options);
  31. if (!function_exists('eaccelerator_put') || !ini_get('eaccelerator.enable'))
  32. {
  33. throw new sfInitializationException('You must have EAccelerator installed and enabled to use sfEAcceleratorCache class (or perhaps you forgot to add --with-eaccelerator-shared-memory when installing).');
  34. }
  35. }
  36. /**
  37. * @see sfCache
  38. */
  39. public function get($key, $default = null)
  40. {
  41. $value = eaccelerator_get($this->getOption('prefix').$key);
  42. return is_null($value) ? $default : $value;
  43. }
  44. /**
  45. * @see sfCache
  46. */
  47. public function has($key)
  48. {
  49. return !is_null(eaccelerator_get($this->getOption('prefix').$key));
  50. }
  51. /**
  52. * @see sfCache
  53. */
  54. public function set($key, $data, $lifetime = null)
  55. {
  56. return eaccelerator_put($this->getOption('prefix').$key, $data, $this->getLifetime($lifetime));
  57. }
  58. /**
  59. * @see sfCache
  60. */
  61. public function remove($key)
  62. {
  63. return eaccelerator_rm($this->getOption('prefix').$key);
  64. }
  65. /**
  66. * @see sfCache
  67. */
  68. public function removePattern($pattern)
  69. {
  70. $infos = eaccelerator_list_keys();
  71. if (is_array($infos))
  72. {
  73. $regexp = self::patternToRegexp($this->getOption('prefix').$pattern);
  74. foreach ($infos as $info)
  75. {
  76. if (preg_match($regexp, $info['name']))
  77. {
  78. eaccelerator_rm($this->getOption('prefix').$key);
  79. }
  80. }
  81. }
  82. }
  83. /**
  84. * @see sfCache
  85. */
  86. public function clean($mode = sfCache::ALL)
  87. {
  88. if (sfCache::OLD === $mode)
  89. {
  90. return eaccelerator_gc();
  91. }
  92. $infos = eaccelerator_list_keys();
  93. if (is_array($infos))
  94. {
  95. foreach ($infos as $info)
  96. {
  97. if (false !== strpos($info['name'], $this->getOption('prefix')))
  98. {
  99. // eaccelerator bug (http://eaccelerator.net/ticket/287)
  100. $key = 0 === strpos($info['name'], ':') ? substr($info['name'], 1) : $info['name'];
  101. if (!eaccelerator_rm($key))
  102. {
  103. return false;
  104. }
  105. }
  106. }
  107. }
  108. return true;
  109. }
  110. /**
  111. * @see sfCache
  112. */
  113. public function getLastModified($key)
  114. {
  115. if ($info = $this->getCacheInfo($key))
  116. {
  117. return $info['created'];
  118. }
  119. return 0;
  120. }
  121. /**
  122. * @see sfCache
  123. */
  124. public function getTimeout($key)
  125. {
  126. if ($info = $this->getCacheInfo($key))
  127. {
  128. return -1 == $info['ttl'] ? 0 : $info['created'] + $info['ttl'];
  129. }
  130. return 0;
  131. }
  132. protected function getCacheInfo($key)
  133. {
  134. $infos = eaccelerator_list_keys();
  135. if (is_array($infos))
  136. {
  137. foreach ($infos as $info)
  138. {
  139. if ($this->getOption('prefix').$key == $info['name'])
  140. {
  141. return $info;
  142. }
  143. }
  144. }
  145. return null;
  146. }
  147. }