PHP code example of yiisoft / cache-memcached

1. Go to this page and download the library: Download yiisoft/cache-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/ */

    

yiisoft / cache-memcached example snippets


$cache = new \Yiisoft\Cache\Memcached\Memcached($persistentId, $servers);

$servers => [
    [
        'host' => 'server-1',
        'port' => 11211,
        'weight' => 100,
    ],
    [
        'host' => 'server-2',
        'port' => 11211,
        'weight' => 50,
    ],
];

$servers => [
    [
        'host' => Memcached::DEFAULT_SERVER_HOST, // '127.0.0.1'
        'port' => Memcached::DEFAULT_SERVER_PORT, // 11211
        'weight' => Memcached::DEFAULT_SERVER_WEIGHT, // 1
    ],
];

$cache = new \Yiisoft\Cache\Memcached\Memcached();
$parameters = ['user_id' => 42];
$key = 'demo';

// try retrieving $data from cache
$data = $cache->get($key);

if ($data === null) {
    // $data is not found in cache, calculate it from scratch
    $data = calculateData($parameters);
    
    // store $data in cache for an hour so that it can be retrieved next time
    $cache->set($key, $data, 3600);
}

// $data is available here

$cache->delete($key);
// Or all cache
$cache->clear();