sfXCacheCache.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 17858 2009-05-01 21:22:50Z FabianLange $
  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. $set = $this->getBaseValue($key);
  46. if (!is_array($set) || !array_key_exists('data', $set))
  47. {
  48. return $default;
  49. }
  50. return $set['data'];
  51. }
  52. /**
  53. * @see sfCache
  54. */
  55. public function has($key)
  56. {
  57. return xcache_isset($this->getOption('prefix').$key);
  58. }
  59. /**
  60. * @see sfCache
  61. */
  62. public function set($key, $data, $lifetime = null)
  63. {
  64. $lifetime = $this->getLifetime($lifetime);
  65. $set = array(
  66. 'timeout' => time() + $lifetime,
  67. 'data' => $data,
  68. 'ctime' => time()
  69. );
  70. return xcache_set($this->getOption('prefix').$key, $set, $lifetime);
  71. }
  72. /**
  73. * @see sfCache
  74. */
  75. public function remove($key)
  76. {
  77. return xcache_unset($this->getOption('prefix').$key);
  78. }
  79. /**
  80. * @see sfCache
  81. */
  82. public function clean($mode = sfCache::ALL)
  83. {
  84. if ($mode !== sfCache::ALL)
  85. {
  86. return true;
  87. }
  88. $this->checkAuth();
  89. for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++)
  90. {
  91. if (false === xcache_clear_cache(XC_TYPE_VAR, $i))
  92. {
  93. return false;
  94. }
  95. }
  96. return true;
  97. }
  98. /**
  99. * @see sfCache
  100. */
  101. public function getLastModified($key)
  102. {
  103. $set = $this->getBaseValue($key);
  104. if (!is_array($set) || !array_key_exists('ctime', $set))
  105. {
  106. return 0;
  107. }
  108. return $set['ctime'];
  109. }
  110. /**
  111. * @see sfCache
  112. */
  113. public function getTimeout($key)
  114. {
  115. $set = $this->getBaseValue($key);
  116. if (!is_array($set) || !array_key_exists('timeout', $set))
  117. {
  118. return 0;
  119. }
  120. return $set['timeout'];
  121. }
  122. public function getBaseValue($key)
  123. {
  124. return xcache_isset($this->getOption('prefix').$key) ? xcache_get($this->getOption('prefix').$key) : null;
  125. }
  126. /**
  127. * @see sfCache
  128. */
  129. public function removePattern($pattern)
  130. {
  131. $this->checkAuth();
  132. $regexp = self::patternToRegexp($this->getOption('prefix').$pattern);
  133. for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++)
  134. {
  135. $infos = xcache_list(XC_TYPE_VAR, $i);
  136. if (!is_array($infos['cache_list']))
  137. {
  138. return;
  139. }
  140. foreach ($infos['cache_list'] as $info)
  141. {
  142. if (preg_match($regexp, $info['name']))
  143. {
  144. xcache_unset($info['name']);
  145. }
  146. }
  147. }
  148. }
  149. protected function getCacheInfo($key)
  150. {
  151. $this->checkAuth();
  152. for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++)
  153. {
  154. $infos = xcache_list(XC_TYPE_VAR, $i);
  155. if (is_array($infos['cache_list']))
  156. {
  157. foreach ($infos['cache_list'] as $info)
  158. {
  159. if ($this->getOption('prefix').$key == $info['name'])
  160. {
  161. return $info;
  162. }
  163. }
  164. }
  165. }
  166. return null;
  167. }
  168. protected function checkAuth()
  169. {
  170. if (ini_get('xcache.admin.enable_auth'))
  171. {
  172. throw new sfConfigurationException('To use all features of the "sfXCacheCache" class, you must set "xcache.admin.enable_auth" to "Off" in your php.ini.');
  173. }
  174. }
  175. }