chillerlan/php-cache

A psr/simple-cache implementation. PHP 7.2+

3.1.0 2019-03-06 18:32 UTC

README

A psr/simple-cache implementation for PHP 7.2+.

version license Travis Coverage Scrunitizer Packagist downloads PayPal donate

Features:

Requirements

Documentation

Installation using composer

You can simply clone the repo and run composer install in the root directory. In case you want to include it elsewhere, just add the following to your composer.json:

(note: replace dev-master with a version boundary)

{
	"require": {
		"php": ">=7.2.0",
		"chillerlan/php-cache": "dev-master"
	}
}

Manual installation

Download the desired version of the package from master or release and extract the contents to your project folder.After that:

  • run composer install to install the required dependencies and generate /vendor/autoload.php.
  • if you use a custom autoloader, point the namespace chillerlan\SimpleCache to the folder src of the package

Profit!

Usage

Just invoke a cache instance with the desired CacheInterface like so:

// Redis
$redis = new Redis;
$redis->pconnect('127.0.0.1', 6379);
		
$cache = new RedisCache($redis);

// Memcached
$memcached = new Memcached('myCacheInstance');
$memcached->addServer('localhost', 11211);

$cache = new MemcachedCache($memcached);

// APCU
$cache = new APCUCache;

// File
$cache = new FileCache(new CacheOptions(['filestorage' => __DIR__.'/../.cache']));

// Session
$cache = new SessionCache(new CacheOptions(['cachekey' => '_my_session_cache']));

// Memory
$cache = new MemoryCache;

Methods

See: Psr\SimpleCache\CacheInterface

$cache->get(string $key, $default = null); // -> mixed
$cache->set(string $key, $value, int $ttl = null):bool
$cache->delete(string $key):bool
$cache->has(string $key):bool
$cache->clear():bool
$cache->getMultiple(array $keys, $default = null):array // -> mixed[]
$cache->setMultiple(array $values, int $ttl = null):bool
$cache->deleteMultiple(array $keys):bool

Disclaimer!

I don't take responsibility for molten memory modules, bloated hard disks, self-induced DoS, broken screens etc. Use at your own risk! 🙈