問(wèn)題描述
每次有人登陸我的頁(yè)面 list.php?id=xxxxx
它都會(huì)重新查詢(xún)一些 MySQL 查詢(xún)以返回:
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
因?yàn)槲覐闹羞M(jìn)行查詢(xún)的數(shù)據(jù)庫(kù)非常大,所以我想將此結(jié)果緩存 24 小時(shí),例如: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 )
因?yàn)橛蟹浅:?jiǎn)單的數(shù)據(jù),我相信它可以使用幾行 PHP 代碼來(lái)完成,不需要使用 memcached 或其他專(zhuān)業(yè)對(duì)象.
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.
因?yàn)槲业?PHP 非常有限,有人可以為此任務(wù)放置 PHP 主線(或代碼)嗎?
Becasuse my PHP is very limited can someone just place the PHP main lines ( or code ) for this task ?
我真的會(huì)很感激!
推薦答案
緩存 PHP 數(shù)組非常簡(jiǎn)單:
Caching a PHP array is pretty easy:
file_put_contents($path, '<?php return '.var_export($my_array,true).';?>');
然后你可以把它讀出來(lái):
Then you can read it back out:
if (file_exists($path)) $my_array = include($path);
您可能還想查看提供緩存的 ADOdb內(nèi)部.
這篇關(guān)于使用PHP緩存MySQL查詢(xún)結(jié)果的最簡(jiǎn)單方法?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!