php代码cache_php cache 缓存方法类二

php cache 缓存方法类二

/**

* Cache class

*

* @author   Avenger

* @version  1.0

* @update   2003-04-29 12:12:37

*/

class cache {

/**

* url wanta cached

*

* @var string

*/

var $cached_file;

/**

* path to the cache save

*

* @var string

*/

var $cached_path;

/**

* cached limit time

*

* @var string

*/

var $cached_time;

/**

* expire time

*

* @var string

*/

var $cached_modtime;

/**

* Construct function

*

* @access public

* @param string $path Cached path

* @param int $time Cached time

* @return void

*/

function cache($path='cache/',$time=120) {

global $HTTP_SERVER_VARS;

$query_str = preg_replace('/(&submit/.[x|y]=[0-9]+)+$/','',$HTTP_SERVER_VARS['REQUEST_URI']);

$this->cached_file = md5($query_str).'.cache';

$this->cached_path = $path;

$this->cached_time = $time * 3600;

if (is_dir($this->cached_path)===false) {

mkdir($this->cached_path,0777);

}

if (file_exists($this->cached_path.$this->cached_file)) {

$this->cached_modtime = date(time()-filemtime($this->cached_path.$this->cached_file));

}

}

/**

* Start the cache

*

* @access public

*/

function start() {

global $HTTP_GET_VARS;

if ( ($HTTP_GET_VARS['update']!="") || (!file_exists($this->cached_path.$this->cached_file)) || ($this->chched_modtime > $this->cached_time) ) {

ob_start();

} else {

readfile($this->cached_path.$this->cached_file);

exit();

}

}

/**

* End the cache

*

* @access public

*/

function end() {

global $HTTP_GET_VARS;

if ( ($HTTP_GET_VARS['update']!="") || (!file_exists($this->cached_path.$this->cached_file)) || ($this->chched_modtime > $this->cached_time) ) {

$contents = ob_get_contents();

ob_end_clean();

$HTTP_GET_VARS['update']!="" ? chmod($this->cached_path.$this->cached_file,0777) : '';

$fp = fopen($this->cached_path.$this->cached_file,'w');

fputs($fp,$contents);

fclose($fp);

echo $contents;

}

}

/**

* Flush all cache

*

* @access public

*/

function flush() {

if (function_exists('exec')) {

if (strpos(strtoupper(PHP_OS),'WIN') !== false) {

$cmd = 'del /s '.str_replace('/','//',$this->cached_path).'*.cache';

} else {

$cmd = 'rm -rf '.$ADODB_CACHE_DIR.'/*.cache';

}

exec($cmd);

} else {

$d = dir($this->cached_path);

while ($entry = $d->read()) {

$modtime = date(time()-filemtime($this->cached_path.$entry));

// if (($entry != ".") && ($entry != "..") && ($modtime > $this->cached_time)) {

if (($entry != ".") && ($entry != "..")) {

chmod($this->cached_path.$entry,0777);

unlink($this->cached_path.$entry);

}

}

$d->close();

}

return;

}

} //End Class

?>

/*

run this scrpt,at the currerent dir,u will see a new folder names' "cache1"

in the "cache1" folder will have a file.the file's contents will be:

This is a Cache test line 1 ...

This is a Cache test line 2 ...

This is a Cache test line 3 ...

And if u wanta del all cache file,u will use:

$c->flush();

It's very easily to use,isn't it? :)

If u find a bug plz report to me: avenger@php.net

Read and learn!!  :)

*/

require_once 'cache.inc.php' ;

// Create Object

$c = new cache( 'cache1/' ,120); // The cache path and cache time

// Start,All the word between the start functon and the end function will be cached

$c ->start();

echo "This is a Cache test line 1 .../n" ;

echo "This is a Cache test line 2 .../n" ;

echo "This is a Cache test line 3 .../n" ;

// End and output

$c -> end ();

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值