使用PHP缓存MySQL查询结果的最简单方法是? [英] The most simple way to cache MySQL query results using PHP?

查看:72
本文介绍了使用PHP缓存MySQL查询结果的最简单方法是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次有人进入我的页面list.php?id=xxxxx时,它都会重新查询一些MySQL查询以返回此信息:

Each time someone lands in my page list.php?id=xxxxx it requeries some MySQL queries to return this:

$ids = array(..,..,..); // not big array - not longer then 50 number records
$thumbs = array(..,..,..); // not big array - not longer then 50 text records
$artdesc = "some text not very long"; // text field

因为我从中进行查询的数据库很大,所以我希望将此结果在以下文件中高速缓存24h:xxxxx.php在/cache/目录中,以便可以在include("xxxxx.php")中使用它存在. (或txt文件!?,或其他任何方式)

Because the database from which I make the queries is quite big I would like to cache this results for 24h in maybe a file like: xxxxx.php in a /cache/ directory so i can use it in include("xxxxx.php") if it is present. ( or txt files !? , or any other way )

因为有非常简单的数据,我相信可以使用一些PHP行来完成,而无需使用memcached或其他专业对象.

Because there is very simple data I believe it can be done using a few of PHP lines and no need to use memcached or other professional objects.

因为我的PHP非常有限,有人可以为此任务放置PHP主行(或代码)吗?

Becasuse my PHP is very limited can someone just place the PHP main lines ( or code ) for this task ?

我真的会非常感激!

推荐答案

缓存PHP数组非常简单:

Caching a PHP array is pretty easy:

file_put_contents($path, '<?php return '.var_export($my_array,true).';?>');

然后您可以将其读出:

if (file_exists($path)) $my_array = include($path);

您可能还想查看ADOdb ,它提供了缓存内部.

You might also want to look into ADOdb, which provides caching internally.

这篇关于使用PHP缓存MySQL查询结果的最简单方法是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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