From 876858b5154a4539dfe9d6e0dbadda762228204b Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Tue, 17 Jan 2017 11:21:07 -0500 Subject: [PATCH] Add APCu driver --- src/Driver/ApcuDriver.php | 133 ++++++++++++++++++++++++++++++++ tests/Driver/ApcuDriverTest.php | 28 +++++++ 2 files changed, 161 insertions(+) create mode 100644 src/Driver/ApcuDriver.php create mode 100644 tests/Driver/ApcuDriverTest.php diff --git a/src/Driver/ApcuDriver.php b/src/Driver/ApcuDriver.php new file mode 100644 index 0000000..08a077b --- /dev/null +++ b/src/Driver/ApcuDriver.php @@ -0,0 +1,133 @@ + + * @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; + + +/** + * Memcached cache backend + */ +class ApcuDriver extends AbstractDriver { + + /** + * See if a key currently exists in the cache + * + * @param string $key + * @return bool + */ + public function exists(string $key): bool + { + return apcu_exists($key) !== FALSE; + } + + /** + * Get the value for the selected cache key + * + * @param string $key + * @return mixed + */ + public function get(string $key) + { + return apcu_fetch($key); + } + + /** + * Retrieve a set of values by their cache key + * + * @param string[] $keys + * @return array + */ + public function getMultiple(array $keys = []): array + { + return apcu_fetch($keys); + } + + /** + * Set a cached value + * + * @param string $key + * @param mixed $value + * @param int $expires + * @return DriverInterface + */ + public function set(string $key, $value, int $expires = 0): DriverInterface + { + if ( ! apcu_exists($key)) + { + apcu_add($key, $value, $expires); + } + else + { + apcu_store($key, $value, $expires); + } + + return $this; + } + + /** + * Remove an item from the cache + * + * @param string $key + * @return boolean + */ + public function delete(string $key): bool + { + return apcu_delete($key); + } + + /** + * Remove multiple items from the cache + * + * @param string[] $keys + * @return boolean + */ + public function deleteMultiple(array $keys = []): bool + { + return apcu_delete($keys); + } + + /** + * Empty the cache + * + * @return boolean + */ + public function flush(): bool + { + return apcu_clear_cache(); + } + + /** + * Set the specified key to expire at the given time + * + * @param string $key + * @param int $expires + * @return boolean + */ + public function expiresAt(string $key, int $expires): bool + { + if ($this->exists($key)) + { + $value = $this->get($key); + return apcu_store($key, $value, $expires); + } + + $this->getLogger()->warn("Tried to set expiration on a key that does not exist"); + + return FALSE; + } +} diff --git a/tests/Driver/ApcuDriverTest.php b/tests/Driver/ApcuDriverTest.php new file mode 100644 index 0000000..1ee33df --- /dev/null +++ b/tests/Driver/ApcuDriverTest.php @@ -0,0 +1,28 @@ + + * @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\Tests\Driver; + +use Aviat\Banker\Driver\ApcuDriver; + +class ApcuDriverTest extends DriverTestBase { + + public function setup() + { + $this->driver = new ApcuDriver(); + $this->driver->flush(); + } +}