Source for file Cache.php
Documentation is available at Cache.php
6: * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
8: * This source file is subject to the "Nette license" that is bundled
9: * with this package in the file license.txt.
11: * For more information please see http://nettephp.com
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license http://nettephp.com/license Nette license
15: * @link http://nettephp.com
17: * @package Nette\Caching
22: require_once dirname(__FILE__) .
'/../Object.php';
27: * Implements the cache for a application.
29: * @author David Grudl
30: * @copyright Copyright (c) 2004, 2009 David Grudl
31: * @package Nette\Caching
50: /** @ignore internal */
51: const NAMESPACE_SEPARATOR =
"\x00";
53: /** @var ICacheStorage */
59: /** @var string last query cache */
62: /** @var mixed last query cache */
67: public function __construct(ICacheStorage $storage, $namespace =
NULL)
69: $this->storage =
$storage;
70: $this->namespace = (string)
$namespace;
72: if (strpos($this->namespace, self::NAMESPACE_SEPARATOR) !==
FALSE) {
73: throw new InvalidArgumentException("Namespace name contains forbidden character.");
80: * Returns cache storage.
81: * @return ICacheStorage
85: return $this->storage;
91: * Returns cache namespace.
96: return $this->namespace;
102: * Discards the internal cache.
107: $this->key =
$this->data =
NULL;
113: * Writes item into the cache.
115: * - Cache::PRIORITY => (int) priority
116: * - Cache::EXPIRE => (timestamp) expiration
117: * - Cache::SLIDING => (bool) use sliding expiration?
118: * - Cache::TAGS => (array) tags
119: * - Cache::FILES => (array|string) file names
120: * - Cache::ITEMS => (array|string) cache items
121: * - Cache::CONSTS => (array|string) cache items
124: * @param mixed value
125: * @param array dependencies
127: * @throws InvalidArgumentException
129: public function save($key, $data, array $dp =
NULL)
131: if (!is_string($key)) {
132: throw new InvalidArgumentException("Cache key name must be string, " .
gettype($key) .
" given.");
135: // convert expire into relative amount of seconds
138: if (is_string($expire) &&
!is_numeric($expire)) {
139: $expire =
strtotime($expire) -
time();
145: // convert FILES into CALLBACKS
146: if (isset($dp[self::FILES])) {
148: foreach ((array)
$dp[self::FILES] as $item) {
149: $dp[self::CALLBACKS][] =
array(array(__CLASS__
, 'checkFile'), $item, @filemtime($item)); // intentionally @
151: unset($dp[self::FILES]);
154: // add namespaces to items
155: if (isset($dp[self::ITEMS])) {
156: $dp[self::ITEMS] = (array)
$dp[self::ITEMS];
157: foreach ($dp[self::ITEMS] as $k =>
$item) {
158: $dp[self::ITEMS][$k] =
$this->namespace .
self::NAMESPACE_SEPARATOR .
$item;
162: // convert CONSTS into CALLBACKS
163: if (isset($dp[self::CONSTS])) {
164: foreach ((array)
$dp[self::CONSTS] as $item) {
165: $dp[self::CALLBACKS][] =
array(array(__CLASS__
, 'checkConst'), $item, constant($item));
167: unset($dp[self::CONSTS]);
171: $this->storage->write(
172: $this->namespace .
self::NAMESPACE_SEPARATOR .
$key,
181: * Removes items from the cache by conditions.
183: * - Cache::PRIORITY => (int) priority
184: * - Cache::TAGS => (array) tags
185: * - Cache::ALL => TRUE
190: public function clean(array $conds =
NULL)
192: $this->storage->clean((array)
$conds);
197: /********************* interface \ArrayAccess ****************d*g**/
202: * Inserts (replaces) item into the cache (\ArrayAccess implementation).
206: * @throws InvalidArgumentException
211: throw new InvalidArgumentException("Cache key name must be string, " .
gettype($key) .
" given.");
214: $this->key =
$this->data =
NULL;
215: if ($data ===
NULL) {
216: $this->storage->remove($this->namespace .
self::NAMESPACE_SEPARATOR .
$key);
218: $this->storage->write($this->namespace .
self::NAMESPACE_SEPARATOR .
$key, $data, array());
225: * Retrieves the specified item from the cache or NULL if the key is not found (\ArrayAccess implementation).
227: * @return mixed|NULL
228: * @throws InvalidArgumentException
233: throw new InvalidArgumentException("Cache key name must be string, " .
gettype($key) .
" given.");
240: $this->data =
$this->storage->read($this->namespace .
self::NAMESPACE_SEPARATOR .
$key);
247: * Exists item in cache? (\ArrayAccess implementation).
250: * @throws InvalidArgumentException
255: throw new InvalidArgumentException("Cache key name must be string, " .
gettype($key) .
" given.");
259: $this->data =
$this->storage->read($this->namespace .
self::NAMESPACE_SEPARATOR .
$key);
260: return $this->data !==
NULL;
266: * Removes the specified item from the cache.
269: * @throws InvalidArgumentException
274: throw new InvalidArgumentException("Cache key name must be string, " .
gettype($key) .
" given.");
277: $this->key =
$this->data =
NULL;
278: $this->storage->remove($this->namespace .
self::NAMESPACE_SEPARATOR .
$key);
283: /********************* dependency checkers ****************d*g**/
288: * Checks CALLBACKS dependencies.
294: foreach ($callbacks as $callback) {
306: * Checks CONSTS dependency.
311: private static function checkConst($const, $value)
319: * Checks FILES dependency.
324: private static function checkFile($file, $time)