Dynamically or automatically created cache?

I have a project in which reports and overviews are generated after a login. On a handful of pages.

Data sets can be added and others can disappear, all dynamically.

Now after some time I have the problem that the data sets are getting bigger and the loading times are correspondingly increasing.

MODX caching would be exactly the right thing. But I haven’t found a solution yet to automatically generate the cache for certain pages from modx.

I cannot install Bob’s plugin “RefreshCache” under Modx3.

Does anyone have any ideas or suggestions?

How do you generate the data that takes a long time to process?

If it’s in a snippet (or PHP code) you could use the MODX cache manager in your code to create/load a cached file.

There is also the extra getCache that basically does the same.

The data is brought together and processed from various sources. For example, a part comes from external calendars and is then expanded with data from the database. Which are then processed or filtered depending on the information provided by the user. Or maps can also be displayed and prepared in Openstreetmap.

Using PHP in snippets but also static, jquery, and so on. The system has grown quite complex.

If I take the page that has the longest loading time and have the cache active for this output (i.e. snippets cached and so on) I go from 10 seconds to under 1 second.

However, data sets can change constantly and even without me noticing, i.e. in external sources. That’s why the pages should be “preloaded” in the cache… and then e.g. Reload every 15 minutes.

There exists a function $resource->clearCache(); to clear only the cache file of a specific resource.

You could make everything in your resource cached and then create a cronjob that periodically deletes the resource cache file and requests the resource again (to recreate the cache file).

A different approach is to cache only certain parts of your page for a some time (and not the whole resource).

Many snippets for example work like this:

// try to load the data from the cache
$cached = $modx->cacheManager->get($key, $options);
if (empty($cached) { // no data in the cache
	// recreate the data
	$cached = ...;
	// cache the data for a certain time span
	$modx->cacheManager->set($key, $cached, $lifetime, $options);
return $cached;

You call the snippet always uncached, but if the data already exists in the cache, it can immediately be returned without the need to process it first.

You can even store the data in a custom cache partition, so that the data won’t get deleted when the ‘normal’ MODX cache is cleared.

So you could (for example) create a cronjob that repeatedly loads the data from an external source and stores it in the cache, and then write a snippet that uses this cached data (and only loads it if no data is available).

You might also look at the CacheManager extra. It clears the cache for the current resource when saving it in the Manager, without clearing the cache for any other resources. If that doesn’t help, you might be able to adapt some of its code to do what you want.

Also, can you explain what happened when you tried to install RefreshCache in MODX 3?

Sorry for my late reply. It’s a holiday in Germany today and I was out with the family.

@halftrainedharry ok, I’ll take a look at it tomorrow and test it out a bit.

@bobray When I want to install the plugin the button “Install all dependent packages to continue” disabled and I can’t do anything anymore. But I don’t have any entries in the log file either.

This is a (known) bug in MODX 3.0.3.

The package that is required for RefreshCache seems to be “Guzzle6”:

Ok the installer automatically installed guzzle7.

But “guzzle6” cannot be loaded via the package manager.

In the overview this is listed as version 0.0.0 of bobray, released 1970-01-01.

Some months ago they made changes to the extras API and the same happened to a few other extras.
I don’t know what the exact cause is.

Maybe @bobray has an idea.

I have now solved the problem differently.

@bobray but it’s still a shame that you can’t install the module at the moment. You can’t display Guzzel6 among the extras on the modx page either.

I think this is my fault. I removed or deprecated the Guzzle6 package at Lean, Flexible, Community-Made Extensions | MODX Extras because it had a security issue and is out of date. I switched other packages to Mark Hamstra’s Guzzle7 extra, but forgot that Guzzle6 was used by RefreshCache.

I’ll try to find time to work on an updated version of RC.

In the meantime, you might try just installing Mark’s Guzzle7 package. It should autoload the classes once installed. Comment out or remove line 11 of the core\components\refreshcache\processors\refresh.class.php file. This line:

require_once MODX_CORE_PATH . 'components/guzzle6/vendor/autoload.php';

I’ve been working on the extra, but so far it refuses to work in MODX 3 and I can’t figure out why.

1 Like