DepcalcCacheBackend.php in Dependency Calculation 8
File
src/Cache/DepcalcCacheBackend.php
View source
<?php
namespace Drupal\depcalc\Cache;
use Drupal\Component\Uuid\Uuid;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Cache\DatabaseBackend;
use Drupal\depcalc\DependencyCalculatorEvents;
use Drupal\depcalc\Event\InvalidateDependenciesEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class DepcalcCacheBackend implements CacheBackendInterface, CacheTagsInvalidatorInterface {
protected $backend;
protected $dispatcher;
protected $connection;
protected $bin;
public function __construct(CacheBackendInterface $backend, EventDispatcherInterface $dispatcher) {
$this->backend = $backend;
$this->dispatcher = $dispatcher;
if ($backend instanceof DatabaseBackend) {
$this
->getProperties($backend);
}
}
private function getProperties(DatabaseBackend $backend) : void {
$r = new \ReflectionObject($backend);
$p = $r
->getProperty('connection');
$p
->setAccessible(TRUE);
$this->connection = $p
->getValue($backend);
$p = $r
->getProperty('bin');
$p
->setAccessible(TRUE);
$this->bin = $p
->getValue($backend);
}
public function invalidateMultiple(array $cids, $allow_invalid = FALSE) {
$original_cids = $cids;
$cache_objects = $this
->getMultiple($cids, $allow_invalid);
$this->backend
->invalidateMultiple($original_cids);
if (!$cache_objects) {
return;
}
$wrappers = array_map(function ($cache) {
return $cache->data;
}, $cache_objects);
$event = new InvalidateDependenciesEvent($wrappers);
$this->dispatcher
->dispatch(DependencyCalculatorEvents::INVALIDATE_DEPENDENCIES, $event);
}
public function get($cid, $allow_invalid = FALSE) {
return $this->backend
->get($cid, $allow_invalid);
}
public function getMultiple(&$cids, $allow_invalid = FALSE) {
return $this->backend
->getMultiple($cids, $allow_invalid);
}
public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
$this->backend
->set($cid, $data, $expire, $tags);
}
public function setMultiple(array $items) {
$this->backend
->setMultiple($items);
}
public function delete($cid) {
$this
->invalidate($cid);
$this->backend
->delete($cid);
}
public function deleteMultiple(array $cids) {
$this->backend
->deleteMultiple($cids);
}
public function deleteAll() {
$this->backend
->deleteAll();
}
public function invalidate($cid) {
$this
->invalidateMultiple([
$cid,
]);
}
public function invalidateAll() {
$this->backend
->invalidateAll();
}
public function garbageCollection() {
$this->backend
->garbageCollection();
}
public function removeBin() {
$this->backend
->removeBin();
}
public function invalidateTags(array $tags) {
if (!Uuid::isValid(reset($tags))) {
return;
}
if ($this->backend instanceof DatabaseBackend) {
if (!$this->connection
->schema()
->tableExists($this->bin)) {
return;
}
foreach ($tags as $tag) {
$result = $this->connection
->select($this->bin, 'bin')
->fields('bin', [
'cid',
])
->condition('tags', "%{$this->connection->escapeLike($tag)}%", 'LIKE')
->execute();
$cids = $result
->fetchCol();
if (empty($cids)) {
continue;
}
$this
->invalidateMultiple($cids, TRUE);
}
}
elseif ($this->backend instanceof CacheTagsInvalidatorInterface) {
$this->backend
->invalidateTags($tags);
}
}
}