Search code examples
phpfat-free-framework

How to force clear cache for dynamic url in Fat-free Framework?


How do i clear the cache of my patterned url (dynamic url)?
Like, /assets/js/@jsname.js...

I don't want to wait until the cache expired, i want to it, immediately after i updated things on my database.

They said that to clear the database, you need to specify the key for it. And i don't know what should i enter for the key :(

Here's my code:

\F3::route("GET @virtualasset: /assets/img/@link/@size.@id.@type", "Control\\Imager->akses", 3600 * 24 * 7); // cache seminggu :3

Solution

  • HTTP responses are cached using the following key: hash(VERB URI).url.

    So if you want to refresh assets/js/foo.js, you'll have to do:

    $hash=$f3->hash('GET /assets/js/foo.js');
    $cache->clear($hash.'.url');
    

    But that's a bit of a hack since this key is not documented and may change in the future.

    Also you need to remember that the route $ttl parameter not only triggers server cache but also client cache. Which means that you need to implement a cache buster to refresh the client cache. Something like assets/js/foo.js?v=3.

    You may consider implementing cache directly in your controller class. That would give you more flexibility.