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