A caching library implementing PSR-6 and PSR-16 interfaces
Go to file
Timothy J. Warren 8123d99bee
Some checks failed
Gitea - aviat/banker/pipeline/head There was a failure building this commit
Attempt to add code quality stuff back to Jenkins
2023-03-16 14:54:13 -04:00
.phive Cleanup old tooling 2023-03-16 14:22:49 -04:00
build Attempt to add code quality stuff back to Jenkins 2023-03-16 14:25:46 -04:00
src Fix method signature issue 2023-03-16 14:24:39 -04:00
tests Fix method signature issue 2023-03-16 14:24:39 -04:00
tools Cleanup old tooling 2023-03-16 14:22:49 -04:00
.editorconfig First commit 2016-08-31 12:18:46 -04:00
.gitignore Cleanup old tooling 2023-03-16 14:22:49 -04:00
CHANGELOG.md Bump version to 4.1.0 2023-03-16 13:09:36 -04:00
composer.json Cleanup old tooling 2023-03-16 14:22:49 -04:00
Jenkinsfile Attempt to add code quality stuff back to Jenkins 2023-03-16 14:54:13 -04:00
phpdoc.dist.xml Attempt to add code quality stuff back to Jenkins 2023-03-16 14:25:46 -04:00
phpstan.neon Add PHPStan checks to CI 2021-02-18 13:27:07 -05:00
README.md Update README with simple usage example 2020-05-07 19:43:01 -04:00

Banker

A Caching library implementing the PSR-6 and PSR-16 interfaces for several common cache backends

Cache Backends

  • Apcu
  • Memcached
  • Redis
  • Null - no persistence

Basic Usage (SimpleCache/PSR-16)

<?php
// $config is the configuration array
// $logger is an optional psr/log compatible logger
$cache = new Aviat\Banker\Teller($config, $logger);

// Get a value from the cache, returning $defaultValue
// if the value doesn't exist in the cache
$value = $cache->get($key, $defaultValue);

// Save a new value at the specified key
$saved = $cache->set($key, 'newValue');

Basic Usage (Pool/PSR-6)

<?php
// Create the pool
// $config is the configuration array
// $logger is an optional psr/log compatible logger
$pool = new Aviat\Banker\Pool($config, $logger);

// Grab an item from the cache
$item = $pool->getItem('foo');

// Was there a cache hit?
if ( ! $item->isHit())
{
	// ... Generation of value to cache
	$item->set($value);
	$item->save();
}
else
{
	$value = $item->get();
}

Configuration / Connection array

The config array passed to the Pool class constructor determines which server to connect to. Regardless of the backend, the basic structure is like so:

<?php
$config = [
	'driver' => 'null', // null, apcu, redis, memcached
	'connection' => [
		// Optional (For some drivers):
		// driver setup, see below for the structure for each
		// driver
	],
	'options' => [
		// Optional:
		// Set additional driver-specific options, like persistence for
		// Memcached, or a prefix for Redis keys
	]
];

Below are the connection arrays for each backend:

Memcached:

<?php
$config['connection'] = [
	'host' => 'localhost', // hostname or socket
	'port' => 11211,       // Port needs to be 0 if socket
	'persistent' => false, // Use persistent connection
];

Redis: See Predis documentation. An empty array will connect to localhost on port 6379.

Null, Apcu: No connection parameters