commit fbcfdc4891a31bdf31f72d23dc511a38a28683d5 Author: beejeebus Date: Mon Apr 14 23:57:14 2014 +1000 revert renames, revert getMultiple() diff --git a/core/core.services.yml b/core/core.services.yml index a55844a..99a43e6 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -23,7 +23,7 @@ services: tags: - { name: cache.context} cache_inconsistent_factory: - class: Drupal\Core\Cache\ChainedConsistentAndInconsistentBackendFactory + class: Drupal\Core\Cache\InconsistentBackendFactory arguments: ['@settings'] calls: - [setContainer, ['@service_container']] diff --git a/core/lib/Drupal/Core/Cache/ChainedConsistentAndInconsistentBackend.php b/core/lib/Drupal/Core/Cache/ChainedConsistentAndInconsistentBackend.php deleted file mode 100644 index 50397ba..0000000 --- a/core/lib/Drupal/Core/Cache/ChainedConsistentAndInconsistentBackend.php +++ /dev/null @@ -1,257 +0,0 @@ -consistentBackend = $consistent_backend; - $this->inconsistentBackend = $inconsistent_backend; - $this->bin = 'cache_' . $bin; - $this->state = $state; - $this->lastWriteTimestamp = NULL; - } - - /** - * {@inheritdoc} - */ - public function get($cid, $allow_invalid = FALSE) { - $cids = array($cid); - $cache = $this->getMultiple($cids, $allow_invalid); - return reset($cache); - } - - /** - * Initialize the last write timestamp using the state instance. - */ - public function lastWriteTimestamp() { - if ($this->lastWriteTimestamp === NULL) { - $this->lastWriteTimestamp = $this->state->get(self::LAST_WRITE_TIMESTAMP_PREFIX . $this->bin); - } - return $this->lastWriteTimestamp; - } - - protected function setLastWriteTimestamp($timestamp) { - $this->lastWriteTimestamp = $timestamp; - } - - /** - * Mark the inconsistent cache bin as outdated because of a write. - */ - protected function markAsOutdated() { - $now = time(); - if ($now > $this->lastWriteTimestamp()) { - $this->setLastWriteTimestamp($now); - $this->state->set(self::LAST_WRITE_TIMESTAMP_PREFIX . $this->bin, $this->lastWriteTimestamp); - } - } - - /** - * {@inheritdoc} - */ - public function getMultiple(&$cids, $allow_invalid = FALSE) { - // Retrieve as many cache entries as possible from the (faster) inconsistent - // backend. (Some cache entries may have been created before the last write - // to this cache bin and therefore be stale/wrong/inconsistent.) - $cache = array(); - if ($this->lastWriteTimestamp()) { - $inconsistent_cache_items = $this->inconsistentBackend->getMultiple($cids, $allow_invalid); - $inconsistent_last_write = array_reduce($inconsistent_cache_items, function ($memo, $item) { - return $memo > $item->created ? $memo : $item->created; - }, 0); - - if ($inconsistent_last_write > $this->lastWriteTimestamp()) { - foreach ($inconsistent_cache_items as $item) { - $cache[$item->cid] = $item; - } - } - else { - foreach ($inconsistent_cache_items as $item) { - $cids[] = $item->cid; - } - } - } - - // If there were any cache entries that weren't available in the - // inconsistent backend, retrieve them from the consistent backend and store - // them in the inconsistent one. - if ($cids) { - foreach ($this->consistentBackend->getMultiple($cids, $allow_invalid) as $item) { - $cache[$item->cid] = $item; - $this->inconsistentBackend->set($item->cid, $item->data); - } - } - - return $cache; - } - - /** - * {@inheritdoc} - */ - public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = array()) { - $this->markAsOutdated(); - $this->consistentBackend->set($cid, $data, $expire, $tags); - $this->inconsistentBackend->set($cid, $data, $expire, $tags); - } - - /** - * {@inheritdoc} - */ - public function delete($cid) { - $this->markAsOutdated(); - $this->consistentBackend->deleteMultiple(array($cid)); - } - - /** - * {@inheritdoc} - */ - public function deleteMultiple(array $cids) { - $this->markAsOutdated(); - $this->consistentBackend->deleteMultiple($cids); - } - - /** - * {@inheritdoc} - */ - public function deleteTags(array $tags) { - $this->markAsOutdated(); - $this->consistentBackend->deleteTags($tags); - } - - /** - * {@inheritdoc} - */ - public function deleteAll() { - $this->markAsOutdated(); - $this->consistentBackend->deleteAll(); - } - - /** - * {@inheritdoc} - */ - public function invalidate($cid) { - $this->invalidateMultiple(array($cid)); - } - - /** - * {@inheritdoc} - */ - public function invalidateMultiple(array $cids) { - $this->markAsOutdated(); - $this->consistentBackend->invalidateMultiple($cids); - } - - /** - * {@inheritdoc} - */ - public function invalidateTags(array $tags) { - $this->markAsOutdated(); - $this->consistentBackend->invalidateTags($tags); - } - - /** - * {@inheritdoc} - */ - public function invalidateAll() { - $this->markAsOutdated(); - $this->consistentBackend->invalidateAll(); - } - - /** - * {@inheritdoc} - */ - public function garbageCollection() { - $this->consistentBackend->garbageCollection(); - $this->inconsistentBackend->garbageCollection(); - } - - /** - * {@inheritdoc} - */ - public function isEmpty() { - return $this->consistentBackend->isEmpty(); - } - - /** - * {@inheritdoc} - */ - public function removeBin() { - $this->consistentBackend->removeBin(); - $this->inconsistentBackend->removeBin(); - } - -} diff --git a/core/lib/Drupal/Core/Cache/ChainedConsistentAndInconsistentBackendFactory.php b/core/lib/Drupal/Core/Cache/ChainedConsistentAndInconsistentBackendFactory.php deleted file mode 100644 index 6bf440e..0000000 --- a/core/lib/Drupal/Core/Cache/ChainedConsistentAndInconsistentBackendFactory.php +++ /dev/null @@ -1,46 +0,0 @@ -settings->get('cache'); - if (isset($cache_settings['inconsistent_cache']) && is_array($cache_settings['inconsistent_cache'])) { - if (!empty($cache_settings['inconsistent_cache']['consistent'])) { - $consistent_service = $cache_settings['inconsistent_cache']['consistent']; - } - if (!empty($cache_settings['inconsistent_cache']['inconsistent'])) { - $inconsistent_service = $cache_settings['inconsistent_cache']['inconsistent']; - } - } - - return new ChainedConsistentAndInconsistentBackend( - $this->container->get($consistent_service)->get($bin), - $this->container->get($inconsistent_service)->get($bin), - $bin, - $this->container->get('state') - ); - } - -} diff --git a/core/lib/Drupal/Core/Cache/InconsistentBackend.php b/core/lib/Drupal/Core/Cache/InconsistentBackend.php new file mode 100644 index 0000000..62e0083 --- /dev/null +++ b/core/lib/Drupal/Core/Cache/InconsistentBackend.php @@ -0,0 +1,251 @@ +consistentBackend = $consistent_backend; + $this->inconsistentBackend = $inconsistent_backend; + $this->bin = 'cache_' . $bin; + $this->state = $state; + $this->lastWriteTimestamp = NULL; + } + + /** + * {@inheritdoc} + */ + public function get($cid, $allow_invalid = FALSE) { + $cids = array($cid); + $cache = $this->getMultiple($cids, $allow_invalid); + return reset($cache); + } + + /** + * Initialize the last write timestamp using the state instance. + */ + public function lastWriteTimestamp() { + if ($this->lastWriteTimestamp === NULL) { + $this->lastWriteTimestamp = $this->state->get(self::LAST_WRITE_TIMESTAMP_PREFIX . $this->bin); + } + return $this->lastWriteTimestamp; + } + + protected function setLastWriteTimestamp($timestamp) { + $this->lastWriteTimestamp = $timestamp; + } + + /** + * Mark the inconsistent cache bin as outdated because of a write. + */ + protected function markAsOutdated() { + $now = time(); + if ($now > $this->lastWriteTimestamp()) { + $this->setLastWriteTimestamp($now); + $this->state->set(self::LAST_WRITE_TIMESTAMP_PREFIX . $this->bin, $this->lastWriteTimestamp); + } + } + + /** + * {@inheritdoc} + */ + public function getMultiple(&$cids, $allow_invalid = FALSE) { + // Retrieve as many cache entries as possible from the (faster) inconsistent + // backend. (Some cache entries may have been created before the last write + // to this cache bin and therefore be stale/wrong/inconsistent.) + $cache = array(); + $last_write_timestamp = $this->lastWriteTimestamp(); + if ($last_write_timestamp) { + foreach ($this->inconsistentBackend->getMultiple($cids, $allow_invalid) as $item) { + if ($item->created < $this->lastWriteTimestamp) { + $cids[] = $item->cid; + } + else { + $cache[$item->cid] = $item; + } + } + } + + // If there were any cache entries that weren't available in the + // inconsistent backend, retrieve them from the consistent backend and store + // them in the inconsistent one. + if ($cids) { + foreach ($this->consistentBackend->getMultiple($cids, $allow_invalid) as $item) { + $cache[$item->cid] = $item; + $this->inconsistentBackend->set($item->cid, $item->data); + } + } + + return $cache; + } + + /** + * {@inheritdoc} + */ + public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = array()) { + $this->markAsOutdated(); + $this->consistentBackend->set($cid, $data, $expire, $tags); + $this->inconsistentBackend->set($cid, $data, $expire, $tags); + } + + /** + * {@inheritdoc} + */ + public function delete($cid) { + $this->markAsOutdated(); + $this->consistentBackend->deleteMultiple(array($cid)); + } + + /** + * {@inheritdoc} + */ + public function deleteMultiple(array $cids) { + $this->markAsOutdated(); + $this->consistentBackend->deleteMultiple($cids); + } + + /** + * {@inheritdoc} + */ + public function deleteTags(array $tags) { + $this->markAsOutdated(); + $this->consistentBackend->deleteTags($tags); + } + + /** + * {@inheritdoc} + */ + public function deleteAll() { + $this->markAsOutdated(); + $this->consistentBackend->deleteAll(); + } + + /** + * {@inheritdoc} + */ + public function invalidate($cid) { + $this->invalidateMultiple(array($cid)); + } + + /** + * {@inheritdoc} + */ + public function invalidateMultiple(array $cids) { + $this->markAsOutdated(); + $this->consistentBackend->invalidateMultiple($cids); + } + + /** + * {@inheritdoc} + */ + public function invalidateTags(array $tags) { + $this->markAsOutdated(); + $this->consistentBackend->invalidateTags($tags); + } + + /** + * {@inheritdoc} + */ + public function invalidateAll() { + $this->markAsOutdated(); + $this->consistentBackend->invalidateAll(); + } + + /** + * {@inheritdoc} + */ + public function garbageCollection() { + $this->consistentBackend->garbageCollection(); + $this->inconsistentBackend->garbageCollection(); + } + + /** + * {@inheritdoc} + */ + public function isEmpty() { + return $this->consistentBackend->isEmpty(); + } + + /** + * {@inheritdoc} + */ + public function removeBin() { + $this->consistentBackend->removeBin(); + $this->inconsistentBackend->removeBin(); + } + +} diff --git a/core/lib/Drupal/Core/Cache/InconsistentBackendFactory.php b/core/lib/Drupal/Core/Cache/InconsistentBackendFactory.php new file mode 100644 index 0000000..2671166 --- /dev/null +++ b/core/lib/Drupal/Core/Cache/InconsistentBackendFactory.php @@ -0,0 +1,46 @@ +settings->get('cache'); + if (isset($cache_settings['inconsistent_cache']) && is_array($cache_settings['inconsistent_cache'])) { + if (!empty($cache_settings['inconsistent_cache']['consistent'])) { + $consistent_service = $cache_settings['inconsistent_cache']['consistent']; + } + if (!empty($cache_settings['inconsistent_cache']['inconsistent'])) { + $inconsistent_service = $cache_settings['inconsistent_cache']['inconsistent']; + } + } + + return new InconsistentBackend( + $this->container->get($consistent_service)->get($bin), + $this->container->get($inconsistent_service)->get($bin), + $bin, + $this->container->get('state') + ); + } + +} diff --git a/core/tests/Drupal/Tests/Core/Cache/InconsistentBackendTest.php b/core/tests/Drupal/Tests/Core/Cache/InconsistentBackendTest.php index 405f9e9..d7f777d 100644 --- a/core/tests/Drupal/Tests/Core/Cache/InconsistentBackendTest.php +++ b/core/tests/Drupal/Tests/Core/Cache/InconsistentBackendTest.php @@ -3,7 +3,7 @@ namespace Drupal\Tests\Core\Cache; use Drupal\Core\Cache\MemoryBackend; -use Drupal\Core\Cache\ChainedConsistentAndInconsistentBackend; +use Drupal\Core\Cache\InconsistentBackend; use Drupal\Core\KeyValueStore\StateInterface; use Drupal\Tests\UnitTestCase; @@ -32,7 +32,7 @@ public function testGetFromInconsistentCache() { ->method("get") ->will($this->returnValue(1234)); - $inconsistent_backend = new ChainedConsistentAndInconsistentBackend( + $inconsistent_backend = new InconsistentBackend( $this->consistent_cache, $this->inconsistent_cache, $this->bin, @@ -55,7 +55,7 @@ public function testFallThroughToConsistentCache() { $inconsistent_cache->expects($this->exactly(2)) ->method("set"); - $inconsistent_backend = new ChainedConsistentAndInconsistentBackend( + $inconsistent_backend = new InconsistentBackend( $this->consistent_cache, $inconsistent_cache, $this->bin,