Get an item from the cache, or store the default value forever.

@param  string   $key
@param  \Closure  $callback
@return mixed
519714
public function sear($key, Closure $callback)
    {
        if (is_array($key)) {
            return $this->searMany($key, $callback);
        }

        return parent::sear($key, $callback);
    }
288136
public static function get($name, $closure = false)
    {

        if (!is_string($name)) {
            throw new \Exception('Cache key must be a string.');
        }

        // First check php-memory cache
        if (isset(self::$cache[$name])) {
            // Cache hit!
            self::$cacheHit = true;
            return self::$cache[$name];
        }

        if (self::$config['memcached']['enabled']) {
            // Connect if not connected
            self::memcachedConnect();

            // Check if in memcached
            $val = self::$memcached->get($name);

            if (self::$memcached->getResultCode() == \Memcached::RES_SUCCESS) {
                // Cache hit!
                self::$cacheHit = true;

                // Set php-memory cache
                self::$cache[$name] = $val;
                return $val;
            }
        }

        self::$cacheHit = false;

        // If no other way to get value, return
        if (!is_callable($closure)) {
            return null;
        }

        // Call given closure to get value
        $val = $closure();

        // Nothing returned, no need to store it
        if (is_null($val)) {
            return null;
        }

        // Store closure-returned value in cache
        self::set($name, $val);

        return $val;
    }
593397
public function get($key)
    {
        $value = $this->memcached->get($this->prefix.$key);

        if ($this->memcached->getResultCode() == 0) {
            return $value;
        }
    }
1151598
protected static function syncCache(string $key, callable $callback)
    {
        if (!isset(static::$cache[$key]))
        {
            static::$cache[static::name($key)] = $callback();
        }

        return static::$cache[static::name($key)];
    }
95487
public function rememberCacheForever($key, $callback)
    {
        $key = $this->getConfigBasedCachePrefix() . $key;

        return $this->rememberCacheForeverOld($key, $callback);
    }
1068541
public static function cache($key, Closure $callback) {
        if (is_array($key)) {
            $key = implode('-', $key);
        }

        if (isset(self::$_cache[$key])) {
            return self::$_cache[$key];
        }

        self::$_cache[$key] = $callback();

        return self::$_cache[$key];
    }
1151273
public static function create(Closure $callback = null)
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }

        if ($callback instanceof Closure) {
            return $callback(self::$instance);
        }

        return self::$instance;
    }
274388
public static function rememberForever($key, Closure $callback, $initDir = '/', $basedir = 'cache')
    {
        return static::remember($key, 99999999, $callback, $initDir, $basedir);
    }
130680
public static function callback($key = null, $callback)
    {
        // Get parameters for callback
        $callbackParams = func_get_args();
        unset($callbackParams[0], $callbackParams[1]);
        $callbackParams = array_values($callbackParams);

        // Get key if no key is given
        if (is_null($key)) {
            if (is_string($callback)) {
                $key = $callback;
            } elseif (is_array($callback)) {
                $keyArr = [];
                foreach ($callback as $k => $v) {
                    if (is_string($v)) {
                        $keyArr[] = $v;
                    } elseif (is_object($v)) {
                        $keyArr[] = get_class($v);
                    }
                }

                $key = implode('', $keyArr);
                if (count($keyArr) > 1) {
                    $method = array_pop($keyArr);
                    $key    = implode('/', $keyArr);
                    $key    .= '::' . $method;
                }

                unset($keyArr, $method);
            } elseif (is_object($callback) && $callback instanceof \Closure) {
                $key = 'closure';
            }
            $key = 'callback: ' . $key;
        }

        // Set default return value
        $returnValue = true;

        // Set error handler, to convert errors to exceptions
        set_error_handler(function ($errno, $errstr, $errfile, $errline, array $errcontext) {
            throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
        });

        try {
            // Start output buffer to capture any output
            ob_start();

            // Start profiler
            self::start($key);

            // Execute callback, and get result
            $callbackResult = call_user_func_array($callback, $callbackParams);

            // Stop profiler
            self::stop($key);

            // Get and clean output buffer
            $callbackOutput = ob_get_clean();
        } catch (\ErrorException $callbackException) {
            // Stop and clean output buffer
            ob_end_clean();

            // Show error message
            self::output('Invalid callback sent to Timer::callback: ' . str_replace('callback: ', '', $key));

            // Clear the item from the collection
            unset(self::$collection[$key]);

            // Clear callback result and output
            unset($callbackResult, $callbackOutput);

            // Set return value to false
            $returnValue = false;
        }

        // Restore error handler
        restore_error_handler();

        // Return result, output or true
        return (isset($callbackResult) ? $callbackResult : (!empty($callbackOutput) ? $callbackOutput : $returnValue));
    }
1307106
public function remember(\Closure $callback, $key)
    {
        if (! config('larecipe.cache.enabled')) {
            return $callback();
        }

        $cachePeriod = $this->checkTtlNeedsChanged(config('larecipe.cache.period'));

        return $this->cache->remember($key, $cachePeriod, $callback);
    }
894921
protected function getCache($key, $refresh, callable $fn)
    {
        if ($refresh || $this->isRefresh()) {
            $this->configCache->remove($key);
        }

        return $this->configCache->get($key, function () use ($fn) {
            return $fn();
        });
    }
1208936
public function onSuccess(Closure $callback)
    {
        return $this->then(function (Container $container) use ($callback) {
            if (0 === $this->exitCode) {
                $container->call($callback);
            }
        });
    }
819137
public function lookup($key, $closure=null)
	{
		// if a closure was provided and we missed, store it
		if(!is_null($closure) && !isset($this->_registry[$key]) && !isset($this->_triggers[$key]))
		{
			$this->_registry[$key] = $this->_memoize($closure($key));
		}

		// the registry stores closures
		if(isset($this->_registry[$key]))
		{
			$result = call_user_func($this->_registry[$key], $this);
			$this->_registry[$key] = $this->_memoize($result);
			return $result;
		}
		// try any registered triggers
		else if(isset($this->_triggers[$key]))
		{
			call_user_func($this->_triggers[$key], $this);
			unset($this->_triggers[$key]);
			return $this->lookup($key);
		}
		else
		{
			throw new RegistryException("No entry for key '$key'");
		}
	}
1115302
public static function make(\Closure $callback = null)
    {
        self::$instance = new static();

        if ($callback instanceof \Closure) {
            return $callback(self::$instance);
        }

        return self::$instance;
    }
546495
protected function get(string $name, callable $callback)
    {
        if (isset($this->cache[$name])) {
            return $this->cache[$name];
        }

        return $this->cache[$name] = call_user_func($callback, $name);
    }
276276
public function map(Closure $callback)
    {
        $hash = $this->create();

        $this->each(function ($value, $key) use ($callback, $hash) {
            $hash[] = $callback($value, $key);
        });

        return $hash;
    }
534748
protected function getFunctionHint(Closure $callback)
    {
        $function = new ReflectionFunction($callback);

        if ($function->getNumberOfParameters() == 0) {
            return;
        }

        $expected = $function->getParameters()[0];

        if (! $expected->getClass()) {
            return;
        }

        return $expected->getClass()->name;
    }