簡體   English   中英

如何在Laravel中使用緩存幫助器功能

[英]How to use Cache helper function in Laravel

我通過擴展boot方法來啟動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

指導我如何實現這一目標。

我通常為此使用Redis,因此請按照以下步驟操作

composer require predis/predis

然后轉到.env文件並將我的CACHE_DRIVER=file更改為CACHE_DRIVER=redis

然后用於設置數據

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

並獲取我使用的數據

Cache::get('key');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM