Jump to content
fbg13

Delete and generate new markup cache

Recommended Posts

I'm caching a big loop (~1000 pages in a pagetable field) and i want to delete it when a new page is added and after that i want to recreate it with php to avoid users waiting for 10 seconds.

I figured out the deletion part 

$path = $config->paths->assets."cache/MarkupCache/cache_name/";
if(file_exists($path)){
	CacheFile::removeAll($path, true);
}

How can i rebuild the cache with php?

Share this post


Link to post
Share on other sites

I'm using this in a module

public function init() {
	$this->addHookBefore("Pages::saveReady", $this, "cacheContent");
}

protected function cacheContent($e) {
	$page = $e->arguments[0];
	if($page->template->name == "templateYouWantToCache") {
		$cache = $this->cache;
		// delete the old cache
		$cache->delete("cacheName:" . $page->id);
		$cache->get("cacheName:" . $page->id, $cache::expireNever, function() use($page){
			// code you want to cache
			$content  = $page->title . "<br>";
			$content .= $page->body; 
			return $content;
		});
	}
}

And in the template 

// since the cache already exists this will just get the cache
// but in case no cache is found it generates it
$cache->get("cacheName:".$page->id, $cache::expireNever, function() use($page){
	// code should be the same as the one that preloads the cache
	$content  = $page->title . "<br>";
	$content .= $page->body; 
	return $content;
});

 

  • Like 1

Share this post


Link to post
Share on other sites

I forgot to add an expiration time.

I updated the code so that the cache never expires.

Share this post


Link to post
Share on other sites

The module where i generate the cache is autoloading and whenever i restart my pc and access a page that should be cached, and it is cached as i can see the entry in the caches table, the caching functions still executes.

If I refresh the page, it loads fast, 0.5 seconds compared to 13 seconds.

If I open the page in another browser the same happens, first page access doesn't load the cache, but creates it again, even though it's in the database.

Is this a bug or am i missing something?

Share this post


Link to post
Share on other sites

I assume you are talking about the frontend hence using the latter code in your example. According to the docs (see this example as well) on $cache->get(), it doesn't say whether a WireCache::expire* constant can be used in that combined get and save of cache, unlike in $cache->save() docs where it is explicit that those constants are allowed. Just guessing here, maybe try it like in the $cache->get() docs? i.e. passing null instead of a constant:

$cache->get("cacheName:".$page->id, null, function() use($page){
	// code should be the same as the one that preloads the cache
	$content  = $page->title . "<br>";
	$content .= $page->body; 
	return $content;
});

Edit:

You can also see the differences in the code here (get) and here (save). save() accepts constants whereas get() doesn't seem to.

Edit 2:

I am also wondering whether in the template file you needed an explicit WireCache:expireNever rather than $cache::expireNever?

Edited by kongondo

Share this post


Link to post
Share on other sites

I changed the code in the template to only $cache->get("cacheName"); and left the cache generation to the hook for easier debugging.

Also I changed the constant to null and opened the page in a new browser, with the same result.

Will test it a little more, but i can't restart my pc right now.

Share this post


Link to post
Share on other sites

With null as expiration, after a restart, it still doesn't get the cache, but when opening the page in another browser (after it was loaded in another browser) it gets it on the first load. Same with no expiration or an integer.

And I forgot to ask, but why is the code inside the hook executed?

Share this post


Link to post
Share on other sites

Looking at your module code again, I notice this:

// @kongondo note: here you are deleting the cache, meaning...
$cache->delete("cacheName:" . $page->id);
// @kongondo note:...that here we don't have that cache and one will be created afresh
$cache->get("cacheName:" . $page->id, $cache::expireNever, function() use($page){
	// code you want to cache
	$content  = $page->title . "<br>";
	$content .= $page->body; 
	return $content;
});

The code inside the Hook is executed because by that point, you have no cache; you have deleted it. So $cache->get() doesn't get the cache and one is created afresh.

It seems to me that a Pages::saveReady is happening when you load your pages and that is triggering the cache to be deleted and recreated? But it doesn't make sense since when you reload, your page is reloaded from the cache. Are you sure about that? Have you checked the timestamps? Maybe confirm your workflow. Are pages being created via the API when you visit some page in the frontend? 

Edited by kongondo
more info
  • Like 1

