Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • Cache
  • OutputHelper

Interfaces

  • IStorage
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Caching;
  9: 
 10: use Nette,
 11:     Nette\Utils\Callback;
 12: 
 13: 
 14: /**
 15:  * Implements the cache for a application.
 16:  *
 17:  * @author     David Grudl
 18:  *
 19:  * @property-read IStorage $storage
 20:  * @property-read string $namespace
 21:  */
 22: class Cache extends Nette\Object implements \ArrayAccess
 23: {
 24:     /** dependency */
 25:     const PRIORITY = 'priority',
 26:         EXPIRATION = 'expire',
 27:         EXPIRE = 'expire',
 28:         SLIDING = 'sliding',
 29:         TAGS = 'tags',
 30:         FILES = 'files',
 31:         ITEMS = 'items',
 32:         CONSTS = 'consts',
 33:         CALLBACKS = 'callbacks',
 34:         ALL = 'all';
 35: 
 36:     /** @internal */
 37:     const NAMESPACE_SEPARATOR = "\x00";
 38: 
 39:     /** @var IStorage */
 40:     private $storage;
 41: 
 42:     /** @var string */
 43:     private $namespace;
 44: 
 45:     /** @var string  last query cache used by offsetGet() */
 46:     private $key;
 47: 
 48:     /** @var mixed  last query cache used by offsetGet()  */
 49:     private $data;
 50: 
 51: 
 52:     public function __construct(IStorage $storage, $namespace = NULL)
 53:     {
 54:         $this->storage = $storage;
 55:         $this->namespace = $namespace . self::NAMESPACE_SEPARATOR;
 56:     }
 57: 
 58: 
 59:     /**
 60:      * Returns cache storage.
 61:      * @return IStorage
 62:      */
 63:     public function getStorage()
 64:     {
 65:         return $this->storage;
 66:     }
 67: 
 68: 
 69:     /**
 70:      * Returns cache namespace.
 71:      * @return string
 72:      */
 73:     public function getNamespace()
 74:     {
 75:         return (string) substr($this->namespace, 0, -1);
 76:     }
 77: 
 78: 
 79:     /**
 80:      * Returns new nested cache object.
 81:      * @param  string
 82:      * @return Cache
 83:      */
 84:     public function derive($namespace)
 85:     {
 86:         $derived = new static($this->storage, $this->namespace . $namespace);
 87:         return $derived;
 88:     }
 89: 
 90: 
 91:     /**
 92:      * Reads the specified item from the cache or generate it.
 93:      * @param  mixed key
 94:      * @param  callable
 95:      * @return mixed|NULL
 96:      */
 97:     public function load($key, $fallback = NULL)
 98:     {
 99:         $data = $this->storage->read($this->generateKey($key));
100:         if ($data === NULL && $fallback) {
101:             return $this->save($key, Callback::closure($fallback));
102:         }
103:         return $data;
104:     }
105: 
106: 
107:     /**
108:      * Writes item into the cache.
109:      * Dependencies are:
110:      * - Cache::PRIORITY => (int) priority
111:      * - Cache::EXPIRATION => (timestamp) expiration
112:      * - Cache::SLIDING => (bool) use sliding expiration?
113:      * - Cache::TAGS => (array) tags
114:      * - Cache::FILES => (array|string) file names
115:      * - Cache::ITEMS => (array|string) cache items
116:      * - Cache::CONSTS => (array|string) cache items
117:      *
118:      * @param  mixed  key
119:      * @param  mixed  value
120:      * @param  array  dependencies
121:      * @return mixed  value itself
122:      * @throws Nette\InvalidArgumentException
123:      */
124:     public function save($key, $data, array $dependencies = NULL)
125:     {
126:         $this->release();
127:         $key = $this->generateKey($key);
128: 
129:         if ($data instanceof Nette\Callback || $data instanceof \Closure) {
130:             $this->storage->lock($key);
131:             $data = call_user_func_array($data, array(& $dependencies));
132:         }
133: 
134:         if ($data === NULL) {
135:             $this->storage->remove($key);
136:         } else {
137:             $this->storage->write($key, $data, $this->completeDependencies($dependencies, $data));
138:             return $data;
139:         }
140:     }
141: 
142: 
143:     private function completeDependencies($dp, $data)
144:     {
145:         // convert expire into relative amount of seconds
146:         if (isset($dp[Cache::EXPIRATION])) {
147:             $dp[Cache::EXPIRATION] = Nette\Utils\DateTime::from($dp[Cache::EXPIRATION])->format('U') - time();
148:         }
149: 
150:         // convert FILES into CALLBACKS
151:         if (isset($dp[self::FILES])) {
152:             //clearstatcache();
153:             foreach (array_unique((array) $dp[self::FILES]) as $item) {
154:                 $dp[self::CALLBACKS][] = array(array(__CLASS__, 'checkFile'), $item, @filemtime($item)); // @ - stat may fail
155:             }
156:             unset($dp[self::FILES]);
157:         }
158: 
159:         // add namespaces to items
160:         if (isset($dp[self::ITEMS])) {
161:             $dp[self::ITEMS] = array_unique(array_map(array($this, 'generateKey'), (array) $dp[self::ITEMS]));
162:         }
163: 
164:         // convert CONSTS into CALLBACKS
165:         if (isset($dp[self::CONSTS])) {
166:             foreach (array_unique((array) $dp[self::CONSTS]) as $item) {
167:                 $dp[self::CALLBACKS][] = array(array(__CLASS__, 'checkConst'), $item, constant($item));
168:             }
169:             unset($dp[self::CONSTS]);
170:         }
171: 
172:         if (!is_array($dp)) {
173:             $dp = array();
174:         }
175:         return $dp;
176:     }
177: 
178: 
179:     /**
180:      * Removes item from the cache.
181:      * @param  mixed  key
182:      * @return void
183:      */
184:     public function remove($key)
185:     {
186:         $this->save($key, NULL);
187:     }
188: 
189: 
190:     /**
191:      * Removes items from the cache by conditions.
192:      * Conditions are:
193:      * - Cache::PRIORITY => (int) priority
194:      * - Cache::TAGS => (array) tags
195:      * - Cache::ALL => TRUE
196:      * @return void
197:      */
198:     public function clean(array $conditions = NULL)
199:     {
200:         $this->release();
201:         $this->storage->clean((array) $conditions);
202:     }
203: 
204: 
205:     /**
206:      * Caches results of function/method calls.
207:      * @param  mixed
208:      * @return mixed
209:      */
210:     public function call($function)
211:     {
212:         $key = func_get_args();
213:         $key[0] = Callback::toReflection($function);
214:         return $this->load($key, function() use ($function, $key) {
215:             return Callback::invokeArgs($function, array_slice($key, 1));
216:         });
217:     }
218: 
219: 
220:     /**
221:      * Caches results of function/method calls.
222:      * @param  mixed
223:      * @param  array  dependencies
224:      * @return Closure
225:      */
226:     public function wrap($function, array $dependencies = NULL)
227:     {
228:         $cache = $this;
229:         return function() use ($cache, $function, $dependencies) {
230:             $key = array(Callback::toReflection($function), func_get_args());
231:             $data = $cache->load($key);
232:             if ($data === NULL) {
233:                 $data = $cache->save($key, Callback::invokeArgs($function, $key[1]), $dependencies);
234:             }
235:             return $data;
236:         };
237:     }
238: 
239: 
240:     /**
241:      * Starts the output cache.
242:      * @param  mixed  key
243:      * @return OutputHelper|NULL
244:      */
245:     public function start($key)
246:     {
247:         $data = $this->load($key);
248:         if ($data === NULL) {
249:             return new OutputHelper($this, $key);
250:         }
251:         echo $data;
252:     }
253: 
254: 
255:     /**
256:      * Generates internal cache key.
257:      *
258:      * @param  string
259:      * @return string
260:      */
261:     protected function generateKey($key)
262:     {
263:         return $this->namespace . md5(is_scalar($key) ? $key : serialize($key));
264:     }
265: 
266: 
267:     /********************* interface ArrayAccess ****************d*g**/
268: 
269: 
270:     /**
271:      * Inserts (replaces) item into the cache (\ArrayAccess implementation).
272:      * @param  mixed key
273:      * @param  mixed
274:      * @return void
275:      * @throws Nette\InvalidArgumentException
276:      */
277:     public function offsetSet($key, $data)
278:     {
279:         $this->save($key, $data);
280:     }
281: 
282: 
283:     /**
284:      * Retrieves the specified item from the cache or NULL if the key is not found (\ArrayAccess implementation).
285:      * @param  mixed key
286:      * @return mixed|NULL
287:      * @throws Nette\InvalidArgumentException
288:      */
289:     public function offsetGet($key)
290:     {
291:         $key = is_scalar($key) ? (string) $key : serialize($key);
292:         if ($this->key !== $key) {
293:             $this->key = $key;
294:             $this->data = $this->load($key);
295:         }
296:         return $this->data;
297:     }
298: 
299: 
300:     /**
301:      * Exists item in cache? (\ArrayAccess implementation).
302:      * @param  mixed key
303:      * @return bool
304:      * @throws Nette\InvalidArgumentException
305:      */
306:     public function offsetExists($key)
307:     {
308:         $this->release();
309:         return $this->offsetGet($key) !== NULL;
310:     }
311: 
312: 
313:     /**
314:      * Removes the specified item from the cache.
315:      * @param  mixed key
316:      * @return void
317:      * @throws Nette\InvalidArgumentException
318:      */
319:     public function offsetUnset($key)
320:     {
321:         $this->save($key, NULL);
322:     }
323: 
324: 
325:     /**
326:      * Discards the internal cache used by ArrayAccess.
327:      * @return void
328:      */
329:     public function release()
330:     {
331:         $this->key = $this->data = NULL;
332:     }
333: 
334: 
335:     /********************* dependency checkers ****************d*g**/
336: 
337: 
338:     /**
339:      * Checks CALLBACKS dependencies.
340:      * @param  array
341:      * @return bool
342:      */
343:     public static function checkCallbacks($callbacks)
344:     {
345:         foreach ($callbacks as $callback) {
346:             if (!call_user_func_array(array_shift($callback), $callback)) {
347:                 return FALSE;
348:             }
349:         }
350:         return TRUE;
351:     }
352: 
353: 
354:     /**
355:      * Checks CONSTS dependency.
356:      * @param  string
357:      * @param  mixed
358:      * @return bool
359:      */
360:     private static function checkConst($const, $value)
361:     {
362:         return defined($const) && constant($const) === $value;
363:     }
364: 
365: 
366:     /**
367:      * Checks FILES dependency.
368:      * @param  string
369:      * @param  int
370:      * @return bool
371:      */
372:     private static function checkFile($file, $time)
373:     {
374:         return @filemtime($file) == $time; // @ - stat may fail
375:     }
376: 
377: }
378: 
Nette 2.2.2 API API documentation generated by ApiGen 2.8.0