PHP code example of clowdy / multi-memcached

1. Go to this page and download the library: Download clowdy/multi-memcached library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

clowdy / multi-memcached example snippets


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

...
'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),
            ),
        )
    )
),

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']);

 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);
    }
}