Jump to content

How to programmatically clear the page cache?


PawelGIX
 Share

Recommended Posts

How to programmatically clear the page cache?
I have a page that uses URL Segments.
Based on the value of the URL Segment, the content of the page varies.
From what I can see, the cache can handle it without a problem.
But I have another question.
I need to create an API that will clean the cache of selected pages - based on the value of URL Segments or some other value.

Is there an API that will allow me to manage the page cache generated by PW?

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

In ProcessWire, you can clear the page cache programmatically using the $cache API object.

To clear the cache for a specific page, you can use the clear method of the $cache object, passing in the page's ID as a parameter. For example:

php
Copy code
$cache = $pages->getCache();
$cache->clear($page->id);
If you want to clear the cache for multiple pages, you can use a selector to retrieve the pages and loop through them, clearing the cache for each page:

php
Copy code
$pages = $pages->find('template=your_template');
foreach($pages as $page) {
    $cache->clear($page->id);
}
You can also clear the entire page cache by calling the $cache->deleteAll() method:

php
Copy code
$cache = $pages->getCache();
$cache->deleteAll();
To clear the cache for pages based on the value of URL segments or some other value, you can use a selector to retrieve the pages that match the criteria and then clear the cache for each page in the same way as shown above.

Keep in mind that clearing the page cache can have performance implications, so you should use it judiciously and only when necessary.

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...