phpmysqlcachingmysqlimysqlnd

Updating DB table when Mysqli result is cached


On my website there are already many visits daily, so I am thinking about providing the Mysqli Db results from cache.

I am trying it like this:

$res   = $mysqli->query("/*" . MYSQLND_QC_ENABLE_SWITCH . "*/" . "SELECT * FROM mytable WHERE id = $id");

while ($res->fetch_assoc()) {...}

If I am not wrong, the first time this query will come from DB and it will be stored to cache. And every further call will come from the cache only?

And my question is, if I insert another data to that table, will it be shown? Because it is not in the cache.


Solution

    1. "there are already many visits daily" is not a valid reason to enable caching.
    2. Query cache is offered by mysql < 8.0 out of the box, albeit disabled by default in 8.0. It invalidates the cache on the data change.
    3. To use MYSQLND_QC_ENABLE_SWITCH you have to install a specific PHP extension and I doubt you did or going to do so
    4. Like any other 3rd party caching solution, the Mysqlnd query result cache plugin requires manual invalidation, which means the new data won't be shown until TTL is reached. All in all this plugin seems to be utterly useless.
    5. "there are already many visits daily" is not a valid reason to enable caching so you should really leave your SQL alone and just optimize your queries as you always should