banker/src/Driver/MemcacheDriver.php

164 lines
3.1 KiB
PHP
Raw Normal View History

2016-10-19 09:57:06 -04:00
<?php declare(strict_types=1);
2016-09-05 16:43:37 -04:00
/**
* Banker
*
* A Caching library implementing psr/cache
*
2016-10-19 09:57:06 -04:00
* PHP version 7.0
2016-09-05 16:43:37 -04:00
*
* @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
*/
2016-09-06 17:03:43 -04:00
class MemcacheDriver extends AbstractDriver {
2016-09-05 16:43:37 -04:00
/**
* Driver for PHP Memcache extension
*
* @param array $config
2016-09-06 20:57:24 -04:00
* @param array $options
2016-09-05 16:43:37 -04:00
* @throws CacheException
*/
2016-09-06 20:57:24 -04:00
public function __construct(array $config = [], array $options = [])
2016-09-05 16:43:37 -04:00
{
2016-09-06 20:26:28 -04:00
// @codeCoverageIgnoreStart
2016-09-05 16:43:37 -04:00
if ( ! class_exists('Memcache'))
{
throw new CacheException('Memcache driver requires the PHP memcache extension');
}
2016-09-06 20:26:28 -04:00
// @codeCoverageIgnoreEnd
2016-09-05 16:43:37 -04:00
$this->conn = new \Memcache();
$method = ($config['persistent'] === TRUE) ? 'pconnect' : 'connect';
2016-10-19 09:57:06 -04:00
$this->conn->$method($config['host'], (int) $config['port']);
2016-09-05 16:43:37 -04:00
}
/**
* Disconnect from memcached server
*/
public function __destruct()
{
$this->conn->close();
}
/**
* See if a key currently exists in the cache
*
* @param string $key
* @return bool
*/
2016-10-19 09:57:06 -04:00
public function exists(string $key): bool
2016-09-05 16:43:37 -04:00
{
return $this->conn->get($key) !== FALSE;
}
/**
* Get the value for the selected cache key
*
* @param string $key
* @return mixed
*/
2016-10-19 09:57:06 -04:00
public function get(string $key)
2016-09-05 16:43:37 -04:00
{
return $this->conn->get($key);
}
/**
* Retrieve a set of values by their cache key
*
* @param string[] $keys
* @return array
*/
2016-10-19 09:57:06 -04:00
public function getMultiple(array $keys = []): array
2016-09-05 16:43:37 -04:00
{
return $this->conn->get($keys);
}
/**
* Set a cached value
*
* @param string $key
* @param mixed $value
* @param int $expires
* @return DriverInterface
*/
2016-10-19 09:57:06 -04:00
public function set(string $key, $value, int $expires = 0): DriverInterface
2016-09-05 16:43:37 -04:00
{
2016-09-06 17:03:43 -04:00
if ($this->exists($key))
{
$this->conn->replace($key, $value, 0, $expires);
}
else
{
$this->conn->set($key, $value, 0, $expires);
}
2016-09-06 20:26:28 -04:00
2016-09-05 16:43:37 -04:00
return $this;
}
/**
* Remove an item from the cache
*
* @param string $key
* @return boolean
*/
2016-10-19 09:57:06 -04:00
public function delete(string $key): bool
2016-09-05 16:43:37 -04:00
{
return $this->conn->delete($key);
}
/**
* Remove multiple items from the cache
*
* @param string[] $keys
* @return boolean
*/
2016-10-19 09:57:06 -04:00
public function deleteMultiple(array $keys = []): bool
2016-09-05 16:43:37 -04:00
{
// 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
*/
2016-10-19 09:57:06 -04:00
public function flush(): bool
2016-09-05 16:43:37 -04:00
{
return $this->conn->flush();
}
2016-09-06 20:26:28 -04:00
2016-09-06 17:03:43 -04:00
/**
* Set the specified key to expire at the given time
*
* @param string $key
* @param int $expires
2016-10-19 09:57:06 -04:00
* @return boolean
2016-09-06 17:03:43 -04:00
*/
2016-10-19 09:57:06 -04:00
public function expiresAt(string $key, int $expires): bool
2016-09-06 17:03:43 -04:00
{
$value = $this->get($key);
$timediff = $expires - time();
2016-09-06 20:26:28 -04:00
2016-10-19 09:57:06 -04:00
$this->set($key, $value, $timediff);
return TRUE;
2016-09-06 17:03:43 -04:00
}
2016-09-05 16:43:37 -04:00
}