136 lines
2.5 KiB
PHP
136 lines
2.5 KiB
PHP
|
<?php
|
||
|
/**
|
||
|
* Banker
|
||
|
*
|
||
|
* A Caching library implementing psr/cache
|
||
|
*
|
||
|
* PHP version 5.6
|
||
|
*
|
||
|
* @package Banker
|
||
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
||
|
* @copyright 2016 Timothy J. Warren
|
||
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
||
|
* @version 1.0.0
|
||
|
* @link https://git.timshomepage.net/timw4mail/banker
|
||
|
*/
|
||
|
|
||
|
namespace Aviat\Banker\Driver;
|
||
|
|
||
|
use Aviat\Banker\Exception\CacheException;
|
||
|
|
||
|
/**
|
||
|
* Redis cache backend
|
||
|
*/
|
||
|
class MemcacheDriver extends Driver {
|
||
|
|
||
|
/**
|
||
|
* Driver for PHP Memcache extension
|
||
|
*
|
||
|
* @param array $config
|
||
|
* @throws CacheException
|
||
|
*/
|
||
|
public function __construct(array $config = [])
|
||
|
{
|
||
|
if ( ! class_exists('Memcache'))
|
||
|
{
|
||
|
throw new CacheException('Memcache driver requires the PHP memcache extension');
|
||
|
}
|
||
|
|
||
|
$this->conn = new \Memcache();
|
||
|
|
||
|
$method = ($config['persistent'] === TRUE) ? 'pconnect' : 'connect';
|
||
|
|
||
|
$this->conn->$method($config['host'], $config['port']);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Disconnect from memcached server
|
||
|
*/
|
||
|
public function __destruct()
|
||
|
{
|
||
|
$this->conn->close();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* See if a key currently exists in the cache
|
||
|
*
|
||
|
* @param string $key
|
||
|
* @return bool
|
||
|
*/
|
||
|
public function exists($key)
|
||
|
{
|
||
|
return $this->conn->get($key) !== FALSE;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get the value for the selected cache key
|
||
|
*
|
||
|
* @param string $key
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function get($key)
|
||
|
{
|
||
|
return $this->conn->get($key);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Retrieve a set of values by their cache key
|
||
|
*
|
||
|
* @param string[] $keys
|
||
|
* @return array
|
||
|
*/
|
||
|
public function getMultiple(array $keys = [])
|
||
|
{
|
||
|
return $this->conn->get($keys);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Set a cached value
|
||
|
*
|
||
|
* @param string $key
|
||
|
* @param mixed $value
|
||
|
* @param int $expires
|
||
|
* @return DriverInterface
|
||
|
*/
|
||
|
public function set($key, $value, $expires = 0)
|
||
|
{
|
||
|
$this->conn->set($key, $value, 0, $expires);
|
||
|
return $this;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Remove an item from the cache
|
||
|
*
|
||
|
* @param string $key
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public function delete($key)
|
||
|
{
|
||
|
return $this->conn->delete($key);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Remove multiple items from the cache
|
||
|
*
|
||
|
* @param string[] $keys
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public function deleteMultiple(array $keys = [])
|
||
|
{
|
||
|
// Iteratively delete each item, using a boolean
|
||
|
// 'and' operation to return false if any deletion fails
|
||
|
return \array_reduce($keys, function($prev, $key) {
|
||
|
return $prev && $this->conn->delete($key);
|
||
|
}, TRUE);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Empty the cache
|
||
|
*
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public function flush()
|
||
|
{
|
||
|
return $this->conn->flush();
|
||
|
}
|
||
|
}
|