PHP-将MYSQL查询缓存在txt文件中是一种好习惯吗? [英] PHP - Is it good practice to cache MYSQL queries in a txt file?
问题描述
我正在建立一家网上商店,试图通过最小化MYSQL查询来提高性能.
I'm building an online shop & trying to improve performance by minimising MYSQL queries.
通过txt文件缓存mysql查询,然后取回它而不是查询,是一种好习惯吗?这就是我在做的事情".
Is it good practice to cache the mysql queries via a txt file and then fetch that instead of the query? This is what I'm doing"
- php类将sql查询作为字符串
- 对其执行md5
- 如果这是第一次运行
- 然后在数据库上执行查询
- 以数组形式获取结果
- 序列化数组并将其存储为md5_Of_Query.txt
- 根据缓存是否存在以及是否有效,返回未序列化(file_get_contents(md5_of_Query.txt))或$ results实际查询的结果.
- 该类还检查txt文件的filemtime(),如果大于一个小时,则重新执行查询并刷新缓存.
这比每次执行sql查询更有效吗?我缺少任何安全性问题吗?
Is this more efficient than doing sql queries every time? Any security issues I'm missing?
推荐答案
如果您只是启动应用程序,内存缓存比使用文本文件要快得多.
If you're just starting the application, memcache is a much faster way to go than using text files.
文本文件可以完成工作,并且您概述的步骤很有意义,但是内存缓存会更快,并且可以为您处理很多繁重的工作.
Text files will do the job, and the steps you've outlined make sense, but memcache will be faster and handle a lot of the heavy lifting for you.
这篇关于PHP-将MYSQL查询缓存在txt文件中是一种好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!