sfCache.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. * sfCache is an abstract class for all cache classes in symfony.
  11. *
  12. * @package symfony
  13. * @subpackage cache
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfCache.class.php 17858 2009-05-01 21:22:50Z FabianLange $
  16. */
  17. abstract class sfCache
  18. {
  19. const OLD = 1;
  20. const ALL = 2;
  21. const SEPARATOR = ':';
  22. protected
  23. $options = array();
  24. /**
  25. * Class constructor.
  26. *
  27. * @see initialize()
  28. */
  29. public function __construct($options = array())
  30. {
  31. $this->initialize($options);
  32. }
  33. /**
  34. * Initializes this sfCache instance.
  35. *
  36. * @param array $options An array of options.
  37. *
  38. * Available options:
  39. *
  40. * * automatic_cleaning_factor: The automatic cleaning process destroy too old (for the given life time) (default value: 1000)
  41. * cache files when a new cache file is written.
  42. * 0 => no automatic cache cleaning
  43. * 1 => systematic cache cleaning
  44. * x (integer) > 1 => automatic cleaning randomly 1 times on x cache write
  45. *
  46. * * lifetime (optional): The default life time (default value: 86400)
  47. *
  48. * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfCache instance.
  49. */
  50. public function initialize($options = array())
  51. {
  52. $this->options = array_merge(array(
  53. 'automatic_cleaning_factor' => 1000,
  54. 'lifetime' => 86400,
  55. 'prefix' => md5(dirname(__FILE__)),
  56. ), $options);
  57. $this->options['prefix'] .= self::SEPARATOR;
  58. }
  59. /**
  60. * Gets the cache content for a given key.
  61. *
  62. * @param string $key The cache key
  63. * @param mixed $default The default value is the key does not exist or not valid anymore
  64. *
  65. * @return mixed The data of the cache
  66. */
  67. abstract public function get($key, $default = null);
  68. /**
  69. * Returns true if there is a cache for the given key.
  70. *
  71. * @param string $key The cache key
  72. *
  73. * @return Boolean true if the cache exists, false otherwise
  74. */
  75. abstract public function has($key);
  76. /**
  77. * Saves some data in the cache.
  78. *
  79. * @param string $key The cache key
  80. * @param mixed $data The data to put in cache
  81. * @param int $lifetime The lifetime
  82. *
  83. * @return Boolean true if no problem
  84. */
  85. abstract public function set($key, $data, $lifetime = null);
  86. /**
  87. * Removes a content from the cache.
  88. *
  89. * @param string $key The cache key
  90. *
  91. * @return Boolean true if no problem
  92. */
  93. abstract public function remove($key);
  94. /**
  95. * Removes content from the cache that matches the given pattern.
  96. *
  97. * @param string $pattern The cache key pattern
  98. *
  99. * @return Boolean true if no problem
  100. *
  101. * @see patternToRegexp
  102. */
  103. abstract public function removePattern($pattern);
  104. /**
  105. * Cleans the cache.
  106. *
  107. * @param string $mode The clean mode
  108. * sfCache::ALL: remove all keys (default)
  109. * sfCache::OLD: remove all expired keys
  110. *
  111. * @return Boolean true if no problem
  112. */
  113. abstract public function clean($mode = self::ALL);
  114. /**
  115. * Returns the timeout for the given key.
  116. *
  117. * @param string $key The cache key
  118. *
  119. * @return int The timeout time
  120. */
  121. abstract public function getTimeout($key);
  122. /**
  123. * Returns the last modification date of the given key.
  124. *
  125. * @param string $key The cache key
  126. *
  127. * @return int The last modified time
  128. */
  129. abstract public function getLastModified($key);
  130. /**
  131. * Gets many keys at once.
  132. *
  133. * @param array $keys An array of keys
  134. *
  135. * @return array An associative array of data from cache
  136. */
  137. public function getMany($keys)
  138. {
  139. $data = array();
  140. foreach ($keys as $key)
  141. {
  142. $data[$key] = $this->get($key);
  143. }
  144. return $data;
  145. }
  146. /**
  147. * Computes lifetime.
  148. *
  149. * @param integer $lifetime Lifetime in seconds
  150. *
  151. * @return integer Lifetime in seconds
  152. */
  153. public function getLifetime($lifetime)
  154. {
  155. return is_null($lifetime) ? $this->getOption('lifetime') : $lifetime;
  156. }
  157. /**
  158. * Gets the backend object.
  159. *
  160. * @return object The backend object
  161. */
  162. public function getBackend()
  163. {
  164. throw new sfException('This cache class does not have a backend object.');
  165. }
  166. /**
  167. * Gets an option value.
  168. *
  169. * @param string $name The option name
  170. * @param mixed $default The default value
  171. *
  172. * @return mixed The option value
  173. */
  174. public function getOption($name, $default = null)
  175. {
  176. return isset($this->options[$name]) ? $this->options[$name] : $default;
  177. }
  178. /**
  179. * Sets an option value.
  180. *
  181. * @param string $name The option name
  182. * @param mixed $value The option value
  183. */
  184. public function setOption($name, $value)
  185. {
  186. return $this->options[$name] = $value;
  187. }
  188. /**
  189. * Converts a pattern to a regular expression.
  190. *
  191. * A pattern can use some special characters:
  192. *
  193. * - * Matches a namespace (foo:*:bar)
  194. * - ** Matches one or more namespaces (foo:**:bar)
  195. *
  196. * @param string $pattern A pattern
  197. *
  198. * @return string A regular expression
  199. */
  200. protected function patternToRegexp($pattern)
  201. {
  202. $regexp = str_replace(
  203. array('\\*\\*', '\\*'),
  204. array('.+?', '[^'.preg_quote(sfCache::SEPARATOR, '#').']+'),
  205. preg_quote($pattern, '#')
  206. );
  207. return '#^'.$regexp.'$#';
  208. }
  209. }