Packages

  • Nette
    • Application
      • Application\Diagnostics
      • Application\Responses
      • Application\Routers
      • Application\UI
    • Caching
      • Caching\Storages
    • ComponentModel
    • Config
    • Database
      • Database\Diagnostics
      • Database\Drivers
      • Database\Reflection
      • Database\Table
    • DI
    • Diagnostics
    • Forms
      • Forms\Controls
      • Forms\Rendering
    • Http
    • Iterators
    • Latte
      • Latte\Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • None
  • PHP

Classes

  • NCache
  • NCachingHelper

Interfaces

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