php - Updating DB table when Mysqli result is cached
Get the solution ↓↓↓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.
Answer
Solution:
- "there are already many visits daily" is not a valid reason to enable caching.
- 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.
- To use MYSQLND_QC_ENABLE_SWITCH you have to install a specific PHP extension and I doubt you did or going to do so
- 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.
- "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
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: uncaught mysqli_sql_exception
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.