• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete Web Page Cache in CI4

#1
I am using Full Page caching:
https://codeigniter.com/userguide4/general/caching.html

It works perfectly.

Now I need to delete a specific page cache.
CI3 has a delete_cache() method to do that but I cannot find one for CI4:
https://codeigniter.com/userguide3/general/caching.html

Am I missing something?

Ben.
Reply

#2
The Cache class has a delete method.

You can get a reference to the Cache class anytime using

PHP Code:
$cache Services::cache(); 

The delete method takes a string argument that is the cached item name.

PHP Code:
$cache->delete($key); 

The hard part, for which I have no answer, is how do you determine the value of $key.
Reply

#3
It's based on a md5 hash of the full url of the page. This example are based on if you have cacheQueryString set to false and run in the Controller.

PHP Code:
// Get the current page url automatically, in case you want to store it somewhere.
$uri $this->request->uri;

$name = \CodeIgniter\HTTP\URI::createURIString($uri->getScheme(), $uri->getAuthority(), $uri->getPath());

echo 
md5($name);

// Get the current scheme and authority (includes domain) automatic, and append custom string.
$uri $this->request->uri;

$name = \CodeIgniter\HTTP\URI::createURIString($uri->getScheme(), $uri->getAuthority(), 'some/path');

echo 
md5($name);

// Build your own, so you can delete it somewhere else.

// http://example.com/
echo md5(\CodeIgniter\HTTP\URI::createURIString('http''example.com''/'));

// http://example.com/some/path
// DO NOT ADD / AT THE END!
echo md5(\CodeIgniter\HTTP\URI::createURIString('http''example.com''some/path')); 

You can of course specify the url manually if you like, you don't need to use createURIString. As long as the url matches, it will work.

The md5 generated are the $key in the above post.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.