-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dennisinteractive/27724_lock_sanitisation
Case 27724; Updated MemcacheBacked lock sanitisation.
- Loading branch information
Showing
3 changed files
with
48 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Drupal\memcache; | ||
|
||
/** | ||
* Class MemcacheCacheNormalizer. | ||
*/ | ||
trait MemcacheCacheNormalizerTrait { | ||
|
||
/** | ||
* Normalizes a cache ID in order to comply with key length limitations. | ||
* | ||
* @param string $key | ||
* The passed in cache ID. | ||
* | ||
* @return string | ||
* An ASCII-encoded cache ID that is at most 250 characters long. | ||
*/ | ||
protected function normalizeKey($key) { | ||
$key = urlencode($key); | ||
// Nothing to do if the ID is a US ASCII string of 250 characters or less. | ||
$key_is_ascii = mb_check_encoding($key, 'ASCII'); | ||
if (strlen($key) <= 250 && $key_is_ascii) { | ||
return $key; | ||
} | ||
// Memcache only supports key lengths up to 250 bytes. If we have generated | ||
// a longer key, we shrink it to an acceptable length with a configurable | ||
// hashing algorithm. Sha1 was selected as the default as it performs | ||
// quickly with minimal collisions. | ||
// Return a string that uses as much as possible of the original cache ID | ||
// with the hash appended. | ||
/** @var DrupalMemcacheConfig $this->settings */ | ||
$hash_algorithm = $this->settings->get('key_hash_algorithm', 'sha1'); | ||
$hash = hash($hash_algorithm, $key); | ||
if (!$key_is_ascii) { | ||
return $hash; | ||
} | ||
return substr($key, 0, 250 - strlen($hash)) . $hash; | ||
} | ||
|
||
} |