Share this post


Link to post
Share on other sites
19 minutes ago, kongondo said:

Are pages being created via the API when you visit some page in the frontend? 

I'm so stupid :(.

I am saving the same page, I have a views field that gets incremented once per session.

Share this post


Link to post
Share on other sites

:-X:lol:...mystery solved :)

Edit:

Your OP stated that you needed the Hook to run when a new page is added. Why not Hook into that instead? i.e. 'Pages::added'

Edited by kongondo
suggestion
  • Like 1

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 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!
      Features
      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 MoritzLost
      Process Cache Control
      This module provides a simple solution to clearing all your cache layers at once, and an extensible interface to perform various cache-related actions.
      The simple motivation behind this module was that I was tired of manually clearing caches in several places after deploying a change on a live site. The basic purpose of this module is a simple Clear all caches link in the Setup menu which clears out all caches, no matter where they hide. You can customize what exactly the module does through it's configuration menu:
      Expire or delete all cache entries in the database, or selectively clear caches by namespace ($cache API) Clear the the template render cache. Clear out specific folders inside your site's cache directory (/site/assets/cache) Clear the ProCache page render cache (if your site is using ProCache) Refresh version strings for static assets to bust client-side browser caches (this requires some setup, see the full documentation for details). This is the basic function of the module. However, you can also add different cache management action through the API and execute them through the module's interface. For this advanced usage, the module provides:
      An interface to see all available cache actions and execute them. A system log and logging output on the module page to see verify what the module is doing. A CacheControlTools class with utility functions to clear out different caches. An API to add cache actions, execute them programmatically and even modify the default action. Permission management, allowing you granular control over which user roles can execute which actions. The complete documentation can be found in the module's README.
      Plans for improvements
      If there is some interest in this, I plan to expand this to a more general cache management solution. I particular, I would like to add additional cache actions. Some ideas that came to mind:
      Warming up the template render cache for publicly accessible pages. Removing all active user sessions. Let me know if you have more suggestions!
      Links
      https://github.com/MoritzLost/ProcessCacheControl ProcessCacheControl in the Module directory CHANGELOG in the repository Screenshots


    • By verdeandrea
      Hello,
      I am using ProCache v3.1.8 on ProcessWire 3.0.96.
      Everything worked fine in the past, but today I noticed that the css file serverd by procache gives a 410 error. 
      The file is there, I checked.
      I deleted the cached files, I deleted the css file, I looked into the .htaccess file looking for some clues about this problem but nothing worked.
      The only way i can see my website correctly again is disabling ProCache. 
      Has anyone any clue on what could be the cause of the problem or on what should I do to fix it?
      Thanks!
    • By abdulqayyum
      Hy Processwire community,
      There are some problem in fileCompiler cache.
      when i change under the directory \site\templates\ it must change under the directory /site/assets/cache/FileCompiler/site/templates/
      but it does not update and functionality working with /site/assets/cache/FileCompiler/site/templates/ directory.
       
      In this case please suggest me how i clear fileCompiler cache?
      what i have to clear it manually?
      Thanks AbdulQayyum.
    • By modifiedcontent
      I had upgraded my Apache configuration to include PHP7.2 and PHP7.3 for a Laravel-based script on the same server. Somehow it/I messed up a previously fine Processwire site, in a very confusing way.
      The site still looks fine, but editing template files has no effect whatsoever. It is stuck on some kind of cached version. I have already disabled PHP7's OPcache, cleared browser caches, etc, with no effect.
      The pages now apparently come from PW's assets/cache/FileCompiler folder, even though I never enabled template caching for this site.
      I have tried adding "namespace ProcessWire;" to the top of the homepage template file, but then I get this fatal error:
      My functions.php file pulls data in from another Processwire installation on the same VPS with the following line:
      $othersitedata = new ProcessWire('/home/myaccount/public_html/myothersite/site/', 'https://myothersite.com/'); That apparently still works fine; the site still displays data from the other installation, but via the "cached" template that I am now unable to change.
       
      I don't know where to start with this mess. Does any of this sound familiar to anyone? Any pointers in the right direction would be much appreciated. 
       
      Edit:
      Adding "$config->templateCompile = false;" to config.php results in the same fatal error as above. 
×
×
  • Create New...