PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/sbweb/sbweb_logica/lib/symfony/plugins/sfCompat10Plugin/lib/cache/sfProcessCache.class.php

http://opac-sbweb.googlecode.com/
PHP | 185 lines | 113 code | 16 blank | 56 comment | 11 complexity | 833bf9f46e6e29743a7bcc02b65b024a MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  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. * sfProcessCache stores content in memory if you run a PHP accelerator.
  11. *
  12. * Current PHP accelerator supported: APC, XCache and Eaccelerator.
  13. *
  14. * @package symfony
  15. * @subpackage cache
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. * @version SVN: $Id$
  18. */
  19. class sfProcessCache
  20. {
  21. /**
  22. * Gets the cache engine name or false if no PHP accelerator is enabled.
  23. *
  24. * @return string The cache engine name
  25. */
  26. public static function cacher()
  27. {
  28. static $cacher = null;
  29. if (null === $cacher)
  30. {
  31. if (!sfConfig::get('sf_use_process_cache'))
  32. {
  33. $cacher = false;
  34. }
  35. elseif (function_exists('apc_store'))
  36. {
  37. $cacher = 'apc';
  38. }
  39. elseif (function_exists('xcache_set'))
  40. {
  41. $cacher = 'xcache';
  42. }
  43. elseif (function_exists('eaccelerator_put'))
  44. {
  45. $cacher = 'eaccelerator';
  46. }
  47. else
  48. {
  49. $cacher = false;
  50. }
  51. }
  52. return $cacher;
  53. }
  54. /**
  55. * Gets the prefix to use for all key name.
  56. *
  57. * @return string The prefix string
  58. */
  59. public static function getPrefix()
  60. {
  61. static $prefix = null;
  62. if (!$prefix)
  63. {
  64. $prefix = md5(sfConfig::get('sf_app_dir')).'_';
  65. }
  66. return $prefix;
  67. }
  68. /**
  69. * Sets a value in the cache for the specified key.
  70. *
  71. * @param string The key name
  72. * @param string The content to put in cache
  73. * @param int The life time to keep the content in the cache in seconds
  74. *
  75. * @return boolean true if ok
  76. */
  77. public static function set($key, $value, $lifeTime = 0)
  78. {
  79. switch (self::cacher())
  80. {
  81. case 'apc':
  82. return apc_store(self::getPrefix().$key, $value, $lifeTime);
  83. case 'xcache':
  84. return xcache_set(self::getPrefix().$key, $value, $lifeTime);
  85. case 'eaccelerator':
  86. return eaccelerator_put(self::getPrefix().$key, serialize($value), $lifeTime);
  87. }
  88. return false;
  89. }
  90. /**
  91. * Gets a value in the cache for the specified key.
  92. *
  93. * @param string The key name
  94. *
  95. * @return mixed The content associated with the key or null if the key does not exist
  96. */
  97. public static function get($key)
  98. {
  99. switch (self::cacher())
  100. {
  101. case 'apc':
  102. $value = apc_fetch(self::getPrefix().$key);
  103. return false === $value ? null : $value;
  104. case 'xcache':
  105. return xcache_isset(self::getPrefix().$key) ? xcache_get(self::getPrefix().$key) : null;
  106. case 'eaccelerator':
  107. return unserialize(eaccelerator_get(self::getPrefix().$key));
  108. }
  109. return null;
  110. }
  111. /**
  112. * Returns true if a given key exists in the cache, false otherwise.
  113. *
  114. * @param string The key name
  115. *
  116. * @return boolean true if the key exists, false otherwise
  117. */
  118. public static function has($key)
  119. {
  120. switch (self::cacher())
  121. {
  122. case 'apc':
  123. return false === apc_fetch(self::getPrefix().$key) ? false : true;
  124. case 'xcache':
  125. return xcache_isset(self::getPrefix().$key);
  126. case 'eaccelerator':
  127. return null === eaccelerator_get(self::getPrefix().$key) ? false : true;
  128. }
  129. return false;
  130. }
  131. /**
  132. * Clears the cache.
  133. *
  134. * @return boolean true if ok, false otherwise
  135. */
  136. public static function clear()
  137. {
  138. switch (self::cacher())
  139. {
  140. case 'apc':
  141. return apc_clear_cache('user');
  142. case 'xcache':
  143. for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++)
  144. {
  145. if (!xcache_clear_cache(XC_TYPE_VAR, $i))
  146. {
  147. return false;
  148. }
  149. }
  150. return true;
  151. case 'eaccelerator':
  152. $infos = eaccelerator_list_keys();
  153. if (is_array($infos))
  154. {
  155. foreach ($infos as $info)
  156. {
  157. // eaccelerator bug (http://eaccelerator.net/ticket/287)
  158. $key = 0 === strpos($info['name'], ':') ? substr($info['name'], 1) : $info['name'];
  159. if (!eaccelerator_rm($key))
  160. {
  161. return false;
  162. }
  163. }
  164. }
  165. return true;
  166. }
  167. return false;
  168. }
  169. }