diff --git a/.htaccess b/.htaccess
index a69bdd4..550e9a9 100644
--- a/.htaccess
diff --git a/core/includes/cache.inc b/core/includes/cache.inc
index 5d82fa0..a6b3f7b 100644
--- a/core/includes/cache.inc
+++ b/core/includes/cache.inc
@@ -6,6 +6,16 @@
  */
 
 /**
+ * Indicates that the cached item contains serialized data.
+ */
+const CACHE_SERIALIZED = 1;
+
+/**
+ * Indicates that the cached item contains compressed data.
+ */
+const CACHE_COMPRESSED = 2;
+
+/**
  * Instantiates and statically caches the correct class for a cache bin.
  *
  * By default, this returns an instance of the DrupalDatabaseCache class.
@@ -506,10 +516,10 @@ class DrupalDatabaseCache implements DrupalCacheInterface {
    * Checks that items are either permanent or did not expire, and unserializes
    * data as appropriate.
    *
-   * @param $cache
+   * @param object $cache
    *   An item loaded from cache_get() or cache_get_multiple().
    *
-   * @return
+   * @return object|false
    *   The item with data unserialized as appropriate or FALSE if there is no
    *   valid item to load.
    */
@@ -530,7 +540,14 @@ class DrupalDatabaseCache implements DrupalCacheInterface {
       return FALSE;
     }
 
-    if ($cache->serialized) {
+    if (($cache->serialized & CACHE_COMPRESSED) != 0) {
+      if (!function_exists('gzuncompress')) {
+        // We cannot decompress the compressed cache data, ignore it.
+        return FALSE;
+      }
+      $cache->data = gzuncompress($cache->data);
+    }
+    if (($cache->serialized & CACHE_SERIALIZED) != 0) {
       $cache->data = unserialize($cache->data);
     }
 
@@ -542,17 +559,23 @@ class DrupalDatabaseCache implements DrupalCacheInterface {
    */
   function set($cid, $data, $expire = CACHE_PERMANENT) {
     $fields = array(
+      'data' => $data,
       'serialized' => 0,
       'created' => REQUEST_TIME,
       'expire' => $expire,
     );
+
+    // Store the data to cache in a string field.
     if (!is_string($data)) {
-      $fields['data'] = serialize($data);
-      $fields['serialized'] = 1;
+      $fields['data'] = serialize($fields['data']);
+      $fields['serialized'] |= CACHE_SERIALIZED;
     }
-    else {
-      $fields['data'] = $data;
-      $fields['serialized'] = 0;
+
+    // Big data chunks can be compressed to reduce e.g. network and database
+    // resource usage at the expense of increased CPU resource usage.
+    if (strlen($fields['data']) > variable_get('cache_compress_threshold', PHP_INT_MAX) && function_exists('gzcompress')) {
+      $fields['data'] = gzcompress($fields['data'], 9);
+      $fields['serialized'] |= CACHE_COMPRESSED;
     }
 
     try {
