vespula/simple-cache

A PSR-16 implementation for very simple caching. Nothing fancy. Just dead-simple caching.

0.3.1 2021-09-24 13:42 UTC

This package is auto-updated.

Last update: 2021-09-24 13:43:33 UTC


README

This is a simple, flexible PSR-16 compliant simple-cache implementation for PHP.

Adapters include File, Sql, and Memcached

Documentation is coming, but for now:

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

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

//////////////////////////////

$path = '/some/path/cache'; // read and write by web server process
$fileAdapter = new FileCache($path, 3600);

$pdo = new \PDO("mysql:host=localhost;dbname=mydb;charset=utf8", 'username', '******');
$table = 'vespula_cache';
$sqlAdapter = new SqlCache($pdo, 3600, $table);


Basic interface for PSR-16 is here: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-16-simple-cache.md