sfXCacheCache.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 XCache.
  11. *
  12. * @package symfony
  13. * @subpackage cache
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfXCacheCache.class.php 8846 2008-05-07 18:03:12Z fabien $
  16. */
  17. class sfXCacheCache 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('xcache_set'))
  32. {
  33. throw new sfInitializationException('You must have XCache installed and enabled to use sfXCacheCache class.');
  34. }
  35. if (!ini_get('xcache.var_size'))
  36. {
  37. throw new sfInitializationException('You must set the "xcache.var_size" variable to a value greater than 0 to use sfXCacheCache class.');
  38. }
  39. }
  40. /**
  41. * @see sfCache
  42. */
  43. public function get($key, $default = null)
  44. {
  45. return xcache_isset($this->getOption('prefix').$key) ? substr(xcache_get($this->getOption('prefix').$key), 12) : $default;
  46. }
  47. /**
  48. * @see sfCache
  49. */
  50. public function has($key)
  51. {
  52. return xcache_isset($this->getOption('prefix').$key);
  53. }
  54. /**
  55. * @see sfCache
  56. */
  57. public function set($key, $data, $lifetime = null)
  58. {
  59. $lifetime = $this->getLifetime($lifetime);
  60. return xcache_set($this->getOption('prefix').$key, str_pad(time() + $lifetime, 12, 0, STR_PAD_LEFT).$data, $lifetime);
  61. }
  62. /**
  63. * @see sfCache
  64. */
  65. public function remove($key)
  66. {
  67. return xcache_unset($this->getOption('prefix').$key);
  68. }
  69. /**
  70. * @see sfCache
  71. */
  72. public function clean($mode = sfCache::ALL)
  73. {
  74. if ($mode !== sfCache::ALL)
  75. {
  76. return true;
  77. }
  78. $this->checkAuth();
  79. for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++)
  80. {
  81. if (!xcache_clear_cache(XC_TYPE_VAR, $i))
  82. {
  83. return false;
  84. }
  85. }
  86. return true;
  87. }
  88. /**
  89. * @see sfCache
  90. */
  91. public function getLastModified($key)
  92. {
  93. if (!xcache_isset($this->getOption('prefix').$key))
  94. {
  95. return 0;
  96. }
  97. if ($info = $this->getCacheInfo($key))
  98. {
  99. return $info['ctime'];
  100. }
  101. return 0;
  102. }
  103. /**
  104. * @see sfCache
  105. */
  106. public function getTimeout($key)
  107. {
  108. return xcache_isset($this->getOption('prefix').$key) ? intval(substr(xcache_get($this->getOption('prefix').$key), 0, 12)) : 0;
  109. }
  110. /**
  111. * @see sfCache
  112. */
  113. public function removePattern($pattern)
  114. {
  115. $this->checkAuth();
  116. $regexp = self::patternToRegexp($this->getOption('prefix').$pattern);
  117. for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++)
  118. {
  119. $infos = xcache_list(XC_TYPE_VAR, $i);
  120. if (!is_array($infos['cache_list']))
  121. {
  122. return;
  123. }
  124. foreach ($infos['cache_list'] as $info)
  125. {
  126. if (preg_match($regexp, $info['name']))
  127. {
  128. xcache_unset($info['name']);
  129. }
  130. }
  131. }
  132. }
  133. protected function getCacheInfo($key)
  134. {
  135. $this->checkAuth();
  136. for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++)
  137. {
  138. $infos = xcache_list(XC_TYPE_VAR, $i);
  139. if (is_array($infos['cache_list']))
  140. {
  141. foreach ($infos['cache_list'] as $info)
  142. {
  143. if ($this->getOption('prefix').$key == $info['name'])
  144. {
  145. return $info;
  146. }
  147. }
  148. }
  149. }
  150. return null;
  151. }
  152. protected function checkAuth()
  153. {
  154. if (ini_get('xcache.admin.enable_auth'))
  155. {
  156. throw new sfConfigurationException('To use all features of the "sfXCacheCache" class, you must set "xcache.admin.enable_auth" to "Off" in your php.ini.');
  157. }
  158. }
  159. }