skip to Main Content

On a server I have about 25 websites running the same laravel based backed. I want to change the session&cache driver from file to memcached. But this causes a cached data mixup. e.g. I see the menu from site 1 on site 2.

How can I use memcached on multiple sites?

3

Answers


  1. Chosen as BEST ANSWER

    Based on the idea of a prefix I found a nicer solution for Laravel. But somehow I cant find this on the internet.

    In the cache.php file I added an extra key options with the memcached prefix option that is available in the memcached documentation:

    'memcached' => [
        'driver'  => 'memcached',
        'servers' => [
            [
                'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100
            ],
        ],
        'options' => [
            Memcached::OPT_PREFIX_KEY => "websitename",
        ],
    ],
    

    Now all the sites can run without issues


  2. I think the only possible way of doing this would be to have unique cache keys for each website.

    It could be based on a unique website identifier (id or url).
    I think the logic to retrieve the cache keys could also be encapsulated a bit. In my current setup, I have something a little similar whereby the cache keys are different depending on the locale.

    protected function getCacheKey($key, $locale = null)
    {
        $prefix = '__app_cache_';
        $suffix = $locale ? "_$locale" : '';
    
        return $prefix . $key . $suffix;
    }
    

    For example, your logic could be something like this:

    protected function getCacheKey($key, Website $website)
    {
        $prefix = '__app_cache_';
        $suffix = "_{$website->id}" : '';
    
        return $prefix . $key . $suffix;
    }
    

    and then to get the menu cache key, you would do something like this:

    $cache->getCacheKey($menu, $website); 
    

    This is, obviously, just an example and needs to be adapted to your specific needs.

    Login or Signup to reply.
  3. Brother, If you haven’t used Memcached with Laravel then I would prefer to go with Redis, It has more good speed in easy to work with Laravel.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search