Jump to content

MarkupCache "nested" ERROR


Recommended Posts

Hi there,

I've optimized a search for items with MarkUpCache for all predefined searches (e.g. like a category) – that did work well so far using code [A] in my search template file.

Today I tried to not only cache the list in its whole markup, but in addition each item in its separate cache-file. [A] and [B]

On first load, it leads to an error:
You must attempt to retrieve a cache first, before you can save it. (in /wire/modules/Markup/MarkupCache.module line 120)
- I can see the single items cache files are generated (item-####), but the cache dir results-for-#### is empty

Reloading the (search)page subsequently runs without error, but still no cache file for the list in results-for-####

Where is my mistake? I'm stuck – would be pleased about a tip.


// [A] ---------------
//@search tpl file.php

$cache = wire("modules")->get("MarkupCache");

if (!$data = $cache->get("results-for-###", 3600)) {
    $data = renderResultList($foundpages);
//using $data for output
echo $data;

// [B] ---------------
//render a result list of items

function renderResultList($items) {
    $out = '';
    // cycle through all the items
    foreach ($items as $item) {
        $out .= renderResultListItem($item);
    return $out;

//render a single item
function renderResultListItem($item) {

    $cache = wire("modules")->get("MarkupCache");
    $cachefile = "item-{$item->id}";

    if (!$data = $cache->get($cachefile, 3600)) {

        $data = $item->title;
        //…add more markup and data

    return $data;


Link to comment
Share on other sites

  • 2 weeks later...

The way MarkupCache works simply does not allow for this nested use, IIRC. My advice is to just use the database cache (class WireCache, available as $cache or cache()), because it’s much easier to use and understand and probably just as fast. Obviously the speed depends on your setup, but if the database is on the same system and fits into memory it’s probably even faster than the filesystem.

Also I would only cache the individual items, since renderResultList() doesn’t add anything interesting. It’s just going to complicate invalidation and bloat the cache table. With $cache you can preload all items in one roundtrip to the database, that should suffice. For more speed gainz you can always look into ProCache.

As a sidenote, you’re overriding the WireCache $cache variable with MarkupCache, which may be a source of mistakes to watch out for.

  • Like 1
Link to comment
Share on other sites

Hello @Jan Romero, thank you for the clarification. Very helpful. I could add the individual items to WireCache now. 

As far as the resultlist is concerned, there are unfortunately still a few things going on. The list consists of several sequential finds() and there is a tag list as an isotope filter-menu*, ... so I'm leaving  the MarkUpCache continue to run for now.

A very special thanks for your sidenote... that explains many a… I wasn't aware of what I was doing, and yes indeed I had problems with WireCache after hijacking it.

*while trying to find the bottleneck(s) with profilerPro I found one exceptional hungry candidate, but that's another topic.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...