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

Debug toolbar