dongyilu3143 2014-09-16 23:08
浏览 53
已采纳

你应该在PHP中缓存MySQL连接吗?

Are there any disadvantages to caching a MySQL connection?

For example,

$mysqli = new mysqli(params);
apc_store('mysqli', $mysqli);

This would save some time if there are lots of users on your site all requiring connections. Instead of opening a connection for every user, why not cache it?

I haven't found anything by googling, so just wondering if I've missed something.

Thanks for your time.

  • 写回答

1条回答 默认 最新

  • ds34222 2014-09-16 23:14
    关注

    A mysqli object is not something you can cache. It's a resource, not a plain object.

    Fetching it out of the cache would require it to reconnect to the database server, which means that the cached version would need to store a password in plaintext, which makes it a security flaw if you could cache it.

    Also, there's no guarantee that the database server would still be available when you fetch it out of the cache.

    And there's no way for multiple PHP requests to share the same cached resource. There's all sorts of problems with this plan.

    What is a solution for your goal is persistent mysqli connections, so the PHP runtime environment maintains some number of connections and can reuse them from one PHP request to the next.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部