On 7 April, testing a new ERP pricing add-on for a Swiss B2B store, I found one customer's personal price shown to another customer. A customer with personal prices saw the guest price until the cache was flushed. The price table sat in a cached block, and its key, added a year and a half earlier for page speed, had neither the customer nor the customer group in it. The add-on never went live on the production shop.
The leak came from the cache keys. Magento caches at two levels, and they split by different things.
The page cache splits by group
In Magento 2.4.9 the full page cache stores a page under HTTP or HTTPS, the URL, the sorted query and the X-Magento-Vary cookie. That cookie is a hash of the HTTP context: by default the customer group and whether the visitor is logged in. Other values, such as a non-default currency or tax rates, join it only when they're set.
Three customers of the same group got one cached page with the same render timestamp. A customer of another group got a page of their own. The page cache splits by group, not by customer.
Personal data goes around the page
Magento keeps what only one customer may see out of the cached HTML and loads it by AJAX from customer/section/load. For the same cached page, that call returned "Anna Alpha" to one test customer and "Bert Beta" to another, and the page itself had neither name in it.
That holds for Magento's own sections. A personal price from an add-on needs a section of its own, or another way around the cached HTML.
A block inside the page has its own key
A block is cached only when getCacheLifetime() returns a lifetime. By default that's the cache_lifetime value, but a class can override it, and both Magento's price box and the price table in the story did. A template block's default key is the store code, the template file, the base URL and the template name. Nothing about the visitor.
With that default key, the test block kept whatever the first request had rendered. Inside a cached page, with a guest first, every logged-in group got the guest price. On a page without the full page cache, customer A's personal price went to B, to C and to a guest.
The fix that doesn't work on a cached page
The obvious fix is the customer ID in the block's key. On a page without the full page cache, that worked: 5 visitors, 5 entries, each with the right price.
On a cacheable page it changed nothing. After Magento generates the layout of such a page, it clears the customer from the session and keeps the group. That keeps one customer's data out of a page the whole group shares. Every visitor's block key got customer ID 0, and the leak stayed. Magento's final price box keys by customer group.
By default a cached page still knows the group, plus the store, the currency and the other context values. Anything finer needs a private section, a page that isn't cached, or a per-customer value in the HTTP context, which splits the page cache by customer.
public function getCacheKeyInfo() { // on a cacheable page the session keeps only the group: // getCustomerId() is null there, so a personal list shows the group price $list = $this->personalPrice() !== null ? 'customer:' . (int) $this->customerSession->getCustomerId() : 'group:' . (int) $this->customerSession->getCustomerGroupId(); return parent::getCacheKeyInfo() + ['price_list' => $list]; } What I deployed to the test server
A key per customer for every product multiplies customers by products, and most of those entries would be duplicates. The fix I deployed to the development server on 14 April keyed the price table by store and customer group, and added the customer ID only for products where some customer had a personal price. It read the customer from the session, so it only held where the page itself wasn't cached.
The same change replaced the add-on's page cache plugin. Its condition read if ($customer_id = …->getId() > 0). In PHP, > binds before =. $customer_id became true, and every logged-in customer got the same suffix, "1".
// > binds before =: $customer_id = (getId() > 0) = true, suffix "1" for every logged-in customer if ($customer_id = $this->customerSession->getCustomer()->getId() > 0) { // what was meant if (($customer_id = $this->customerSession->getCustomer()->getId()) > 0) { 7 to 14 April took about 20 hours: the cache fix, two price-data bugs, a disappearing quantity field and the deploy. In September these overrides of the add-on were reverted together.
Laravel: the key is all yours
Laravel's core has no full page cache. The cache.headers middleware sets response headers and answers 304 on a matching ETag. It stores nothing. With the database store, Cache::remember() stores exactly the key you pass, plus the store's prefix. Without the user ID in it, user B got user A's value. With the ID, each got their own.
Nothing clears the user before your code runs. Once the auth middleware has run, Auth::id() is there when you build the key. The other half is the same as in Magento: nobody adds it for you.
// database store: saved as the store's prefix + this key, nothing else added $html = Cache::remember( "price_block_{$product->id}_user_" . Auth::id(), 600, fn () => view('prices.table', ['product' => $product])->render(), ); The rule
If the output depends on who's asking, the key has to say who, at the coarsest level that's still correct: group, price list or customer. In Magento, check what the page still knows when the block renders. On a cached page, by default, that's the group.
Next: multi-store is built in, Magento's scopes next to Laravel's tenancy packages.
What's the coarsest correct level in your cache keys: user, group, tenant or plan?
Part 12 of 15 of the Magento 2 vs Laravel series. Originally published on LinkedIn.
