Memcached Adapter

You must have memcached installed on your server. On Ubuntu-type systems you can do:

sudo apt install memcached

sudo apt install php-memcached

Then restart the web server

sudo systemctl restart apache2

Check that the module is loaded:

php -i | grep memcached

Check for memcached server:

ps aux | grep memcached

or

sudo systemctl status memcached

<?php

include 'vendor/autoload.php';

use Vespula\Cache\Adapter\Memcached as MemcachedCache;

// Create new Memcached object
$phpmemcached = new \Memcached();

// add the server
$port = 11211; //this is the default
$phpmemcached->addServer('localhost', $port);

// create the adapter
// Expire in 3600 seconds (1 hr)
// Setting the expiry to 0 indicates NO expiry.
$adapter = new MemcachedCache($phpmemcached, 3600);

$data = [
    'foo'=>'this is foo',
    'bar'=>'this is bar',
    'dog'=>'canine',

];

$adapter->setMultiple($data);

// Set a value in the cache with expiry of 1800s (30 min)
$adapter->set('cat', 'meow', 1800);

// Get a value. If it doesn't exist, return a default value
$value = $adapter->get('cat', 'a default value');

// Delete a value
$adapter->delete($key);

// Check for presence in the cache
$is_cached = $adapter->has($key);

// Delete all entries in the cache
$adapter->flush();

// Update the expiry on an item
$adapter->touch($key, $ttl);

// Delete multiples
$keys = [
    'foo',
    'bar'
];

$adapter->deleteMultiple($keys);