dongyilu3143 2014-09-17 07: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-17 07: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.

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

报告相同问题?

悬赏问题

  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!