Magento – EE 1.14.2 / CE 1.9.2 block caching update has non-unique cache keys – duplicate content showing on frontend

cachece-1.9.2.0duplicateee-1.14.2.0static-block

When I upgraded to EE 1.14.2, most things went smoothly but I came across an issue when I started to check my various frontend pages. I have a catalog node with several subcategories and each of those has a different static block showing on them. After the upgrade, whichever page was hit first after a cache flush would end up showing on all of the different pages.

I don't know if this same issue will be present when CE 1.9.2 is released but I wanted to put my solution here for those who may find this same issue.

UPDATE: As confirmed here the same problem came up in CE 1.9.2

Best Answer

Since this was EE, I was able to utilize Magento support but I also worked things out on my own to help focus the issue and get a solution as fast as possible. The code changes were provided by Magento so applying them to the actual app/code/core files is fine though you could always duplicate the files in your /app/code/local and apply the changes there.

The issue was that the block caching method that was added in 1.14.2 was not generating a unique cache key so when I had multiple blocks used in the category controller space, the generated cache key ended up being unique only for the first page hit, resulting in all of those pages to show duplicate content.

The fix was to add the following (displayed in diff file format to show the context surrounding the additions - just add in the lines with the + where they need to go):

In app/code/core/Mage/Cms/Block/Block.php at line 72:

         }
         return $html;
     }
+
+    /**
+     * Retrieve values of properties that unambiguously identify unique content
+     *
+     * @return array
+     */
+    public function getCacheKeyInfo()
+    {
+        $blockId = $this->getBlockId();
+        if ($blockId) {
+            $result = array(
+                $blockId,
+                Mage::app()->getStore()->getCode(),
+            );
+        } else {
+            $result = parent::getCacheKeyInfo();
+        }
+        return $result;
+    }
 }

In app/code/core/Mage/Cms/Block/Widget/Block.php at line 82:

                 $helper = Mage::helper('cms');
                 $processor = $helper->getBlockTemplateProcessor();
                 $this->setText($processor->filter($block->getContent()));
+                $this->addModelTags($block);
             }
         }

         unset(self::$_widgetUsageMap[$blockHash]);
         return $this;
     }
+
+    /**
+     * Retrieve values of properties that unambiguously identify unique content
+     *
+     * @return array
+     */
+    public function getCacheKeyInfo()
+    {
+        $result = parent::getCacheKeyInfo();
+        $blockId = $this->getBlockId();
+        if ($blockId) {
+            $result[] = $blockId;
+        }
+        return $result;
+    }
 }

I wouldn't think I'd be the only one to see this issue and if it shows up in CE 1.9.2, hopefully this will help resolve it for some people.

Related Topic