media_mpx-8.x-1.x-dev/src/DrupalPSR16/Cache.php
src/DrupalPSR16/Cache.php
<?php
namespace Drupal\media_mpx\DrupalPSR16;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Psr\SimpleCache\CacheInterface;
/**
* Convert Drupal 8+ Cache objects to PSR-16 compliant cache objects.
*
* This was ported over from https://github.com/highwire/drupal-psr-16 because
* that library became unmaintained and we needed it to work with
* psr/simple-cache 3+.
*/
class Cache implements CacheInterface {
/**
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $drupalCacheBackend;
/**
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* @param \Drupal\Core\Cache\CacheBackendInterface $drupal_cache
* The drupal cache backend to convert to PSR-16
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
*
* @example
* $drupalcache = \Drupal::cache('mybin');
* $psr16cache = new \Drupal\media_mpx\DrupalPSR16\Cache($drupalcache);
*/
public function __construct(CacheBackendInterface $drupal_cache, TimeInterface $time) {
$this->drupalCacheBackend = $drupal_cache;
$this->time = $time;
}
/**
* {@inheritdoc}
*/
public function get(string $key, $default = null): mixed {
$cache_entry = $this->drupalCacheBackend->get($key);
if (empty($cache_entry)) {
return $default;
}
else {
return $cache_entry->data;
}
}
/**
* {@inheritdoc}
*/
public function set($key, $value, $ttl = NULL): bool {
if ($ttl === NULL) {
$this->drupalCacheBackend->set($key, $value);
}
else {
$this->drupalCacheBackend->set($key, $value, $this->time->getRequestTime() + $ttl);
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function delete($key): bool {
$this->drupalCacheBackend->delete($key);
return TRUE;
}
/**
* {@inheritdoc}
*/
public function clear(): bool {
$this->drupalCacheBackend->deleteAll();
return TRUE;
}
/**
* {@inheritdoc}
*/
public function getMultiple($keys, $default = NULL): array {
$result = [];
// \Drupal\Core\Cache\CacheBackendInterface::getMultiple() will remove from
// the $keys array passed in the keys successfully returned, but we want
// all of them later, so we use a copy when fetching from the Drupal
// backend.
$k = $keys;
$caches = $this->drupalCacheBackend->getMultiple($k);
foreach ($keys as $key) {
if (!empty($caches[$key])) {
$result[$key] = $caches[$key]->data;
}
else {
$result[$key] = $default;
}
}
return $result;
}
/**
* {@inheritdoc}
*/
public function setMultiple($values, $ttl = NULL): bool {
$items = [];
foreach ($values as $key => $value) {
if ($ttl === NULL) {
$items[$key] = array('data' => $value);
}
else {
$items[$key] = array('data' => $value, 'expire' => time() + $ttl);
}
}
$this->drupalCacheBackend->setMultiple($items);
return TRUE;
}
/**
* {@inheritdoc}
*/
public function deleteMultiple($keys): bool {
$this->drupalCacheBackend->deleteMultiple((array) $keys);
return TRUE;
}
/**
* {@inheritdoc}
*/
public function has($key): bool {
$cache = $this->drupalCacheBackend->get($key);
return !empty($cache);
}
}
