code-8
code-8

Reputation: 58662

Configure Memcached in Laravel

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 ?

Upvotes: 2

Views: 13301

Answers (2)

AID
AID

Reputation: 144

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

Upvotes: 0

ceejayoz
ceejayoz

Reputation: 180024

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.

Upvotes: 3

Related Questions