Jump to content

Extend cache for dynamic pages

Recommended Posts

I'm reusing pages depending on the url of the site and want to cache these pages.

For example example.com/blog/1 and example.nl/blog/1 would use the same template but i'm extending the Page to limit the results depending on url.

Is there a way that i can append a name to the cache name used to cache this page?
It seems it should be easy to hook in the cache rending and change it but i could not find anything helpfull.

Share this post

Link to post
Share on other sites

It seems that i need to set $cacheFile->setSecondaryID after Pagerender.module has set it so i don't affect page id's and translation.

If someone know how to do this properly any help is much apreciated!

Share this post

Link to post
Share on other sites

Is there no one who can help with this? Am i asking something very difficult?
It's probably not that helpfull for others, but it didn't look that hard to do. I just don't know how to do it.

-edit: some text

Share this post

Link to post
Share on other sites

Are these domains for the same site? 

You can try to use markup cache module and $cache for these pages where you have some dynamic content. 

Share this post

Link to post
Share on other sites

The domains go to the same site/server.

I wanted to avoid unnecessary complexity in my template code (i'm just a hobby programmer and more of a designer). Also it allows me to change it from the admin panel, which removes the necessity of re-uploading files.

But i see that your route is a better way to go, then to wait for someone to help me with this niece case.

Sorry for being a bit impatient with things. I can't sit long behind my desk and am long past my deadline, but that's my problem.

Share this post

Link to post
Share on other sites

Markup cache is basically useless for me, i'm going to edit the Pagerender.module instead.

Other suggestions are welcome

Share this post

Link to post
Share on other sites

You where right LostKobrakai, I was to stubborn to really listen and take advantage of your advice.

Reading up about it the first time made it seem like it would work fine in my case, bringing it in to practice was a whole other thing. Saving pages to cache resulted in returning only id's after saving and sometimes a piece of code worked but a minute later it didn't, so i got really frustrated and  just gave up after a while.

I took a fresh look a few days later and I could fix the id problem (LazyLoading) by using $pages->findOne instead of $pages->get. Also i noticed pages where being cached somehow even if i turn it off by using  // FileCompiler=0 in the top of the file, or using the following in config.php

$config->debug = true;
if ( $config->debug == true ) {
    $config->fileCompilerOptions = array(
        'siteOnly' => false,
        'showNotices' => true,
        'logNotices' => false,
        'exclusions' => array('php', 'phtml'),
        'extensions' => array('php', 'module', 'inc', 'phtml'),
    $config->dbCache = false;
    $config->moduleCompile = true;
    $config->templateCompile = false;

Strangely the error page show's the updated code but still thinks there is an error.

So i'm sorry for my little outburst in the comment above. Although it seemed simpler to set the secondaryID in my mind, this solution is a whole lot more powerful.

Share this post

Link to post
Share on other sites

You disabled everything for the fileCompiler, but wireCache and the fileCompiler are two totally different things. The first one is a multiple purpose cache for data you handle in your code. The fileCompiler is there to dynamically add the ProcessWire namespace needed in php files since pw 3.0, a.k.a. it's literally changing your php files and is not a cache for data at all.

You could do this:

// in config.php
$config->cacheTimeMultiplier = 1
if($config->debug) $config->cacheTimeMultiplier = 0

// elsewhere
echo $cache->get("hello-world", 3600 * $config->cacheTimeMultiplier, function() {
  echo "<p>Hello World</p>";


  • Like 2

Share this post

Link to post
Share on other sites

I commented out the wire cache during testing (by commenting out the code), so no problem there. Thanks for letting me know the filecompiler stuff is unnecessary, i already namespace my files so it's only needed for modules.

I already gave up on the cache problem.

edit: grasping at straws

Share this post

Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By louisstephens
      So I ran into a very strange issue today. I have a template with a pagetable and I went to add an item to it, when I went to select an image (for an image field) the page instantly threw up an error
      "ProcessPageSearchLive: No search specified"
      The page's content also switched to the image attached. This all worked perfectly last week (local mamp box). Has anyone experienced this before, and how did you solve it?

    • By Pip
      Hi everyone, 
      Is there a way for us to replicate the "http://mydomain.com/processwire/page/edit/?id=xxx" and change the template to match my site template? 
      I'm terribly happy with the admin / backend page edit. It covers literaly everything I want to empower my non super admin user in updating pages such as validation, repeater management, file upload. 
      I wish not to allow the users to see the backend for both security and aesthetic reasons. 
      Thanks and hope to hear from you soon. 
    • By MoritzLost
      Cacheable Placeholders
      This module allows you to have pieces of dynamic content inside cached output. This aims to solve the common problem of having a mostly cacheable site, but with pieces of dynamic output here and there.  Consider this simple example, where you want to output a custom greeting to the current user:
      <h1>Good morning, <?= ucfirst($user->name) ?></h1> This snippet means you can't use the template cache (at least for logged-in users), because each user has a different name. Even if 99% of your output is static, you can only cache the pieces that you know won't include this personal greeting. A more common example would be CSRF tokens for HTML forms - those need to be unique by definition, so you can't cache the form wholesale.
      This module solves this problem by introducing cacheable placeholders - small placeholder tokens that get replaced during every request. The replacement is done inside a Page::render hook so it runs during every request, even if the response is served from the template cache. So you can use something like this:
      <h1>Good morning, {{{greeting}}}</h1> Replacement tokens are defined with a callback function that produces the appropriate output and added to the module through a simple hook:
      // site/ready.php wire()->addHookAfter('CachePlaceholders::getTokens', function (HookEvent $e) { $tokens = $e->return; $tokens['greeting'] = [ 'callback' => function (array $tokenData) { return ucfirst(wire('user')->name); } ]; $e->return = $tokens; }); Tokens can also include parameters that are parsed and passed to the callback function. There are more fully annotated examples and step-by-step instructions in the README on Github!
      A simple and fast token parser that calls the appropriate callback and runs automatically. Tokens may include multiple named or positional parameters, as well as multi-value parameters. A manual mode that allows you to replace tokens in custom pieces of cached content (useful if you're using the $cache API). Some built-in tokens for common use-cases: CSRF-Tokens, replacing values from superglobals and producing random hexadecimal strings. The token format is completely customizable, all delimiters can be changed to avoid collisions with existing tag parsers or template languages. Links
      Github Repository & documentation Module directory If you are interested in learning more, the README is very extensive, with more usage examples, code samples and usage instructions!
    • By EyeDentify
      Hello dear PW gurus.

      I have stumbled over a strange error that i all of sudden got when trying to upload an image to a images field on a page.
      There where images allready stored in the field that i wanted to keep, but during the upload the error apear and after that all images are gone from the field and i can´t upload any, i just get the error every time.
      I am running ProcessWire 3.0.153 dev.
      After looking in the assets folder i find the folder for the page and the image files seems to be there including the ones i tried to upload when the error occured.
      But they don´t show up in the images field in the page editor.
      The error reported:
      SQLSTATE[01000]: Warning: 1265 Data truncated for column 'ratio' at row 1 And here is a screenshot of the event:

      The TracyDebugger Error reporting:

      I hope you fine folks could point me in a direction.
      But it seems our old pal set_time_limit() is back.

      Regards, EyeDentify
    • By Spyros
      I'm having a strange issue with the $page->find(), for some reason I'm missing some of the pages from the results. I found then that I was missing all the pages with the same "PAGE NAME". Is it a bug or am I missing something?
      If I change the "PAGE NAME" of one of the missing ones then I'm retrieving the page without any problem.
      Thank you
  • Create New...