2

I've tried to set up memcached with my Laravel

Set Driver

/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 ?

Community
  • 1
  • 1
code-8
  • 54,650
  • 106
  • 352
  • 604

2 Answers2

3

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

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

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.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • Maybe I set it wrong, when I run `telnet 127.0.0.1 11211` , I got `telnet: connect to address 127.0.0.1: Connection refused` – code-8 Feb 03 '16 at 18:10
  • I've tried my Mac IP, I got the same result `telnet 192.168.100.87 11211` – code-8 Feb 03 '16 at 18:12
  • @ihue I have no idea how you set up memcached, so it's hard to tell you what to try, but you could attempt `telnet localhost 11211`. – ceejayoz Feb 03 '16 at 18:45
  • I tried that, I got `Trying ::1... telnet: connect to address ::1: Connection refused Trying 127.0.0.1... telnet: connect to address 127.0.0.1: Connection refused telnet: Unable to connect to remote host` – code-8 Feb 03 '16 at 19:02
  • It's ok. I'll just research more into it. But if you know anything, I hope you dont mind update me. – code-8 Feb 03 '16 at 19:03
  • @ihue Well, did you install and start memcached? – ceejayoz Feb 03 '16 at 19:07
  • Oh o, not yet. I used MAMP on my Mac, should I install it as a separate entity. Do you know anything about that ? – code-8 Feb 03 '16 at 19:09
  • @ihue OK, time for you to take a deep breath and engage the brain. If you don't have memcached installed or turned on, it should be obvious that you can't connect it. The typical way of installing it on a Macwould be via [Homebrew](http://brew.sh/). – ceejayoz Feb 03 '16 at 19:14
  • I should start with that then. I thought it come with Laravel because I saw the config option on there. But I guess not. Thanks. – code-8 Feb 03 '16 at 19:16
0

For the case when Memcache is on separate machine named memcached, i.e. docker with corresponding service: in .env set MEMCACHED_HOST=memcached

AID
  • 124
  • 5