MODX 2.8.4: How to refresh cache for a specific resource ID?

Hello,

I am creating programmatically a new resource via formit and custom hooks.
Also in this process I create a custom alias stored in resource data set.

But, wenn ich get data via getPage and getResource the alias not set.
Only after clearing the main cache via Manager.

In tpl the URL should generated via [[+pagetitle]]

I use $doc->clearCache(); after saving, but without success.

Any idea what I am doing wrong?

Thank you in advance.

bye
Chris

I believe you have to clear the whole cache with

$modx->cacheManager->refresh();

$doc->clearCache(); probably only deletes the corresponding file in core/cache/resource. But there is additional information in core/cache/context_settings (like “aliasMap”, “resourceMap”) that also has to be updated.

oh, thank you.
is it possible to refersh only for this resource?
Because if there are many new resources created every time all cache is cleared. So, the performance of whole website then suffers…

Well, I’m not an expert. But as far as I can tell, the information in core/cache/context_settings is an aggregate of the whole context information. So you probably have to clear at least the data from the context the new resource is in, if just calling $doc->clearCache(); doesn’t work.

Also, it’s worth checking if getPage/getResources internally caches the data. Maybe it’s enough to just clear that cache.

that works.

thank you very much :pray:

The CacheMaster extra will clear the cache for just one resource when you save it rather than clearing the whole cache.

Be aware, though that just clearing the resource cache might not be enough, because the cache for TVs and other things might also need clearing when the resource changes.

Thank you Bob.

I saw, that not the cache of the special resource was the problem.
I think the problem was the aliasmap cache.
Is it possible to add the specific entry to this cache for better performance?

thank you in advance

If the alias map is the issue (and that’s fairly common) try this:

$modx->reloadContext($modx->context->get('key'));

or, if you know the context key, something like this:

$modx->reloadContext('web');

wow. cool. this works.

thank yo very much :pray:

Glad I could help. :wink:

Just a quick add on to this, you can unlink() a specific cache file programmatically since all the cached resources are saved with the same nomenclature in the same place (this location changed in v3, so updating your site would require a script change). In v2 the path is core/cache/resource/[context]/resources/[id].cache.php so you could, as an example:

unlink(MODX_BASE_PATH.'core/cache/resource/web/resources/56.cache.php');

Of course, this will not clear any navigation or stored lists this resource might appear in, but it will remove that specific resource cache and force it to refresh on the next view.

@claytonk Calling the function clearCache() on the resource should do the same and is the cleaner solution in my opinion.