Source for file Cache.php
Documentation is available at Cache.php
6: * @copyright Copyright (c) 2004, 2010 David Grudl
7: * @license http://nettephp.com/license Nette license
8: * @link http://nettephp.com
10: * @package Nette\Caching
16: * Implements the cache for a application.
18: * @copyright Copyright (c) 2004, 2010 David Grudl
19: * @package Nette\Caching
38: /** @ignore internal */
39: const NAMESPACE_SEPARATOR =
"\x00";
41: /** @var ICacheStorage */
47: /** @var string last query cache */
50: /** @var mixed last query cache */
55: public function __construct(ICacheStorage $storage, $namespace =
NULL)
57: $this->storage =
$storage;
58: $this->namespace = (string)
$namespace;
60: if (strpos($this->namespace, self::NAMESPACE_SEPARATOR) !==
FALSE) {
61: throw new InvalidArgumentException("Namespace name contains forbidden character.");
68: * Returns cache storage.
69: * @return ICacheStorage
73: return $this->storage;
79: * Returns cache namespace.
84: return $this->namespace;
90: * Discards the internal cache.
95: $this->key =
$this->data =
NULL;
101: * Writes item into the cache.
103: * - Cache::PRIORITY => (int) priority
104: * - Cache::EXPIRE => (timestamp) expiration
105: * - Cache::SLIDING => (bool) use sliding expiration?
106: * - Cache::TAGS => (array) tags
107: * - Cache::FILES => (array|string) file names
108: * - Cache::ITEMS => (array|string) cache items
109: * - Cache::CONSTS => (array|string) cache items
112: * @param mixed value
113: * @param array dependencies
114: * @return mixed value itself
115: * @throws InvalidArgumentException
117: public function save($key, $data, array $dp =
NULL)
119: if (!is_string($key) &&
!is_int($key)) {
120: throw new InvalidArgumentException("Cache key name must be string or integer, " .
gettype($key) .
" given.");
123: // convert expire into relative amount of seconds
128: // convert FILES into CALLBACKS
129: if (isset($dp[self::FILES])) {
131: foreach ((array)
$dp[self::FILES] as $item) {
132: $dp[self::CALLBACKS][] =
array(array(__CLASS__
, 'checkFile'), $item, @filemtime($item)); // intentionally @
134: unset($dp[self::FILES]);
137: // add namespaces to items
138: if (isset($dp[self::ITEMS])) {
139: $dp[self::ITEMS] = (array)
$dp[self::ITEMS];
140: foreach ($dp[self::ITEMS] as $k =>
$item) {
141: $dp[self::ITEMS][$k] =
$this->namespace .
self::NAMESPACE_SEPARATOR .
$item;
145: // convert CONSTS into CALLBACKS
146: if (isset($dp[self::CONSTS])) {
147: foreach ((array)
$dp[self::CONSTS] as $item) {
148: $dp[self::CALLBACKS][] =
array(array(__CLASS__
, 'checkConst'), $item, constant($item));
150: unset($dp[self::CONSTS]);
154: $dp[self::CALLBACKS][] =
array(array(__CLASS__
, 'checkSerializationVersion'), get_class($data),
159: $this->storage->write(
160: $this->namespace .
self::NAMESPACE_SEPARATOR .
$key,
170: * Removes items from the cache by conditions.
172: * - Cache::PRIORITY => (int) priority
173: * - Cache::TAGS => (array) tags
174: * - Cache::ALL => TRUE
179: public function clean(array $conds =
NULL)
181: $this->storage->clean((array)
$conds);
186: /********************* interface \ArrayAccess ****************d*g**/
191: * Inserts (replaces) item into the cache (\ArrayAccess implementation).
195: * @throws InvalidArgumentException
200: throw new InvalidArgumentException("Cache key name must be string or integer, " .
gettype($key) .
" given.");
203: $this->key =
$this->data =
NULL;
204: if ($data ===
NULL) {
205: $this->storage->remove($this->namespace .
self::NAMESPACE_SEPARATOR .
$key);
207: $this->storage->write($this->namespace .
self::NAMESPACE_SEPARATOR .
$key, $data, array());
214: * Retrieves the specified item from the cache or NULL if the key is not found (\ArrayAccess implementation).
216: * @return mixed|NULL
217: * @throws InvalidArgumentException
222: throw new InvalidArgumentException("Cache key name must be string or integer, " .
gettype($key) .
" given.");
225: $key = (string)
$key;
230: $this->data =
$this->storage->read($this->namespace .
self::NAMESPACE_SEPARATOR .
$key);
237: * Exists item in cache? (\ArrayAccess implementation).
240: * @throws InvalidArgumentException
245: throw new InvalidArgumentException("Cache key name must be string or integer, " .
gettype($key) .
" given.");
249: $this->data =
$this->storage->read($this->namespace .
self::NAMESPACE_SEPARATOR .
$key);
250: return $this->data !==
NULL;
256: * Removes the specified item from the cache.
259: * @throws InvalidArgumentException
264: throw new InvalidArgumentException("Cache key name must be string or integer, " .
gettype($key) .
" given.");
267: $this->key =
$this->data =
NULL;
268: $this->storage->remove($this->namespace .
self::NAMESPACE_SEPARATOR .
$key);
273: /********************* dependency checkers ****************d*g**/
278: * Checks CALLBACKS dependencies.
284: foreach ($callbacks as $callback) {
296: * Checks CONSTS dependency.
301: private static function checkConst($const, $value)
309: * Checks FILES dependency.
314: private static function checkFile($file, $time)
322: * Checks object @serializationVersion label.
327: private static function checkSerializationVersion($class, $value)