Skip to content

Instantly share code, notes, and snippets.

@Johnbug
Created September 3, 2013 04:21
Show Gist options
  • Save Johnbug/6419696 to your computer and use it in GitHub Desktop.
Save Johnbug/6419696 to your computer and use it in GitHub Desktop.
php缓存机制的相关代码!一段来自uchome的代码,可以学习!
<?php
#活动类别函数
function eventclass_cache(){
global $_SGLOBAL;
$_SGLOBAL['eventclass'] = array();
#同时写入全局
$query = $_SGLOBAL['db']->query("SELECT * FROM " . tname("eventclass") . " ORDER BY displayorder");
while($value = $_SGLOBAL['db']->fetch_array($query)){
if($value['poster']) {
$value['poster'] = "data/event/".$value['classid'].".jpg";
} else {
$value['poster'] = "image/event/default.jpg";
}
$_SGLOBAL['eventclass'][$value['classid']] = $value;
}
cache_write('eventclass', "_SGLOBAL['eventclass']", $_SGLOBAL['eventclass']);
}
#写入缓存
function cache_write($name, $var, $values) {
#指定文件path
$cachefile = S_ROOT.'./data/data_'.$name.'.php';
$cachetext = "<?php\r\n".
"if(!defined('IN_UCHOME')) exit('Access Denied');\r\n".
'$'.$var.'='.arrayeval($values).
"\r\n?>";
if(!swritefile($cachefile, $cachetext)) {
exit("File: $cachefile write error.");
}
}
#写文件
function swritefile($filename, $writetext, $openmod='w') {
if(@$fp = fopen($filename, $openmod)) {
flock($fp, 2);
fwrite($fp, $writetext);
fclose($fp);
return true;
} else {
runlog('error', "File: $filename write error.");
return false;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment