75 lines
1.6 KiB
PHP
75 lines
1.6 KiB
PHP
<?php
|
|
namespace app\common\cache;
|
|
|
|
use Psr\SimpleCache\CacheInterface;
|
|
|
|
class ThinkphpCacheAdapter implements CacheInterface
|
|
{
|
|
protected $cache;
|
|
|
|
public function __construct($cache)
|
|
{
|
|
$this->cache = $cache;
|
|
}
|
|
|
|
public function get($key, $default = null): mixed
|
|
{
|
|
$value = $this->cache->get($key);
|
|
return $value === false ? $default : $value;
|
|
}
|
|
|
|
public function set($key, $value, $ttl = null): bool
|
|
{
|
|
if ($ttl === null) {
|
|
return $this->cache->set($key, $value);
|
|
}
|
|
return $this->cache->set($key, $value, $ttl);
|
|
}
|
|
|
|
public function delete($key): bool
|
|
{
|
|
return $this->cache->delete($key);
|
|
}
|
|
|
|
public function clear(): bool
|
|
{
|
|
return $this->cache->clear();
|
|
}
|
|
|
|
public function getMultiple($keys, $default = null): iterable
|
|
{
|
|
$results = [];
|
|
foreach ($keys as $key) {
|
|
$results[$key] = $this->get($key, $default);
|
|
}
|
|
return $results;
|
|
}
|
|
|
|
public function setMultiple($values, $ttl = null): bool
|
|
{
|
|
$success = true;
|
|
foreach ($values as $key => $value) {
|
|
if (!$this->set($key, $value, $ttl)) {
|
|
$success = false;
|
|
}
|
|
}
|
|
return $success;
|
|
}
|
|
|
|
public function deleteMultiple($keys): bool
|
|
{
|
|
$success = true;
|
|
foreach ($keys as $key) {
|
|
if (!$this->delete($key)) {
|
|
$success = false;
|
|
}
|
|
}
|
|
return $success;
|
|
}
|
|
|
|
public function has($key): bool
|
|
{
|
|
return $this->cache->has($key);
|
|
}
|
|
}
|