diff --git a/core/lib/Drupal/Core/Cache/ChainedConsistentAndInconsistentBackend.php b/core/lib/Drupal/Core/Cache/ChainedConsistentAndInconsistentBackend.php new file mode 100644 index 0000000..7140030 --- /dev/null +++ b/core/lib/Drupal/Core/Cache/ChainedConsistentAndInconsistentBackend.php @@ -0,0 +1,214 @@ +consistentBackend = $consistent_backend; + $this->inconsistentBackend = $inconsistent_backend; + $this->bin = 'cache_' . $bin; + $this->state = $state; + $this->lastSetTimestamp = 0; + } + + /** + * Implements Drupal\Core\Cache\CacheBackendInterface::get(). + */ + public function get($cid, $allow_invalid = FALSE) { + $cids = array($cid); + $cache = $this->getMultiple($cids, $allow_invalid); + return reset($cache); + } + + public function getConsistencyTimestamp() { + $this->consistencyTimestamp = $this->state->get(CONSISTENCY_TIMESTAMP_PREFIX . $this->bin); + return $this->consistencyTimestamp; + } + + public function setConsistencyTimestamp() { + $now = time(); + if ($now > $this->lastSetTimestamp) { + $this->consistencyTimestamp = $now; + $this->lastSetTimestamp = $now; + $this->state->set(CONSISTENCY_TIMESTAMP_PREFIX . $this->bin, $this->consistencyTimestamp); + } + } + + /** + * Implements Drupal\Core\Cache\CacheBackendInterface::getMultiple(). + */ + public function getMultiple(&$cids, $allow_invalid = FALSE) { + if ($this->consistencyTimestamp === NULL) { + $this->getConsistencyTimestamp(); + } + + $cache = array(); + if ($this->consistencyTimestamp) { + foreach ($this->inconsistentBackend->getMultiple($cids, $allow_invalid) as $item) { + if ($item->created < $this->consistencyTimestamp) { + $cids[] = $item->cid; + } + else { + $cache[$item->cid] = $item; + } + } + } + + if ($cids) { + foreach ($this->consistentBackend->getMultiple($cids, $allow_invalid) as $item) { + $cache[$item->cid] = $item; + } + } + + return $cache; + } + + /** + * Implements Drupal\Core\Cache\CacheBackendInterface::set(). + */ + public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = array()) { + $this->setConsistencyTimestamp(); + $this->consistentBackend->set($cid, $data, $expire, $tags); + } + + /** + * Implements Drupal\Core\Cache\CacheBackendInterface::delete(). + */ + public function delete($cid) { + $this->setConsistencyTimestamp(); + $this->consistentBackend->deleteMultiple(array($cid)); + } + + /** + * Implements Drupal\Core\Cache\CacheBackendInterface::deleteMultiple(). + */ + public function deleteMultiple(array $cids) { + $this->setConsistencyTimestamp(); + $this->consistentBackend->deleteMultiple($cids); + } + + /** + * Implements Drupal\Core\Cache\CacheBackendInterface::deleteTags(). + */ + public function deleteTags(array $tags) { + $this->setConsistencyTimestamp(); + $this->consistentBackend->deleteTags($tags); + } + + /** + * Implements Drupal\Core\Cache\CacheBackendInterface::deleteAll(). + */ + public function deleteAll() { + $this->setConsistencyTimestamp(); + $this->consistentBackend->deleteAll(); + } + + /** + * Implements Drupal\Core\Cache\CacheBackendInterface::invalidate(). + */ + public function invalidate($cid) { + $this->invalidateMultiple(array($cid)); + } + + /** + * Implements Drupal\Core\Cache\CacheBackendInterface::invalideMultiple(). + */ + public function invalidateMultiple(array $cids) { + $this->setConsistencyTimestamp(); + $this->consistentBackend->invalidateMultiple($cids); + } + + /** + * Implements Drupal\Core\Cache\CacheBackendInterface::invalidateTags(). + */ + public function invalidateTags(array $tags) { + $this->setConsistencyTimestamp(); + $this->consistentBackend->invalidateTags($tags); + } + + /** + * Implements Drupal\Core\Cache\CacheBackendInterface::invalidateAll(). + */ + public function invalidateAll() { + $this->setConsistencyTimestamp(); + $this->consistentBackend->invalidateTags($tags); + } + + /** + * Implements Drupal\Core\Cache\CacheBackendInterface::garbageCollection(). + */ + public function garbageCollection() { + $this->consistentBackend->garbageCollection(); + $this->inconsistentBackend->garbageCollection(); + } + + /** + * Implements Drupal\Core\Cache\CacheBackendInterface::isEmpty(). + */ + public function isEmpty() { + return $this->consistentBackend->isEmpty(); + } + + /** + * {@inheritdoc} + */ + public function removeBin() { + $this->consistentBackend->removeBin(); + $this->inconsistentBackend->removeBin(); + } + +}