当前位置:Gxlcms > PHP教程 > PHP快取问题

PHP快取问题

时间:2021-07-01 10:21:17 帮助过:2人阅读

我的网页是利用PHP去得到JSON的数据去显示内容
而在刷新一次网页,也使用到JSON的内容
这样就出现问题,令网页空间的CPU使用过高
而我在网上知道可以利用PHP快取,可是不知道如何实现

要怎么设定呢?是不是直接在index.php文件上加上什么就可以实现?如何设定快取过期?

希望可以得到解答。


回复讨论(解决方案)

快取? 是指cache吗?
可以这样写,注意,只是提供思路。

'', 'time'=>0)), true);}// 读取cache文件内容$cache = json_decode(file_get_contents($cache_file),true);// cache 未过期if(time()-$cache['time']>=$expire){	$data = file_get_contents($data_file); // 读取数据,这里可以随便写个txt放入内容测试	$cache = array(		'data' => $data,		'time' => time()	);	file_put_contents($cache_file, json_encode($cache), true); // 写入cache	echo 'read data
';}else{ // cache 已过期 echo 'read cache
'; $data = $cache['data'];}echo $data;?>

人气教程排行