在 Laravel 中配置 Memcached

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35181488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:10:07  来源:igfitidea点击:

Configure Memcached in Laravel

laravellaravel-5memcachedlaravel-5.1

提问by cyber8200

I've tried to set up memcached with my Laravel

我试图用我的 Laravel 设置 memcached

Set Driver

设置驱动程序

/config/app.php

/config/app.php

'default' => 'memcached',


Configure Memcache Server

配置内存缓存服务器

'stores' => [


    'database' => [
        'driver' => 'database',
        'table'  => 'caches',
        'connection' => null,
    ],


    'memcached' => [
        'driver'  => 'memcached',
        'servers' => [
            [
                'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100,
            ],
        ],
    ]

],


Cache

缓存

Route::get('cache/users', function() {
    return Cache::remember('users', 60, function() {
        return User::all();
    });
});


How do I know I configure my caching with memcache properly ? How do I see what I stored ?

我怎么知道我使用 memcache 正确配置了我的缓存?我如何查看我存储的内容?

回答by ceejayoz

First, you can use Cache::has('users')and Cache::get('users')in php artisan tinkeror on a test route of some sort to see if stuff's being saved to the cache (and what the current contents are).

首先,您可以在某种测试路线中或在测试路线上使用Cache::has('users')和来查看是否将内容保存到缓存中(以及当前内容是什么)。Cache::get('users')php artisan tinker

Second, you can connect to memcached (as well as other drivers like redis) via telnetand check directly.

其次,您可以通过 telnet连接到 memcached(以及其他驱动程序,如 redis)并直接检查。

telnet 127.0.0.1 11211

Once in, you can issue commands, like:

进入后,您可以发出命令,例如:

get users

which should spit out the contents of that cache key.

这应该吐出该缓存键的内容。

With the code you've shown, a non-working cache should also result in an exception. You can test this by turning off the memcached instance and refreshing the page - it should error out.

使用您显示的代码,非工作缓存也应该导致异常。您可以通过关闭 memcached 实例并刷新页面来测试这一点 - 它应该会出错。