如何在Laravel中使用Cache helper功能

时间:2017-07-18 06:08:45

标签: php laravel caching laravel-5.4

我通过扩展启动方法来启动我的Laravel应用程序,其中我有一个小的检查点来检查缓存中的值。为此,我使用Laravel's自己Illuminate\Foundation helper function cache,但遗憾的是我收到错误,我的应用程序代码为:

<?php

namespace App;

use Illuminate\Foundation\Application as IlluminateApplication;
use LayerShifter\TLDExtract\Extract;

/**
 * Extends \Illuminate\Foundation\Application to override some defaults.
 */
class Application extends IlluminateApplication
{
    /**
     * Is Client.
     *
     * @var boolean
     */
    protected $isClient = false;

    /**
     * Client secret key.
     *
     * @var boolean
     */
    protected $clientSecret;

    /**
     * Client ID.
     *
     * @var boolean
     */
    protected $clientID;

    /**
     * Constructing the class with tenant check
     */
    public function __construct($basePath = null)
    {
        parent::__construct($basePath);

        $this->clientCheck();
    }

    public function clientCheck()
    {
        if($this->isClient = cache('is_client'))
        {
            return $this->isClient;
        }
        else
        {
            $domainData = $this->getDomainSubDomain();
            //Do Check and return the value,
            //Set the values
            // Cache for one day
            $data = //Data which is being recieved
            cache(['is_client' => $data], 1 * 24 * 60);
            return $data;
        }
    }

    /**
     * Get Domain and Sub Domain
     *
     * @return array
     */
    public function getDomainSubDomain()
    {
        $http_req = php_sapi_name() == 'cli' ? 'noetic.com' : $_SERVER["SERVER_NAME"];
        $extract = new Extract();
        $result = $extract->parse($http_req);
        return array(
            "domain" => $result->getHostname() . '.' . $result->getSuffix(),
            "subDomain" => $result->getSubdomain()
        );
    }
}

我得到的错误:

Fatal error: Uncaught ReflectionException: Class cache does not exist in 
E:\xamppNew\htdocs\noeticit\vendor\laravel\framework\src\Illuminate\Container\Container.php:729 Stack trace: #0 
E:\xamppNew\htdocs\noeticit\vendor\laravel\framework\src\Illuminate\Container\Container.php(729): ReflectionClass->__construct('cache') #1 
E:\xamppNew\htdocs\noeticit\vendor\laravel\framework\src\Illuminate\Container\Container.php(608): Illuminate\Container\Container->build('cache') #2 
E:\xamppNew\htdocs\noeticit\vendor\laravel\framework\src\Illuminate\Container\Container.php(575): Illuminate\Container\Container->resolve('cache') #3 
E:\xamppNew\htdocs\noeticit\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(728): Illuminate\Container\Container->make('cache') #4 
E:\xamppNew\htdocs\noeticit\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php(106): Illuminate\Foundation\Application->make('cache') #5 
E:\xamppNew\htdocs\noeticit\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php(230): app('cache') #6 
E:\ in E:\xamppNew\htdocs\noeticit\vendor\laravel\framework\src\Illuminate\Container\Container.php on line 729

指导我如何实现这一目标。

1 个答案:

答案 0 :(得分:-1)

我通常使用Redis,所以我按照以下步骤

composer require predis/predis

然后转到.env文件并将我的CACHE_DRIVER=file更改为CACHE_DRIVER=redis

然后设置数据我使用

Cache::put('key', 'value', $expiresAt);

获取我使用的数据

Cache::get('key');