PHP-将MYSQL查询缓存在txt文件中是一种好习惯吗? [英] PHP - Is it good practice to cache MYSQL queries in a txt file?

查看:103
本文介绍了PHP-将MYSQL查询缓存在txt文件中是一种好习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一家网上商店,试图通过最小化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"

  1. php类将sql查询作为字符串
  2. 对其执行md5
  3. 如果这是第一次运行
  4. 然后在数据库上执行查询
  5. 以数组形式获取结果
  6. 序列化数组并将其存储为md5_Of_Query.txt
  7. 根据缓存是否存在以及是否有效,返回未序列化(file_get_contents(md5_of_Query.txt))或$ results实际查询的结果.
  8. 该类还检查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.

http://memcached.org/

文本文件可以完成工作,并且您概述的步骤很有意义,但是内存缓存会更快,并且可以为您处理很多繁重的工作.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