Home > php教程 > php手册 > body text

PHP写的一个简单的cache示例

WBOY
Release: 2016-06-21 08:57:07
Original
834 people have browsed it

一个简单的cache示例(不过太简单了一些!)

?// 首先创建"cache"目录 ,用来写文件

function BZ_cache ($url,$cacheName)??
{
global $cache ;??
global $QUERY_STRING ;
// 这里你可以按自己的习惯命名??
$filename = "cache/".$cacheName.",".$QUERY_STRING.".HTML" ;

// 这里你还应该测试一下文件的时间,看是否过期
//(不过这里没做。呵呵!自己完成吧!)
?????if ( file_exists (???$filename) )??
????????{??
????????readfile ($filename) ;
???????return 1 ; ///??Ok i'v send the HTML page?????
????????}
else??
???????{
??????if ( ! isset ($cache ) )??
?????????{
??????????$fcontents = join ('', file ($url."?".$QUERY_STRING."&cache=t"));
??????????$fp = fopen ($filename , "w");
??????????fwrite ($fp, $fcontents??);
??????????fclose ( $fp) ;
?????????????return 0 ;???///??i'v to execute the file??
??????????}
???????}
}??
// 在你的PHP文件的最开始放入下面一行代码
//??if ( BZ_cache ("COMPLETE URL WITHOUT PARAMS" , "AN_IDENTIFIER" ) )???exit () ;??
//记住,这个URL是要完全的,即前面要有http://。但后面不要参数。

// 网上有一个例子:http://azerclic.labynet.org/doc.PHP3
// 第一行是??
// if ( BZ_cache ("http://azerclic.labynet.org/doc.PHP3" , "doc" ) )???exit () ;??
// 你会看到cached文件在 http://azerclic.labynet.org/cache/
// That's all??
//好运!
//我估计在WIN32下没问题,在LINUX下还要考虑权限问题的,因为一般都没有写权限



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template