Search code examples
phpcachingfat-free-framework

Fat Free Framework dynamic cache expiration


I am trying to improve my Fat Free Framework web app by using cached queries, wherever possibile.

I am wondering whether it would make any sense to try caching even queries involving data which changes quite ofter or so, by applying a little 'trick'.

IE, tipically you wouldn't cache this:

SELECT * FROM tasks WHERE status = 'open';

But what if I could instead cache it indefinitely and have external factors clear its cache when a specific event (e.g. a task is closed, in this case) occurs?

If this is my query:

$tasks = $this->db->exec(
    array(
        "SELECT *
         FROM tasks
         WHERE status = 'open'"
    ),
    NULL,
    86400
);

is there any possible way by which I can force its 24h-long cached version to expire earlier?


Solution

  • Yes:

    Solution #1:

    Clear the whole cache content: $f3->clear('CACHE')

    Solution #2:

    Tag your cached query:

    $tasks = $this->db->exec(
      array(
        "SELECT *
         FROM tasks
         WHERE status = 'open'"
      ),
      NULL,
      [86400,'mytag'] // << tag defined here
    );
    

    Then you can clear only the cache content matching this tag:

    Cache::instance()->reset('mytag');