Commit 7347f914 authored by japerry's avatar japerry
Browse files

Revert "Issue #2996615 by m4olivei, wim leers, Fabianx: Transaction support...

Revert "Issue #2996615 by m4olivei, wim leers, Fabianx: Transaction support for cache (tags) invalidation"

This reverts commit be60a5c3.
parent bc3a3775
......@@ -7,9 +7,6 @@
"issues": "https://www.drupal.org/project/issues/memcache"
},
"license": "GPL-2.0-or-later",
"require": {
"drupal/core": "^8.8 || ^9"
},
"extra": {
"branch-alias": {
"dev-8.x-2.x": "2.x-dev"
......
......@@ -2,4 +2,5 @@ name: Memcache
description: 'High performance integration with memcache.'
type: module
package: 'Performance and scalability'
core_version_requirement: ^8.8 || ^9
core: 8.x
core_version_requirement: ^8 || ^9
name: Memcache Admin
description: Adds a User Interface to monitor the Memcache for this site.
package: Performance and scalability
type: module
core_version_requirement: ^8.8 || ^9
core: 8.x
core_version_requirement: ^8 || ^9
configure: memcache_admin.settings
......
......@@ -3,7 +3,6 @@
namespace Drupal\memcache\Cache;
use Drupal\Core\Cache\CacheTagsChecksumInterface;
use Drupal\Core\Cache\CacheTagsChecksumTrait;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\memcache\Invalidator\TimestampInvalidatorInterface;
......@@ -12,10 +11,6 @@ use Drupal\memcache\Invalidator\TimestampInvalidatorInterface;
*/
class TimestampCacheTagsChecksum implements CacheTagsChecksumInterface, CacheTagsInvalidatorInterface {
use CacheTagsChecksumTrait {
getCurrentChecksum as traitGetCurrentChecksum;
}
/**
* The timestamp invalidator object.
*
......@@ -23,6 +18,22 @@ class TimestampCacheTagsChecksum implements CacheTagsChecksumInterface, CacheTag
*/
protected $invalidator;
/**
* Contains already loaded cache invalidations from the backend.
*
* @var array
*/
protected $tagCache = [];
/**
* A list of tags that have already been invalidated in this request.
*
* Used to prevent the invalidation of the same cache tag multiple times.
*
* @var array
*/
protected $invalidatedTags = [];
/**
* Constructs a TimestampCacheTagsChecksum object.
*
......@@ -36,8 +47,14 @@ class TimestampCacheTagsChecksum implements CacheTagsChecksumInterface, CacheTag
/**
* {@inheritdoc}
*/
public function doInvalidateTags(array $tags) {
public function invalidateTags(array $tags) {
foreach ($tags as $tag) {
// @todo Revisit this behavior and determine a better way to handle.
// Only invalidate tags once per request unless they are written again.
if (isset($this->invalidatedTags[$tag])) {
continue;
}
$this->invalidatedTags[$tag] = TRUE;
$this->tagCache[$tag] = $this->invalidator->invalidateTimestamp($tag);
}
}
......@@ -46,7 +63,19 @@ class TimestampCacheTagsChecksum implements CacheTagsChecksumInterface, CacheTag
* {@inheritdoc}
*/
public function getCurrentChecksum(array $tags) {
return min($this->invalidator->getCurrentTimestamp(), $this->traitGetCurrentChecksum($tags));
// @todo Revisit the invalidatedTags hack.
// Remove tags that were already invalidated during this request from the
// static caches so that another invalidation can occur later in the same
// request. Without that, written cache items would not be invalidated
// correctly.
foreach ($tags as $tag) {
unset($this->invalidatedTags[$tag]);
}
// Taking the minimum of the current timestamp and the checksum is used to
// ensure that items that are not valid yet are identified properly as not
// valid. The checksum will change continuously until the item is valid,
// at which point the checksum will match and freeze at that value.
return min($this->invalidator->getCurrentTimestamp(), $this->calculateChecksum($tags));
}
/**
......@@ -73,9 +102,9 @@ class TimestampCacheTagsChecksum implements CacheTagsChecksumInterface, CacheTag
$query_tags = array_diff($tags, array_keys($this->tagCache));
if ($query_tags) {
$tag_invalidations = $this->invalidator->getLastInvalidationTimestamps($query_tags);
$this->tagCache += $tag_invalidations;
$invalid = array_diff($query_tags, array_keys($tag_invalidations));
$backend_tags = $this->invalidator->getLastInvalidationTimestamps($query_tags);
$this->tagCache += $backend_tags;
$invalid = array_diff($query_tags, array_keys($backend_tags));
if (!empty($invalid)) {
// Invalidate any missing tags now. This is necessary because we cannot
// zero-optimize our tag list -- we can't tell the difference between
......@@ -110,21 +139,9 @@ class TimestampCacheTagsChecksum implements CacheTagsChecksumInterface, CacheTag
/**
* {@inheritdoc}
*/
protected function getTagInvalidationCounts(array $tags) {
// Note that the CacheTagsChecksumTrait assumes that the checksum strategy
// uses integer counters on each cache tag, but here we use timestamps. We
// return an empty array since we don't fit that mould. Currently only
// \Drupal\Core\Cache\CacheTagsChecksumTrait::calculateChecksum uses this
// method, which we override.
return [];
public function reset() {
$this->tagCache = [];
$this->invalidatedTags = [];
}
/**
* {@inheritdoc}
*/
protected function getDatabaseConnection() {
// This is not injected to avoid a dependency on the database in the
// critical path. It is only needed during cache tag invalidations.
return \Drupal::database();
}
}
......@@ -74,13 +74,6 @@ class MemcacheBackend implements CacheBackendInterface {
*/
protected $timestampInvalidator;
/**
* Delayed deletions for deletions during a transaction.
*
* @var string[]
*/
protected $delayedDeletions = [];
/**
* Constructs a MemcacheBackend object.
*
......@@ -319,56 +312,18 @@ class MemcacheBackend implements CacheBackendInterface {
* {@inheritdoc}
*/
public function delete($cid) {
$this->deleteMultiple([$cid]);
$this->memcache->delete($cid);
}
/**
* {@inheritdoc}
*/
public function deleteMultiple(array $cids) {
$in_transaction = \Drupal::database()->inTransaction();
if ($in_transaction) {
if (empty($this->delayedDeletions)) {
\Drupal::database()->addRootTransactionEndCallback([$this, 'postRootTransactionCommit']);
}
$this->delayedDeletions = array_unique(array_merge($this->delayedDeletions, $cids));
}
else {
$this->doDeleteMultiple($cids);
}
}
/**
* Execute the deletion.
*
* This can be delayed to avoid race conditions.
*
* @param array $cids
* An array of cache IDs to delete.
*
* @see static::deleteMultiple()
*/
public function doDeleteMultiple(array $cids) {
foreach ($cids as $cid) {
$this->memcache->delete($cid);
}
}
/**
* Callback to be invoked after a database transaction gets committed.
*
* Invalidates all delayed cache deletions.
*
* @param bool $success
* Whether or not the transaction was successful.
*/
public function postRootTransactionCommit($success) {
if ($success) {
$this->doDeleteMultiple($this->delayedDeletions);
}
$this->delayedDeletions = [];
}
/**
* {@inheritdoc}
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment