Add some documentation to the README
This commit is contained in:
parent
3728a1f4f4
commit
b639c5ce87
68
README.md
68
README.md
@ -1,3 +1,69 @@
|
|||||||
# Banker
|
# 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
|
@ -43,8 +43,9 @@ abstract class AbstractDriver implements DriverInterface, LoggerAwareInterface {
|
|||||||
* Common constructor interface for driver classes
|
* Common constructor interface for driver classes
|
||||||
*
|
*
|
||||||
* @param array $config - Connection parameters for the specified backend
|
* @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
|
* Common destructor
|
||||||
|
@ -27,9 +27,10 @@ class MemcacheDriver extends AbstractDriver {
|
|||||||
* Driver for PHP Memcache extension
|
* Driver for PHP Memcache extension
|
||||||
*
|
*
|
||||||
* @param array $config
|
* @param array $config
|
||||||
|
* @param array $options
|
||||||
* @throws CacheException
|
* @throws CacheException
|
||||||
*/
|
*/
|
||||||
public function __construct(array $config = [])
|
public function __construct(array $config = [], array $options = [])
|
||||||
{
|
{
|
||||||
// @codeCoverageIgnoreStart
|
// @codeCoverageIgnoreStart
|
||||||
if ( ! class_exists('Memcache'))
|
if ( ! class_exists('Memcache'))
|
||||||
|
@ -30,9 +30,10 @@ class MemcachedDriver extends AbstractDriver {
|
|||||||
* Driver for PHP Memcache extension
|
* Driver for PHP Memcache extension
|
||||||
*
|
*
|
||||||
* @param array $config
|
* @param array $config
|
||||||
|
* @param array $options
|
||||||
* @throws CacheException
|
* @throws CacheException
|
||||||
*/
|
*/
|
||||||
public function __construct(array $config = [])
|
public function __construct(array $config = [], array $options = [])
|
||||||
{
|
{
|
||||||
// @codeCoverageIgnoreStart
|
// @codeCoverageIgnoreStart
|
||||||
if ( ! class_exists('Memcached'))
|
if ( ! class_exists('Memcached'))
|
||||||
@ -46,8 +47,13 @@ class MemcachedDriver extends AbstractDriver {
|
|||||||
$this->conn = new Memcached();
|
$this->conn = new Memcached();
|
||||||
$this->conn->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
|
$this->conn->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
|
||||||
$this->conn->addServer($config['host'], $config['port']);
|
$this->conn->addServer($config['host'], $config['port']);
|
||||||
|
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
|
if ( ! empty($options))
|
||||||
|
{
|
||||||
|
$this->conn->setOptions($options);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// @codeCoverageIgnoreStart
|
|
||||||
catch (MemcachedException $e)
|
catch (MemcachedException $e)
|
||||||
{
|
{
|
||||||
// Rewrite MemcachedException as a CacheException to
|
// Rewrite MemcachedException as a CacheException to
|
||||||
|
@ -34,8 +34,9 @@ class NullDriver extends AbstractDriver {
|
|||||||
* NullDriver constructor.
|
* NullDriver constructor.
|
||||||
*
|
*
|
||||||
* @param array $config
|
* @param array $config
|
||||||
|
* @param array $options
|
||||||
*/
|
*/
|
||||||
public function __construct(array $config = [])
|
public function __construct(array $config = [], array $options = [])
|
||||||
{
|
{
|
||||||
$this->store = [];
|
$this->store = [];
|
||||||
}
|
}
|
||||||
|
@ -35,9 +35,10 @@ class RedisDriver extends AbstractDriver {
|
|||||||
* RedisDriver constructor.
|
* RedisDriver constructor.
|
||||||
*
|
*
|
||||||
* @param array $config
|
* @param array $config
|
||||||
|
* @param array $options - Predis library connection options
|
||||||
* @throws CacheException
|
* @throws CacheException
|
||||||
*/
|
*/
|
||||||
public function __construct(array $config = [])
|
public function __construct(array $config = [], array $options = [])
|
||||||
{
|
{
|
||||||
// @codeCoverageIgnoreStart
|
// @codeCoverageIgnoreStart
|
||||||
if ( ! class_exists('Predis\\Client'))
|
if ( ! class_exists('Predis\\Client'))
|
||||||
@ -46,7 +47,7 @@ class RedisDriver extends AbstractDriver {
|
|||||||
}
|
}
|
||||||
// @codeCoverageIgnoreEnd
|
// @codeCoverageIgnoreEnd
|
||||||
|
|
||||||
$this->conn = new Client($config);
|
$this->conn = new Client($config, $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -312,6 +312,11 @@ class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
|
|||||||
$driver = ucfirst(strtolower($driverConfig['driver']));
|
$driver = ucfirst(strtolower($driverConfig['driver']));
|
||||||
$class = __NAMESPACE__ . "\\Driver\\${driver}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']);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user