Basic Usage

Below is a simple example using the Memcached adapter. See the other adapters for more info.

<?php

include 'vendor/autoload.php';

use \Vespula\Cache\Adapter\Memcached as MemcachedCache;
use \Vespula\Cache\Adapter\File as FileCache;
use \Vespula\Cache\Adapter\Sql as SqlCache;

$phpmemcached = new \Memcached();
$phpmemcached->addServer('localhost', 11211);

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

// Use a DateInterval object for expiry
$interval = new \DateInterval('PT1H'); // Period, Time, 1 Hour
$adapter = new MemcachedCache($phpmemcached, $interval);

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

// Set a value in the cache with expiry of 1800s as a DateInterval object
$interval = new \DateInterval('PT1800S');
$adapter->set('cat', 'meow', $interval);

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