关于缓存技术不只在php有,很多系统都有,这是为了减轻服务吕压力与数据库压力来做的,本文章从php缓存技术入门到利用缓存实例来告诉你如何应用php来实例文件缓存描述。
先看这个缓存类
<?php
代码如下 |
复制代码 |
class cache{ /* class name: cache description: control to cache data,$cache_out_time is a array to save cache date time out. version: 1.0 author: 老农 cjjer last modify:2006-2-26 author url: http://www.phprm.com private $cache_dir; private $expiretime=180;//缓存的时间是 60 秒 function __construct($cache_dirname){ if(!@is_dir($cache_dirname)){ if(!@mkdir($cache_dirname,0777)){ $this->warn('缓存文件不存在而且不能创建,需要手动创建.'); return false; } } $this->cache_dir = $cache_dirname; } function __destruct(){ echo 'cache class bye.'; } function get_url() { if (!isset($_server['request_uri'])) { $url = $_server['request_uri']; }else{ $url = $_server['script_name']; $url .= (!empty($_server['query_string'])) ? '?' . $_server['query_string'] : ''; } return $url; } function warn($errorstring){ echo "<b><font color='red'>发生错误:<pre>".$errorstring."</pre></font></b>"; } function cache_page($pageurl,$pagedata){ if(!$fso=fopen($pageurl,'w')){ $this->warns('无法打开缓存文件.');//trigger_error return false; } if(!flock($fso,lock_ex)){//lock_nb,排它型锁定 $this->warns('无法锁定缓存文件.');//trigger_error return false; } if(!fwrite($fso,$pagedata)){//写入字节流,serialize写入其他格式 $this->warns('无法写入缓存文件.');//trigger_error return false; } flock($fso,lock_un);//释放锁定 fclose($fso); return true; } |
永久链接:http://www.phprm.com/code/35555.html
转载随意!带上文章地址吧。