You are here

protected function FileSystemBackend::normalizeCid in File Cache 8

Normalizes a cache ID in order to comply with file naming limitations.

There are many different file systems in use on web servers. In order to maximize compatibility we will use filenames that only include alphanumeric characters, hyphens and underscores with a max length of 255 characters.

Parameters

string $cid: The passed in cache ID.

Return value

string An cache ID consisting of alphanumeric characters, hyphens and underscores with a maximum length of 255 characters.

1 call to FileSystemBackend::normalizeCid()
FileSystemBackend::getFilename in src/Cache/FileSystemBackend.php
Returns the filename for the given cache ID.

File

src/Cache/FileSystemBackend.php, line 282

Class

FileSystemBackend
A cache backend that stores cache items as files on the file system.

Namespace

Drupal\filecache\Cache

Code

protected function normalizeCid($cid) {

  // Nothing to do if the ID is already valid.
  $cid_uses_valid_characters = (bool) preg_match('/^[a-zA-Z0-9_-]+$/', $cid);
  if (strlen($cid) <= 255 && $cid_uses_valid_characters) {
    return $cid;
  }

  // Return a string that uses as much as possible of the original cache ID
  // with the hash appended.
  $hash = Crypt::hashBase64($cid);
  if (!$cid_uses_valid_characters) {
    return $hash;
  }
  return substr($cid, 0, 255 - strlen($hash)) . $hash;
}