diff --git a/core/lib/Drupal/Core/Cache/PhpBackend.php b/core/lib/Drupal/Core/Cache/PhpBackend.php
index b4014c5..0729be9 100644
--- a/core/lib/Drupal/Core/Cache/PhpBackend.php
+++ b/core/lib/Drupal/Core/Cache/PhpBackend.php
@@ -254,7 +254,7 @@ public function removeBin() {
    *   The cache item to store.
    */
   protected function writeItem($cid, \stdClass $item) {
-    $data = str_replace('\\', '\\\\', serialize($item));
+    $data = addcslashes(serialize($item), '$\\');
 
     // Data can contain 'EOF' or 'EOF;', either of which cause a fatal PHP
     // error when the cache item is read back from disk. To guard against this,
diff --git a/core/modules/system/lib/Drupal/system/Tests/Cache/GenericCacheBackendUnitTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Cache/GenericCacheBackendUnitTestBase.php
index 7aee6a5..0a9108c 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Cache/GenericCacheBackendUnitTestBase.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Cache/GenericCacheBackendUnitTestBase.php
@@ -181,6 +181,12 @@ public function testSetGet() {
     $this->assertTrue($cached->valid, 'Item is marked as valid.');
     $this->assertEqual($cached->created, REQUEST_TIME, 'Created time is correct.');
     $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
+
+    $with_variable = array('foo' => '$bar');
+    $backend->set('test6', $with_variable);
+    $cached = $backend->get('test6');
+    $this->assert(is_object($cached), "Backend returned an object for cache id test6.");
+    $this->assertIdentical($with_variable, $cached->data);
   }
 
   /**
