Example #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;
     }
 }
Example #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;
		}
	}
Example #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;
     }
 }
Example #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;
     }
 }
Example #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;
     }
 }