From a6ea7b9cce6f53f3e8d1efa776360d35cae85209 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Thu, 19 Jan 2023 01:48:31 +0000 Subject: [PATCH] - First commit --- LICENSE.md | 21 ++++++++++++ composer.json | 21 ++++++++++++ src/ApcuCache.php | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 LICENSE.md create mode 100644 composer.json create mode 100644 src/ApcuCache.php diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..f1d1f0d --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +# The MIT License (MIT) + +Copyright (c) 2023 Dave Mc Nicoll + +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +> THE SOFTWARE. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..f08e070 --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "mcnd/kash", + "description": "A simple caching library making implementing PSR-16 interfaces.", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Dave Mc Nicoll", + "email": "mcndave@gmail.com" + } + ], + "require": { + "php": "^8.2", + "ext-apcu": "*" + }, + "autoload": { + "psr-4": { + "Kash\\": "src/" + } + } +} diff --git a/src/ApcuCache.php b/src/ApcuCache.php new file mode 100644 index 0000000..2aa24ad --- /dev/null +++ b/src/ApcuCache.php @@ -0,0 +1,86 @@ +fullkey($key), $status); + + return $status === true ? $value : $default; + } + + public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool + { + return apcu_store($this->fullkey($key), $value, $this->handleTTL($ttl)); + } + + public function delete(string $key): bool + { + return apcu_delete($this->fullkey($key)); + } + + public function clear() : bool + { + return apcu_clear_cache(); + } + + public function getMultiple(iterable $keys, mixed $default = null): iterable + { + $nsKeys = $this->fullkey($keys); + + $result = apcu_fetch($nsKeys); + + return array_combine(array_map(fn($e) => substr($e, strlen($this->namespace) + strlen(static::DEFAULT_NS_SEPARATOR)), $nsKeys), $result) + array_fill_keys($keys, $default); + } + + public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool + { + $list = array_combine(array_map( [$this, 'prependNamespace' ], array_keys($values), $values); + + $result = apcu_store($list, $this->ttl($ttl)); + + return in_array($result, [ [], true ], true); + } + + public function deleteMultiple(iterable $keys): bool + { + return apcu_delete($this->fullkey($keys)) === []; + } + + public function has(string $key): bool + { + return apcu_exists($this->fullkey($key)); + } + + protected function handleTTL(null|int|\DateInterval $ttl) : int + { + if ($ttl instanceof \DateInterval) { + $ttl = (new \DateTime)->add($ttl)->getTimestamp() - (new \DateTime)->getTimestamp(); + } + + $ttl ??= $this->ttl; + + return (int) $ttl; + } + + protected function fullkey(string|array $key) : string|array + { + return is_array($key) ? array_map([ $this, 'prependNamespace' ], $key) : $this->prependNamespace($key); + } + + protected function prependNamespace(string $key) : string + { + return $this->namespace . static::DEFAULT_NS_SEPARATOR . $key; + } +}