Пример #1
0
 /**
  * Writes item into the cache.
  * Dependencies are:
  * - Cache::PRIORITY => (int) priority
  * - Cache::EXPIRATION => (timestamp) expiration
  * - Cache::SLIDING => (bool) use sliding expiration?
  * - Cache::TAGS => (array) tags
  * - Cache::FILES => (array|string) file names
  * - Cache::ITEMS => (array|string) cache items
  * - Cache::CONSTS => (array|string) cache items
  *
  * @param  mixed  key
  * @param  mixed  value
  * @param  array  dependencies
  * @return mixed  value itself
  * @throws Nette\InvalidArgumentException
  */
 public function save($key, $data, array $dependencies = NULL)
 {
     $key = $this->generateKey($key);
     if ($data instanceof Nette\Callback || $data instanceof \Closure) {
         if ($data instanceof Nette\Callback) {
             trigger_error('Nette\\Callback is deprecated, use closure or Nette\\Utils\\Callback::toClosure().', E_USER_DEPRECATED);
         }
         $this->storage->lock($key);
         try {
             $data = call_user_func_array($data, [&$dependencies]);
         } catch (\Throwable $e) {
             $this->storage->remove($key);
             throw $e;
         } catch (\Exception $e) {
             $this->storage->remove($key);
             throw $e;
         }
     }
     if ($data === NULL) {
         $this->storage->remove($key);
     } else {
         $this->storage->write($key, $data, $this->completeDependencies($dependencies));
         return $data;
     }
 }
Пример #2
0
	/**
	 * Writes item into the cache.
	 * Dependencies are:
	 * - Cache::PRIORITY => (int) priority
	 * - Cache::EXPIRATION => (timestamp) expiration
	 * - Cache::SLIDING => (bool) use sliding expiration?
	 * - Cache::TAGS => (array) tags
	 * - Cache::FILES => (array|string) file names
	 * - Cache::ITEMS => (array|string) cache items
	 * - Cache::CONSTS => (array|string) cache items
	 *
	 * @param  mixed  key
	 * @param  mixed  value
	 * @param  array  dependencies
	 * @return mixed  value itself
	 * @throws Nette\InvalidArgumentException
	 */
	public function save($key, $data, array $dependencies = NULL)
	{
		$this->release();
		$key = $this->generateKey($key);

		if ($data instanceof Nette\Callback || $data instanceof \Closure) {
			$this->storage->lock($key);
			try {
				$data = call_user_func_array($data, array(& $dependencies));
			} catch (\Throwable $e) {
				$this->storage->remove($key);
				throw $e;
			} catch (\Exception $e) {
				$this->storage->remove($key);
				throw $e;
			}
		}

		if ($data === NULL) {
			$this->storage->remove($key);
		} else {
			$this->storage->write($key, $data, $this->completeDependencies($dependencies, $data));
			return $data;
		}
	}
Пример #3
0
 /**
  * Writes item into the cache.
  * Dependencies are:
  * - Cache::PRIORITY => (int) priority
  * - Cache::EXPIRATION => (timestamp) expiration
  * - Cache::SLIDING => (bool) use sliding expiration?
  * - Cache::TAGS => (array) tags
  * - Cache::FILES => (array|string) file names
  * - Cache::ITEMS => (array|string) cache items
  * - Cache::CONSTS => (array|string) cache items
  *
  * @param  mixed  key
  * @param  mixed  value
  * @param  array  dependencies
  * @return mixed  value itself
  * @throws Nette\InvalidArgumentException
  */
 public function save($key, $data, array $dependencies = NULL)
 {
     $key = $this->generateKey($key);
     if ($data instanceof Nette\Callback || $data instanceof \Closure) {
         $this->storage->lock($key);
         $data = call_user_func_array($data, [&$dependencies]);
     }
     if ($data === NULL) {
         $this->storage->remove($key);
     } else {
         $this->storage->write($key, $data, $this->completeDependencies($dependencies, $data));
         return $data;
     }
 }
