Home > OS >  Connect 2 Memcached servers on Laravel
Connect 2 Memcached servers on Laravel

Time:11-15

I want to have separated Memcached one for session and another for cache

I tried to do this in Config/cache.php

'memcached' => [
    'driver' => 'memcached',
    'persistent_id' => env('MEMCACHED_PERSISTENT_ID'),
    'sasl' => [
        env('MEMCACHED_USERNAME'),
        env('MEMCACHED_PASSWORD'),
    ],
    'options' => [
        // Memcached::OPT_CONNECT_TIMEOUT => 2000,
    ],
    'servers' => [
        [
            'host' => env('MEMCACHED_HOST', '127.0.0.1'),
            'port' => env('MEMCACHED_PORT', 11211),
            'weight' => 100,
        ],
    ],
],

'memcachedz2' => [
    'driver' => 'memcached',
    'persistent_id' => env('MEMCACHED_PERSISTENT_ID'),
    'sasl' => [
        env('MEMCACHED_USERNAME'),
        env('MEMCACHED_PASSWORD'),
    ],
    'options' => [
        // Memcached::OPT_CONNECT_TIMEOUT => 2000,
    ],
    'servers' => [
        [
            'host' => env('MEMCACHED_HOST1', '127.0.0.1'),
            'port' => env('MEMCACHED_PORT1', 11211),
            'weight' => 100,
        ],
    ],
],

and in .env file I added

CACHE_DRIVER=memcached
CACHE_PREFIX=areviwez_cache
SESSION_DRIVER=memcachedz2

MEMCACHED_HOST=35.192.75.119
MEMCACHED_PORT=11211


MEMCACHED_HOST1=127.0.0.1
MEMCACHED_PORT1=11211

I'm getting this error

Driver [memcachedz2] not supported

but it didn't work
Is it possible to make it work like this ?

CodePudding user response:

The session driver that is specified in config/session.php is not related to the cache driver configuration therefore memcached (meaning use the memcached session driver) would be supported but memcachedz2 would not be supported in your case because there's no session driver defined as memcachedz2 (only a cache driver).

You have 2 options:

  1. Flip your cache driver configurations:
'memcached' => [
    'driver' => 'memcached',
    'persistent_id' => env('MEMCACHED_PERSISTENT_ID'),
    'sasl' => [
        env('MEMCACHED_USERNAME'),
        env('MEMCACHED_PASSWORD'),
    ],
    'options' => [
        // Memcached::OPT_CONNECT_TIMEOUT => 2000,
    ],
    'servers' => [
        [
            'host' => env('MEMCACHED_HOST1', '127.0.0.1'),
            'port' => env('MEMCACHED_PORT1', 11211),
            'weight' => 100,
        ],
    ],
],

'memcachedz2' => [
    'driver' => 'memcached',
    'persistent_id' => env('MEMCACHED_PERSISTENT_ID'),
    'sasl' => [
        env('MEMCACHED_USERNAME'),
        env('MEMCACHED_PASSWORD'),
    ],
    'options' => [
        // Memcached::OPT_CONNECT_TIMEOUT => 2000,
    ],
    'servers' => [
        [
            'host' => env('MEMCACHED_HOST', '127.0.0.1'),
            'port' => env('MEMCACHED_PORT', 11211),
            'weight' => 100,
        ],
    ],
],

and in your .env file:

CACHE_DRIVER=memcachedz2
CACHE_PREFIX=areviwez_cache
SESSION_DRIVER=memcached
  1. Define a cache driver for memcachedz2 by adding the following in any service provider's boot method:
 Session::extend('memcachedz2', function ($app) {
    return new CacheBasedSessionHandler(\Cache::driver('memcachedz2'), config(''session.lifetime'));
});

For your relatively simple use case approach 1 is probably easiest.

  • Related