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 https://nette.org
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license https://nette.org/license Nette license
15: * @link https://nette.org
17: * @package Nette\Caching
23: require_once dirname(__FILE__) .
'/../Object.php';
28: * Implements the cache for a application.
30: * @author David Grudl
31: * @copyright Copyright (c) 2004, 2009 David Grudl
32: * @package Nette\Caching
47: /** @var ICacheStorage */
53: /** @var string last query cache */
56: /** @var mixed last query cache */
61: public function __construct(ICacheStorage $storage, $namespace =
NULL)
63: $this->storage =
$storage;
64: $this->namespace =
$namespace ==
NULL ?
'' :
$namespace .
"\x00";
70: * Returns cache storage.
71: * @return ICacheStorage
75: return $this->storage;
81: * Returns cache namespace.
86: return $this->namespace;
92: * Discards the internal cache.
97: $this->key =
$this->data =
NULL;
103: * Writes item into the cache.
105: * priority => (int) priority
106: * expire => (timestamp) expiration
107: * refresh => (bool) use sliding expiration?
108: * tags => (array) tags
109: * files => (array|string) file names
110: * items => (array|string) cache items
111: * consts => (array|string) cache items
117: * @throws InvalidArgumentException
119: public function save($key, $data, array $dependencies =
NULL)
121: if (!is_string($key)) {
122: throw new InvalidArgumentException("Cache key name must be string, " .
gettype($key) .
" given.");
127: $this->adjust($dependencies);
129: $this->storage->write(
130: $this->namespace .
$key,
139: * Removes items from the cache by conditions.
143: public function clean(array $conds =
NULL)
145: $this->adjust($conds);
152: * Update dependencies array.
156: private function adjust(& $arr)
158: if ($arr ===
NULL) {
162: if (isset($arr[self::TAGS])) {
163: $arr[self::TAGS] = (array)
$arr[self::TAGS];
164: foreach ($arr[self::TAGS] as $key =>
$value) {
165: $arr[self::TAGS][$key] =
$this->namespace .
$value;
169: if (isset($arr[self::ITEMS])) {
170: $arr[self::ITEMS] = (array)
$arr[self::ITEMS];
171: foreach ($arr[self::ITEMS] as $key =>
$value) {
172: $arr[self::ITEMS][$key] =
$this->namespace .
$value;
179: /********************* interface \ArrayAccess ****************d*g**/
184: * Inserts (replaces) item into the cache (\ArrayAccess implementation).
188: * @throws InvalidArgumentException
193: throw new InvalidArgumentException("Cache key name must be string, " .
gettype($key) .
" given.");
196: $this->key =
$this->data =
NULL;
197: if ($data ===
NULL) {
198: $this->storage->remove($this->namespace .
$key);
200: $this->storage->write($this->namespace .
$key, $data, array());
207: * Retrieves the specified item from the cache or NULL if the key is not found (\ArrayAccess implementation).
209: * @return mixed|NULL
210: * @throws InvalidArgumentException
215: throw new InvalidArgumentException("Cache key name must be string, " .
gettype($key) .
" given.");
222: $this->data =
$this->storage->read($this->namespace .
$key);
229: * Exists item in cache? (\ArrayAccess implementation).
232: * @throws InvalidArgumentException
237: throw new InvalidArgumentException("Cache key name must be string, " .
gettype($key) .
" given.");
241: $this->data =
$this->storage->read($this->namespace .
$key);
242: return $this->data !==
NULL;
248: * Removes the specified item from the cache.
251: * @throws InvalidArgumentException
256: throw new InvalidArgumentException("Cache key name must be string, " .
gettype($key) .
" given.");
259: $this->key =
$this->data =
NULL;
260: $this->storage->remove($this->namespace .
$key);