Add some documentation to the README

This commit is contained in:
Timothy Warren 2016-09-06 20:57:24 -04:00
parent 3728a1f4f4
commit b639c5ce87
7 changed files with 104 additions and 23 deletions

View File

@ -1,3 +1,69 @@
# Banker
A Caching library implementing the PSR-6 interface for several common cache backends
A Caching library implementing the PSR-6 interface for several common cache
backends
## Cache Backends
* Memcache
* Memcached
* Redis
* Null - no persistence
### Basic Usage
```php
// Create the pool
$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
[
'driver' => 'null', // null, redis, memcache, memcached
'connection' => [
// 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:
Memcache / Memcached:
```php
[
'host' => 'localhost', // hostname or socket
'port' => 11211, // Port needs to be 0 if socket
'persistent' => false, // Use persistent connection
]
```
Redis:
See [Predis](https://github.com/nrk/predis#connecting-to-redis) documentation. An empty array will connect to localhost on port 6379.
Null:
No connection parameters

View File

@ -43,8 +43,9 @@ abstract class AbstractDriver implements DriverInterface, LoggerAwareInterface {
* Common constructor interface for driver classes
*
* @param array $config - Connection parameters for the specified backend
* @param array $options - Special connection options for the specified backend
*/
abstract public function __construct(array $config = []);
abstract public function __construct(array $config = [], array $options = []);
/**
* Common destructor

View File

@ -27,9 +27,10 @@ class MemcacheDriver extends AbstractDriver {
* Driver for PHP Memcache extension
*
* @param array $config
* @param array $options
* @throws CacheException
*/
public function __construct(array $config = [])
public function __construct(array $config = [], array $options = [])
{
// @codeCoverageIgnoreStart
if ( ! class_exists('Memcache'))

View File

@ -30,9 +30,10 @@ class MemcachedDriver extends AbstractDriver {
* Driver for PHP Memcache extension
*
* @param array $config
* @param array $options
* @throws CacheException
*/
public function __construct(array $config = [])
public function __construct(array $config = [], array $options = [])
{
// @codeCoverageIgnoreStart
if ( ! class_exists('Memcached'))
@ -46,8 +47,13 @@ class MemcachedDriver extends AbstractDriver {
$this->conn = new Memcached();
$this->conn->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$this->conn->addServer($config['host'], $config['port']);
}
// @codeCoverageIgnoreStart
if ( ! empty($options))
{
$this->conn->setOptions($options);
}
}
catch (MemcachedException $e)
{
// Rewrite MemcachedException as a CacheException to

View File

@ -34,8 +34,9 @@ class NullDriver extends AbstractDriver {
* NullDriver constructor.
*
* @param array $config
* @param array $options
*/
public function __construct(array $config = [])
public function __construct(array $config = [], array $options = [])
{
$this->store = [];
}

View File

@ -35,9 +35,10 @@ class RedisDriver extends AbstractDriver {
* RedisDriver constructor.
*
* @param array $config
* @param array $options - Predis library connection options
* @throws CacheException
*/
public function __construct(array $config = [])
public function __construct(array $config = [], array $options = [])
{
// @codeCoverageIgnoreStart
if ( ! class_exists('Predis\\Client'))
@ -46,7 +47,7 @@ class RedisDriver extends AbstractDriver {
}
// @codeCoverageIgnoreEnd
$this->conn = new Client($config);
$this->conn = new Client($config, $options);
}
/**

View File

@ -312,6 +312,11 @@ class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
$driver = ucfirst(strtolower($driverConfig['driver']));
$class = __NAMESPACE__ . "\\Driver\\${driver}Driver";
return new $class($driverConfig['connection']);
if ( ! array_key_exists('options', $driverConfig))
{
$driverConfig['options'] = [];
}
return new $class($driverConfig['connection'], $driverConfig['options']);
}
}