- First commit
This commit is contained in:
commit
a6ea7b9cce
|
@ -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.
|
|
@ -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/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,86 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kash;
|
||||||
|
|
||||||
|
use Psr\SimpleCache\CacheInterface;
|
||||||
|
|
||||||
|
class ApcuCache implements CacheInterface
|
||||||
|
{
|
||||||
|
const DEFAULT_NS_SEPARATOR = ':';
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
protected string $namespace,
|
||||||
|
protected int $ttl,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function get(string $key, mixed $default = null): mixed
|
||||||
|
{
|
||||||
|
$value = apcu_fetch($this->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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue