简体   繁体   English

如何在 Laravel 5.7 中使用 Memcached 进行会话?

[英]How to use Memcached for session in Laravel 5.7?

Normally I used file as driver to store session and cache.通常我使用文件作为驱动程序来存储会话和缓存。 Now I want to work with Memcached to store session and cache on Laravel 5.7 application.现在我想使用Memcached在 Laravel 5.7 应用程序上存储会话和缓存。 It is new to me.这对我来说是新的。 I have already installed php-memcached on my local environment.我已经在本地环境中安装了php-memcached

In my Laravel 5.7 web application I have configured session.driver and cache.default in the .env file as following.在我的 Laravel 5.7 Web 应用程序中,我在.env文件中配置了session.drivercache.default ,如下所示。

SESSION_DRIVER=memcached
CACHE_DRIVER=memcached

I leave everything else with its default installation.我将其他所有内容保留为默认安装。 When I refresh the page I get the following error.当我刷新页面时,出现以下错误。

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Class 'Memcached' not found
/vendor/laravel/framework/src/Illuminate/Cache/MemcachedConnector.php 69

I do understand that when you configure driver for session and cache config to use Memcached , the MemcachedConnector will get involved.我知道当您为会话和缓存配置配置驱动程序以使用Memcached ,将涉及MemcachedConnector It imports the Memcached class and tries to create an instance object of the Memcached class as following.它进口Memcached类,并试图创建的实例对象Memcached类如下。

use Memcached;

class MemcachedConnector {
    protected function createMemcachedInstance($connectionId) {
        return empty($connectionId) ? new Memcached : new Memcached($connectionId);
    }
}

With the above error, it seems like the Memcached class is missing.由于上述错误,似乎缺少Memcached类。 How can I solve it?我该如何解决? Please do not tell me to do the following even it solves the problem.即使它解决了问题,请不要告诉我执行以下操作。

SESSION_DRIVER=file
CACHE_DRIVER=file

or或者

SESSION_DRIVER=array
CACHE_DRIVER=array

Because I would like to use Memcached and would like to know what to do to make it work.因为我想使用Memcached并且想知道如何使它工作。

I can't comment yet so sorry for posting as answer.我还不能发表评论,很抱歉张贴作为答案。

Try running php -i || grep 'memcached'尝试运行php -i || grep 'memcached' php -i || grep 'memcached' from your app directory. php -i || grep 'memcached'从您的应用程序目录。

If you're running vagrant, try vagrant ssh then php -i || grep 'memcached'如果您正在运行 vagrant,请尝试vagrant ssh然后php -i || grep 'memcached' php -i || grep 'memcached'

You could also (as suggested below) add phpinfo();您也可以(如下所示)添加phpinfo(); to the top of your routes/web.php (underneath the <? ) and that should spit out your php info when you try to load your site in the browser.到你的routes/web.php的顶部(在<?下方),当你尝试在浏览器中加载你的网站时,它应该会吐出你的 php 信息。

This will clue you up as to whether or not memcached is installed and loaded or not.这将提示您是否安装和加载了 memcached。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM