Skip to content

Commit cc93b77

Browse files
committed
Case 27724; Updated MemcacheBacked lock sanitisation.
1 parent 86475db commit cc93b77

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/MemcacheBackend.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Drupal\memcache;
99

10-
use Drupal\Core\Cache\Cache;
10+
use Drupal\Component\Utility\Crypt;
1111
use Drupal\Core\Cache\CacheBackendInterface;
1212
use Drupal\Core\Cache\CacheTagsChecksumInterface;
1313
use Drupal\Core\Lock\LockBackendInterface;
@@ -245,7 +245,7 @@ public function deleteAll() {
245245
* {@inheritdoc}
246246
*/
247247
public function invalidate($cid) {
248-
$this->invalidateMultiple((array) $cid);
248+
$this->invalidateMultiple([$cid]);
249249
}
250250

251251
/**
@@ -257,10 +257,10 @@ public function invalidate($cid) {
257257
* @param array $cids
258258
* An array of cache IDs to invalidate.
259259
*
260-
* @see Drupal\Core\Cache\CacheBackendInterface::deleteMultiple()
261-
* @see Drupal\Core\Cache\CacheBackendInterface::invalidate()
262-
* @see Drupal\Core\Cache\CacheBackendInterface::invalidateTags()
263-
* @see Drupal\Core\Cache\CacheBackendInterface::invalidateAll()
260+
* @see \Drupal\Core\Cache\CacheBackendInterface::deleteMultiple()
261+
* @see \Drupal\Core\Cache\CacheBackendInterface::invalidate()
262+
* @see \Drupal\Core\Cache\CacheBackendInterface::invalidateTags()
263+
* @see \Drupal\Core\Cache\CacheBackendInterface::invalidateAll()
264264
*/
265265
public function invalidateMultiple(array $cids) {
266266
foreach ($cids as $cid) {
@@ -308,6 +308,32 @@ public function isEmpty() {
308308
return TRUE;
309309
}
310310

311+
/**
312+
* Normalizes a cache ID in order to comply with database limitations.
313+
*
314+
* The lock still uses the database so must comply with limitations.
315+
*
316+
* @param string $cid
317+
* The passed in cache ID.
318+
*
319+
* @return string
320+
* An ASCII-encoded cache ID that is at most 255 characters long.
321+
*/
322+
protected function normalizeCid($cid) {
323+
// Nothing to do if the ID is a US ASCII string of 255 characters or less.
324+
$cid_is_ascii = mb_check_encoding($cid, 'ASCII');
325+
if (strlen($cid) <= 255 && $cid_is_ascii) {
326+
return $cid;
327+
}
328+
// Return a string that uses as much as possible of the original cache ID
329+
// with the hash appended.
330+
$hash = Crypt::hashBase64($cid);
331+
if (!$cid_is_ascii) {
332+
return $hash;
333+
}
334+
return substr($cid, 0, 255 - strlen($hash)) . $hash;
335+
}
336+
311337
/**
312338
* Returns a cache key prefixed with the current bin.
313339
*
@@ -316,7 +342,7 @@ public function isEmpty() {
316342
* @return string
317343
*/
318344
protected function key($cid) {
319-
return $this->bin . '-' . $cid;
345+
return $this->normalizeCid($this->bin . '-' . $cid);
320346
}
321347

322348
}

0 commit comments

Comments
 (0)