clowdy / multi-memcached by clowdy

Multiple memcached connection handler for Laravel and elasticache support
25,605
4
6
Package Data
Maintainer Username: clowdy
Maintainer Contact: pulkit@clowdy.com (Pulkit Jalan)
Package Create Date: 2014-09-26
Package Last Update: 2018-09-11
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-04-24 15:00:06
Package Statistics
Total Downloads: 25,605
Monthly Downloads: 59
Daily Downloads: 8
Total Stars: 4
Total Watchers: 6
Total Forks: 6
Total Open Issues: 0

Multiple Memcached Connections

Multiple memcached connection handler for Laravel cache and elasticache support. There is also support for getMulti, putMulti, foreverMulti and forgetMulti specifically for memcached only.

The cache driver can not be swapped if you make use of the additional getMulti, putMulti, foreverMulti or forgetMulti functions or the get, put, forever, forget with arrays. They are specific for memcached only!

Installation

You can install the package using the Composer package manager:

{
    "require": {
        "clowdy/multi-memcached": "0.2.*"
    }
}

Update app/config/app.php with the new service provider.

'providers' => array(
    ...
    //'Illuminate\Cache\CacheServiceProvider',
    'Clowdy\Cache\CacheServiceProvider',
    ...
)

Configuration

The package makes use of the existing memcached configs in app/config/cache.php, with a slightly modified structure.

Example:

...
'memcached' => array(
    'default' => 'data1',

    'connections' => array(

        // cluster
        'data1' => array(
            array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 100),
            array('host' => '127.0.0.1', 'port' => 11212, 'weight' => 100)
        ),

        // single node
        'data2' => array(
            array('host' => '127.0.0.1', 'port' => 11213, 'weight' => 100),
        ),

        // elasticache cluster
        'data3' => array(
            'elasticache' => true
            'servers' => array(
                array('host' => 'memcached.cache.amazonaws.com', 'port' => 11211, 'weight' => 100),
            ),
        )
    )
),

Any other providers in Laravel that make use of memcached will use the default connection. For example the session provider using the memcached driver will use the default connection.

Example Usage

Cache::connection('data1')->get('somekey');

// or you can omit the connection method to use the default connection.

Cache::get('somekey');

// also perform a multi get using an array

Cache::get(['key1', 'key2']);

or

<?php namespace Some\App;

use Illuminate\Cache\Repository as CacheRepository;

class SomeClass
{
    protected $cache;

    public function __construct(CacheRepository $cache)
    {
        $this->cache = $cache;
    }

    public function update($data)
    {
        $this->cache->put('key', $data, 60);
        $this->update2($data);
    }

    public function update2($data)
    {
        $this->cache->connection('data2')->put('key', $data, 60);
        $this->update3(['key' => $data]);
    }

    public function update3(array $data)
    {
        $this->cache->connection('data3')->put(array_keys($data), array_values($data), 60);

        // or
        
        $this->cache->connection('data3')->putMulti($data, 60);
    }
}