Jump to content

Delete and generate new markup cache


fbg13
 Share

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?

Link to comment
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
Link to comment
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?

Link to comment
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
Link to comment
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.

Link to comment
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?

Link to comment
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
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...