Jump to content

A custom cache key before rendering the page, using a built-in template cache engine


mav_medved
 Share

Recommended Posts

Hi all,

Keen on processwire. Thanks a lot for this thing!

But here is my issue:

I have a lot of pages that need to be cached. But the content on this pages depends on what user has in his session's variable (not a logged-in user, just a guest but with a different session's variable value).

So I need to add this session variable as a cache key before page will have been rendered and created the cache file.

How to do so?

I tried to find the solution, but there is no Hooks in wire/core/CacheFile.php

So, my thoughts:

1. add the Hook for method "buildFilename" in wire/core/CacheFile.php to have the possibility to change filename for cache file (or at least to add custom cache key for filename)

or

2. somehow do it using addHookBefore('Page::render'

For example: let's assume we have a special param like $options['custom_cache_key'] and we can change it inside Hook method (by the way how to do that?)

Link to comment
Share on other sites

hi mav_medved, welcome to the forum! :)

sounds like markupcache is the right thing for you:

https://processwire.com/talk/topic/7-new-markupcache-module/

http://modules.processwire.com/modules/markup-cache/

you could just create a cache-file bound to your session variable

$cache = $modules->get("MarkupCache");
if(!$data = $cache->get($your_session_var)) {
      $data = [...] // your code
      $cache->save($data);
}
echo $data;

don't know if or what procache has to offer in this case...

hope that helps :)

  • Like 1
Link to comment
Share on other sites

BernhardB, thanks for response!

A very nice module, I've found it very useful! Thanks for advice!

But I needed to make cache not for a part of the template, but whole template. (have some specific :))

So I came up with the solution, using this module:

(maybe someone will find it useful too)



<?
$cache = $modules->get("MarkupCache");
$cache_key = 'page_'.$page->id.'_myvar_'.$my_session_var;

if (!isset($options['render_for_cache'])){
$page->template->prependFile = '';
if (!$output = $cache->get($cache_key)){
$output = $page->render(array('render_for_cache' => 1, 'prependFile' => '', 'appendFile' => ''));
$cache->save($output);
}
echo $output;
}
else{
?>

<!-- here is my plain html/php markup -->

<?
}
?>

So now I can make a cache for any template with any cache_key.

I'm sure it's possible to make a Hook using "before render" for certain templates, so that can make cache and keep the template file clean. (or maybe not :D)

Anyway don't want to do it, ask me if someone need it ;)

  • Like 2
Link to comment
Share on other sites

nobody said it can't be used for the whole template ;)

hmm... nice. you could also handle everything in your template and outsource your markup to a different file:

yourtemplatefile.php

<?php
$config->session->myvar = 'test1'; // set session variable manually for testing

$cache = $modules->get("MarkupCache");
$cache_key = 'page_'.$page->id.'_myvar_'.$config->session->myvar;

if (!$output = $cache->get($cache_key)){
    $output = wireRenderFile('_markup.php');
    $cache->save($output);
}
echo $output;

_markup.php

<html>

[...]

<?= $page->title ?><br>
<br>
<?= $config->session->myvar ?>

[...]

</html>

so all your templates that are using "yourtemplatefile.php" will have cached output based on your session key. don't know how this compares to templatecache regarding performance but would be interesting to know :)

  • Like 1
Link to comment
Share on other sites

No 'real benchmarks' as in repeated tests on X different scenarios. Original usecase (and one measured) is a template listing shop items — ~40 products — where only difference in the rendered HTML is an input value - which is either default '0' or anything else picked up from $post or session.

It's a rather slow server, but load time for that page dropped from ~2.5s to ~450ms, which is the same as template caching (roughly), but usable on every page load, not only the first one.

I use MarkupCache (which in turn uses the same caching mechanism as the template-level caching), so there is maybe three or four function calls 'overhead' versus the template-level caching — which is nothing (I might be wrong, haven't been digging in that code lately).

---

There is a number of improvements I would like to make, but was working on a different projects lately.

  • Like 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...