Jump to content

Cache seems do not work


LAPS
 Share

Recommended Posts

According to this old post, I'm trying to use the PW cache feature:

$my_page = $pages->get($pageId);

if($my_page->viewable()) {
  $content = $cache->get("my_page");
  if(!$content) {
    $content = renderCustom($my_page->children);
    $cache->save('my_page', $content, 3600);
  }
  echo $content; 
}


function renderCustom(PageArray $items) {
  foreach($items as $item) {
	echo $item->title;
    // ... heavy content loading
  }
}

After loading the page which renders the above code, in the admin I can see something was cached (note: I'm using ClearCacheAdmin) :

PW-ClearCacheAdmin-01.thumb.png.ef05286ceab04969d1f2c0e0a66e336e.png

But when I reload the page the cached value seems do not be used and the page loads slowly.

The result of doing var_dump() is the following:

var_dump($cache);
// return object(ProcessWire\WireCache)#20 (0) { }

$content = $cache->get("my_page");
var_dump($content);
// return string(0) ""

 

What could be the problem?

Link to comment
Share on other sites

function renderCustom(PageArray $items) {
  foreach($items as $item) {
	echo $item->title;
    // ... heavy content loading
  }
}

Is this just an example, or is your actual function also just echoing content? If so, that right there could be your problem — echo will output the content right way instead of returning it for assignment.

Try something like this instead:

function renderCustom(PageArray $items) {
  $out = "";
  foreach($items as $item) {
	$out .= $item->title;
    // ... heavy content loading
  }
  return $out;
}

 

  • Like 1
  • Thanks 1
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
 Share

×
×
  • Create New...