* @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 function apcu_add; use function apcu_clear_cache; use function apcu_delete; use function apcu_exists; use function apcu_fetch; use function apcu_store; use Aviat\Banker\Exception\CacheException; /** * Memcached cache backend */ class ApcuDriver extends AbstractDriver { /** * Constructor * * @param array $config - Not used by this driver * @param array $options - Not used by this driver */ public function __construct(array $config = [], array $options = []) { } /** * Destructor */ public function __destruct() { } /** * 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 { $status = FALSE; $output = apcu_fetch($keys, $status); if ($status === FALSE || !is_array($output)) { return []; } return $output; } /** * 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; } }