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

Debug toolbar