简体   繁体   English

缓存 Mysqli 结果时更新数据库表

[英]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.在我的网站上,每天的访问量已经很多,所以我正在考虑从缓存中提供 Mysqli Db 结果。

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.因为它不在缓存中。

  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.查询缓存由 mysql < 8.0 开箱即用提供,尽管在 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要使用 MYSQLND_QC_ENABLE_SWITCH,您必须安装特定的 PHP 扩展,我怀疑您是否这样做或打算这样做
  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.与任何其他 3rd 方缓存解决方案一样,Mysqlnd 查询结果缓存插件需要手动失效,这意味着在达到 TTL 之前不会显示新数据。 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 “每天已经有很多访问”不是启用缓存的正当理由,所以你真的应该让你的 SQL 一个人呆着,像往常一样优化你的查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM