diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php
index 06c5dc2..02a18a2 100644
--- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php
+++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php
@@ -407,7 +407,7 @@ protected function catchException(\Exception $e, $table_name = NULL) {
    */
   protected function normalizeCid($cid) {
     // Nothing to do if the ID is a US ASCII string of 255 characters or less.
-    $cid_is_ascii = mb_check_encoding($cid, 'ASCII');
+    $cid_is_ascii = $this->isAscii($cid);
     if (strlen($cid) <= 255 && $cid_is_ascii) {
       return $cid;
     }
@@ -421,6 +421,36 @@ protected function normalizeCid($cid) {
   }
 
   /**
+   * Checks to see if a given string is only made up of ASCII characters.
+   *
+   * This uses mb_check_encoding if that's present, otherwise this falls back
+   * to preg_match.
+   *
+   * @param string $text
+   *   The text to check.
+   *
+   * @return boolean
+   *   True if $text only contains ASCII characters, otherwise false.
+   */
+  protected function isAscii($text) {
+    if ($this->mbStringExists()) {
+      return mb_check_encoding($text, 'ASCII');
+    }
+    else {
+      return preg_match('/^[[:ascii:]]+$/', $text);
+    }
+  }
+
+  /**
+   * Check if mb_check_encoding function is present.
+   *
+   * @return bool
+   */
+  protected function mbStringExists() {
+    return function_exists('mb_check_encoding');
+  }
+
+  /**
    * Defines the schema for the {cache_*} bin tables.
    */
   public function schemaDefinition() {
diff --git a/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php b/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php
index 934fe81..0494a0a 100644
--- a/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php
+++ b/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\system\Tests\Cache;
 
+use Drupal\Component\Utility\Crypt;
 use Drupal\Core\Cache\DatabaseBackend;
 
 /**
@@ -53,4 +54,99 @@ public function testSetGet() {
     $this->assertIdentical($cached_value_short, $backend->get($cid_short)->data, "Backend contains the correct value for short, non-ASCII cache id.");
   }
 
+  /**
+   * Tests that the DatabaseBackend normalizeCid works.
+   */
+  public function testCidNormalization() {
+
+    $cids = array(
+      // Ascii only, should return same.
+      array("cid" => "0000000000", "expected" => "0000000000"),
+      // Utf-8, should return hash.
+      array(
+        "cid" => "Some Danish characters: � �",
+        "expected" => "6Q10YnfEtF092na6OZOwuLniGFilOQgIPnam3IXjcBU"
+      ),
+      // Ascii, over 255 characters long, should return hash padded with
+      // original string to a 255 length.
+      array(
+        "cid" => str_repeat("0", 256),
+        "expected" => "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000Z_AiGV7kBRQpaMobU64lE6i6sEBNcFd3hTFvqVIY6Lo"
+      ),
+      // Utf-8, over 255 long, should return a hash.
+      array(
+        "cid" => "Some Danish characters: � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �",
+        "expected" => "KbjIhg-2SJ5fjtlNol8DnT_tJDr65kcJsq9cpgLSVJ0"
+      ),
+    );
+
+    $db_cache = new MockedDatabaseBackend($this->container->get('database'), $this->container->get('cache_tags.invalidator.checksum'), 'test');
+
+    $db_cache->setMbString(FALSE);
+    foreach ($cids as $cid) {
+      $this->assertIdentical(
+        $cid["expected"],
+        $db_cache->normalizeCid($cid["cid"]),
+        "Assert that cid is normalized as expected."
+      );
+    }
+
+    // Forces Preg usage.
+    $db_cache->setMbString(TRUE);
+    foreach ($cids as $cid) {
+      $this->assertIdentical(
+        $cid["expected"],
+        $db_cache->normalizeCid($cid["cid"]),
+        "Assert that cid is normalized as expected."
+      );
+    }
+
+  }
+
+}
+
+class MockedDatabaseBackend extends DatabaseBackend {
+
+  /**
+   * @var bool
+   *  mocked value for mbStringExists().
+   */
+  protected $mbString;
+
+  /**
+   * Sets the value for mbString
+   *
+   * @param $mbString
+   */
+  public function setMbString($mbString) {
+    $this->mbString = $mbString;
+  }
+
+  /**
+   * Check if mb_check_encoding function is present.
+   *
+   * @return bool
+   */
+  protected function mbStringExists() {
+    return $this->mbString;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function normalizeCid($cid) {
+    // Nothing to do if the ID is a US ASCII string of 255 characters or less.
+    $cid_is_ascii = $this->isAscii($cid);
+    if (strlen($cid) <= 255 && $cid_is_ascii) {
+      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_is_ascii) {
+      return $hash;
+    }
+    return substr($cid, 0, 255 - strlen($hash)) . $hash;
+  }
+
 }
