Skip to content

Commit 85990bf

Browse files
committed
#2867682; Added cache key normalizer.
1 parent cc93b77 commit 85990bf

File tree

3 files changed

+51
-44
lines changed

3 files changed

+51
-44
lines changed

src/DrupalMemcacheBase.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
abstract class DrupalMemcacheBase implements DrupalMemcacheInterface {
1616

17+
use MemcacheCacheNormalizer;
18+
1719
/**
1820
* The memcache config object.
1921
*
@@ -79,17 +81,7 @@ public function get($key) {
7981
* {@inheritdoc}
8082
*/
8183
public function key($key) {
82-
$full_key = urlencode($this->prefix . '-' . $key);
83-
84-
// Memcache only supports key lengths up to 250 bytes. If we have generated
85-
// a longer key, we shrink it to an acceptable length with a configurable
86-
// hashing algorithm. Sha1 was selected as the default as it performs
87-
// quickly with minimal collisions.
88-
if (strlen($full_key) > 250) {
89-
$full_key = urlencode(hash($this->hashAlgorithm, $this->prefix . '-' . $key));
90-
}
91-
92-
return $full_key;
84+
return $this->normalizeKey($this->prefix . '-' . $key);
9385
}
9486

9587
/**

src/MemcacheBackend.php

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

88
namespace Drupal\memcache;
99

10-
use Drupal\Component\Utility\Crypt;
10+
use Drupal\Core\Cache\Cache;
1111
use Drupal\Core\Cache\CacheBackendInterface;
1212
use Drupal\Core\Cache\CacheTagsChecksumInterface;
1313
use Drupal\Core\Lock\LockBackendInterface;
@@ -17,6 +17,8 @@
1717
*/
1818
class MemcacheBackend implements CacheBackendInterface {
1919

20+
use MemcacheCacheNormalizer;
21+
2022
/**
2123
* The cache bin to use.
2224
*
@@ -245,7 +247,7 @@ public function deleteAll() {
245247
* {@inheritdoc}
246248
*/
247249
public function invalidate($cid) {
248-
$this->invalidateMultiple([$cid]);
250+
$this->invalidateMultiple((array) $cid);
249251
}
250252

251253
/**
@@ -257,10 +259,10 @@ public function invalidate($cid) {
257259
* @param array $cids
258260
* An array of cache IDs to invalidate.
259261
*
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()
262+
* @see Drupal\Core\Cache\CacheBackendInterface::deleteMultiple()
263+
* @see Drupal\Core\Cache\CacheBackendInterface::invalidate()
264+
* @see Drupal\Core\Cache\CacheBackendInterface::invalidateTags()
265+
* @see Drupal\Core\Cache\CacheBackendInterface::invalidateAll()
264266
*/
265267
public function invalidateMultiple(array $cids) {
266268
foreach ($cids as $cid) {
@@ -308,32 +310,6 @@ public function isEmpty() {
308310
return TRUE;
309311
}
310312

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-
337313
/**
338314
* Returns a cache key prefixed with the current bin.
339315
*
@@ -342,7 +318,7 @@ protected function normalizeCid($cid) {
342318
* @return string
343319
*/
344320
protected function key($cid) {
345-
return $this->normalizeCid($this->bin . '-' . $cid);
321+
return $this->normalizeKey($this->bin . '-' . $cid);
346322
}
347323

348324
}

src/MemcacheCacheNormalizer.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Drupal\memcache;
4+
5+
/**
6+
* Class MemcacheCacheNormalizer.
7+
*/
8+
trait MemcacheCacheNormalizer {
9+
10+
/**
11+
* Normalizes a cache ID in order to comply with key length limitations.
12+
*
13+
* @param string $key
14+
* The passed in cache ID.
15+
*
16+
* @return string
17+
* An ASCII-encoded cache ID that is at most 250 characters long.
18+
*/
19+
protected function normalizeKey($key) {
20+
$key = urlencode($key);
21+
// Nothing to do if the ID is a US ASCII string of 250 characters or less.
22+
$key_is_ascii = mb_check_encoding($key, 'ASCII');
23+
if (strlen($key) <= 250 && $key_is_ascii) {
24+
return $key;
25+
}
26+
// Memcache only supports key lengths up to 250 bytes. If we have generated
27+
// a longer key, we shrink it to an acceptable length with a configurable
28+
// hashing algorithm. Sha1 was selected as the default as it performs
29+
// quickly with minimal collisions.
30+
// Return a string that uses as much as possible of the original cache ID
31+
// with the hash appended.
32+
$hash = hash($this->hashAlgorithm, $this->prefix . '-' . $key);
33+
if (!$key_is_ascii) {
34+
return $hash;
35+
}
36+
return substr($key, 0, 250 - strlen($hash)) . $hash;
37+
}
38+
39+
}

0 commit comments

Comments
 (0)