从osCommerce找出来的缓存函数


<?php



//! Write out serialized data.
// write_cache uses serialize() to store $var in $filename.
// $var - The variable to be written out.
// $filename - The name of the file to write to.
function write_cache(&$var, $filename) {
$filename = $filename;
$success = false;

// try to open the file
if ($fp = @fopen($filename, 'w')) {
// obtain a file lock to stop corruptions occuring
flock($fp, 2); // LOCK_EX
// write serialized data
fputs($fp, serialize($var));
// release the file lock
flock($fp, 3); // LOCK_UN
fclose($fp);
$success = true;
}

return $success;
}


//! Read in seralized data.
// read_cache reads the serialized data in $filename and
// fills $var using unserialize().
// $var - The variable to be filled.
// $filename - The name of the file to read.
function read_cache(&$var, $filename, $auto_expire = false){
$filename = $filename;
$success = false;

if (($auto_expire == true) && file_exists($filename)) {
$now = time();
$filetime = filemtime($filename);
$difference = $now - $filetime;

if ($difference >= $auto_expire) {
return false;
}
}

// try to open file
if ($fp = @fopen($filename, 'r')) {
// read in serialized data
$szdata = fread($fp, filesize($filename));
fclose($fp);
// unserialze the data
$var = unserialize($szdata);

$success = true;
}

return $success;
}


?>




示例:

<?php

read_cache($array,'cache.cache');
echo count($array);


$array = array();


$array[] = '1';
$array[] = '2';
$array[] = '3';

write_cache($array,'cache.cache');
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值