Пример #4
0
 /**
  * Writes item into the cache.
  * Dependencies are:
  * - Cache::PRIORITY => (int) priority
  * - Cache::EXPIRATION => (timestamp) expiration
  * - Cache::SLIDING => (bool) use sliding expiration?
  * - Cache::TAGS => (array) tags
  * - Cache::FILES => (array|string) file names
  * - Cache::ITEMS => (array|string) cache items
  * - Cache::CONSTS => (array|string) cache items
  *
  * @param  mixed  key
  * @param  mixed  value
  * @param  array  dependencies
  * @return mixed  value itself
  * @throws Nette\InvalidArgumentException
  */
 public function save($key, $data, array $dependencies = NULL)
 {
     $this->release();
     $key = $this->generateKey($key);
     if ($data instanceof Nette\Callback || $data instanceof \Closure) {
         $this->storage->lock($key);
         $data = Nette\Callback::create($data)->invokeArgs(array(&$dependencies));
     }
     if ($data === NULL) {
         $this->storage->remove($key);
     } else {
         $this->storage->write($key, $data, $this->completeDependencies($dependencies, $data));
         return $data;
     }
 }
Пример #5
0
 /**
  * Writes item into the cache.
  * Dependencies are:
  * - Cache::PRIORITY => (int) priority
  * - Cache::EXPIRATION => (timestamp) expiration
  * - Cache::SLIDING => (bool) use sliding expiration?
  * - Cache::TAGS => (array) tags
  * - Cache::FILES => (array|string) file names
  * - Cache::ITEMS => (array|string) cache items
  * - Cache::CONSTS => (array|string) cache items
  *
  * @param  mixed  key
  * @param  mixed  value
  * @param  array  dependencies
  * @return mixed  value itself
  * @throws Nette\InvalidArgumentException
  */
 public function save($key, $data, array $dp = NULL)
 {
     $this->release();
     $key = $this->namespace . md5(is_scalar($key) ? $key : serialize($key));
     if ($data instanceof Nette\Callback || $data instanceof \Closure) {
         $this->storage->lock($key);
         $data = callback($data)->invokeArgs(array(&$dp));
     }
     if ($data === NULL) {
         $this->storage->remove($key);
     } else {
         $this->storage->write($key, $data, $this->completeDependencies($dp, $data));
         return $data;
     }
 }
Пример #6
0
 /**
  * Writes item into the cache.
  * Dependencies are:
  * - Cache::PRIORITY => (int) priority
  * - Cache::EXPIRATION => (timestamp) expiration
  * - Cache::SLIDING => (bool) use sliding expiration?
  * - Cache::TAGS => (array) tags
  * - Cache::FILES => (array|string) file names
  * - Cache::ITEMS => (array|string) cache items
  * - Cache::CONSTS => (array|string) cache items
  *
  * @param  mixed  key
  * @param  mixed  value
  * @param  array  dependencies
  * @return mixed  value itself
  * @throws Nette\InvalidArgumentException
  */
 public function save($key, $data, array $dp = NULL)
 {
     $this->key = is_scalar($key) ? (string) $key : serialize($key);
     $key = $this->namespace . md5($this->key);
     // convert expire into relative amount of seconds
     if (isset($dp[Cache::EXPIRATION])) {
         $dp[Cache::EXPIRATION] = Nette\DateTime::from($dp[Cache::EXPIRATION])->format('U') - time();
     }
     // convert FILES into CALLBACKS
     if (isset($dp[self::FILES])) {
         //clearstatcache();
         foreach ((array) $dp[self::FILES] as $item) {
             $dp[self::CALLBACKS][] = array(array(__CLASS__, 'checkFile'), $item, @filemtime($item));
             // @ - stat may fail
         }
         unset($dp[self::FILES]);
     }
     // add namespaces to items
     if (isset($dp[self::ITEMS])) {
         $dp[self::ITEMS] = (array) $dp[self::ITEMS];
         foreach ($dp[self::ITEMS] as $k => $item) {
             $dp[self::ITEMS][$k] = $this->namespace . md5(is_scalar($item) ? $item : serialize($item));
         }
     }
     // convert CONSTS into CALLBACKS
     if (isset($dp[self::CONSTS])) {
         foreach ((array) $dp[self::CONSTS] as $item) {
             $dp[self::CALLBACKS][] = array(array(__CLASS__, 'checkConst'), $item, constant($item));
         }
         unset($dp[self::CONSTS]);
     }
     if ($data instanceof Nette\Callback || $data instanceof \Closure) {
         Nette\Utils\CriticalSection::enter();
         $data = $data->__invoke();
         Nette\Utils\CriticalSection::leave();
     }
     if (is_object($data)) {
         $dp[self::CALLBACKS][] = array(array(__CLASS__, 'checkSerializationVersion'), get_class($data), Nette\Reflection\ClassType::from($data)->getAnnotation('serializationVersion'));
     }
     $this->data = $data;
     if ($data === NULL) {
         $this->storage->remove($key);
     } else {
         $this->storage->write($key, $data, (array) $dp);
     }
     return $data;
 }