PHP 缓存 MySQL 结果的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5327160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:01:30  来源:igfitidea点击:

PHP Best way to cache MySQL results?

phpmysqlperformancecaching

提问by Ozzy

I am currently building a PHP framework (original, i know) and im working on some optimisation features for it. One dilema I have come accross is what is the best way to cache MySQL results? I know some people will say, first optimise your MySQL etc, but lets say for arguments sake, my query takes 1 minute to run and is as optimised as possible.

我目前正在构建一个 PHP 框架(我知道是原创的),并且正在为它开发一些优化功能。我遇到的一个难题是缓存 MySQL 结果的最佳方法是什么?我知道有些人会说,首先优化你的 MySQL 等,但是为了参数的缘故,我的查询需要 1 分钟才能运行并且尽可能优化。

What would be the best way to cache the results in PHP so I dont have to rerun the query every page load?

在 PHP 中缓存结果的最佳方法是什么,这样我就不必在每个页面加载时重新运行查询?

My first thought was to maybe loop through the results, add them to an array... serialize them and then store in a file. Now as creating the cache only occurs once, I can afford the overhead of the serialize function if the array contains say 1 million results. How ever, loading the cached file and then unserializing the array on every pageload, could have performance impact.

我的第一个想法是循环遍历结果,将它们添加到一个数组中...序列化它们然后存储在一个文件中。现在创建缓存只发生一次,如果数组包含 100 万个结果,我可以负担序列化函数的开销。但是,加载缓存文件然后在每次页面加载时反序列化数组,可能会对性能产生影响。

Would it then be a better idea to while caching, instead of serializing the results and the writing to file, write to the file in a way that displays the results in a PHP readable array. So when it loads, there is no unserialize overhead.

那么在缓存时,而不是序列化结果和写入文件,以一种在 PHP 可读数组中显示结果的方式写入文件会是一个更好的主意。所以当它加载时,没有反序列化开销。

Are there any other (read: faster) ways to cache a slow query for frequent use?

是否有其他(读取:更快)方法来缓存经常使用的慢查询?

采纳答案by Mark Baker

If this is a straight array, then you could use var_export()rather than serialize (wrapping it with the appropriate "" and write it to a .php file; then include()that in your script. Best done if you can write it outside the htdocs tree, and only really appropriate for large volumes of data that memory caches would consider excessive.

如果这是一个直接数组,那么您可以使用var_export()而不是序列化(用适当的“”包装它并将其写入一个 .php 文件;然后在脚本中包含()。如果你能写它,那就最好了在 htdocs 树之外,并且仅真正适用于内存缓存会认为过多的大量数据。

回答by Dan Lugg

Memcached.

内存缓存

I always try to create my own solution at least once, to grasp better what's going on under the hood in most situations. When I created my own caching solution, I essentially did what you're talking about.

我总是尝试至少创建一次我自己的解决方案,以便在大多数情况下更好地掌握幕后发生的事情。当我创建自己的缓存解决方案时,我基本上做了你所说的。

// serialize an array of all results
$serialzedData = serialize($resultData);

// set TTL (60 seconds) and create cache filename with timestamp
$ttl = 60;
$cacheFilename = $ttl . '_' . time() . '_' . md5($sqlQuery)

// dump
file_put_contents($cacheFilename, $serializedData);

Prior to a query firing, it would search the cachedirectory for files with a matching query hash. If it does, it tests timestamp + ttl <= current_time, and if true, returns the unserialized file contents. Otherwise, overwrite it.

在查询触发之前,它会在cache目录中搜索具有匹配查询哈希的文件。如果是,则测试timestamp + ttl <= current_time,如果为真,则返回未序列化的文件内容。否则,覆盖它。

回答by Scalar

Mysql caches result of query, may be you should increase mysql query cache size? Or cache result of big query in standalone table?

mysql缓存查询结果,可能是你应该增加mysql查询缓存大小?或者在独立表中缓存大查询的结果?

回答by Furicane

Google for Memcache, that should put you in the right direction.

Google for Memcache,这应该会让你朝着正确的方向前进。