discuz 将帖子内容页使用文件缓存起来
改造discuz的缓存
1、增加文件:
cache_class.php 放在source/class 文件夹中
文件内容为:
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: class_cache.php 27449 2014-10-06 17:52 by assassin0905 $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class HfccCache {
static public function getCache($type,$key,$extra) {
switch($type) {
case "file":
if(file_exists($extra['filepath']) && (filemtime($extra['filepath']) + $extra['expire']) > time()) {
require_once $extra['filepath'];
}else{
return false;
}
break;
case "memcache":
$result = C::memory()->get($key);
break;
}
return $result ? $result : false;
}
static public function setCache($type,$key,$value,$expire = 3600,$extra) {
if(empty($value)) return false;
switch($type) {
case "file":
self::_set_data($extra['filepath'],$value);
break;
case "memcache":
C::memory()->set($key,$value,$expire);
break;
}
}
static public function _set_data($path,$value) {
$fileArr = explode('/',$path);
$fileName = end($fileArr); //获取文件名
$file = pathinfo($path);
$pathSet = $file['dirname']; //获取文件路径
if(!is_dir($pathSet)) {
// 尝试创建目录
if(!mkdir($pathSet,0777,true)){
exit('上传目录'.$pathSet.'不存在');
return false;
}
}else {
if(!is_writeable($pathSet)) {
exit('上传目录'.$pathSet.'不可写');
return false;
}
}
//写入文件缓存
$data = "<?php\r\n";
$data.= "\$result = ";
$data.= var_export($value, true);
$data.= "\r\n?>";
$fp = fopen($pathSet.'/'.$fileName,"w");
fwrite($fp,$data."\r\n");
fclose($fp);
}
}
?>
2、新增函数get_postpath() 将函数放在discuz的核心函数文件中 /source/funciton/function_core.php 中 该函数类似discuz的头像获取地址的那个函数
函数内容为:
/*
* 获取回帖内容缓存存放地址
*/
function get_postpath($tid) {
$tid = abs(intval($tid));
$tid = sprintf("%09d", $tid);
$dir1 = substr($tid, 0, 3);
$dir2 = substr($tid, 3, 2);
$dir3 = substr($tid, 5, 2);
$typeadd = $type == 'real' ? '_real' : '';
return $dir1.'/'.$dir2.'/'.$dir3.'/'.substr($tid, -2);
}
3、为了便于控制 设置开关常量 将常量放在配置文件中 便于管理
define('OPEN_FILE',true); //帖子内容页文件缓存开关常量 放置在/config/global_config.php 文件的最底部
4、修改/source/class/table/table_forum_post.php 文件
找到方法fetch_all_by_tid_range_position
修改下面的的程序
修改前的是
$data = DB::fetch_all('SELECT * FROM %t WHERE tid=%d AND position>=%d AND position<%d ORDER BY position'.($ordertype == 1 ? ' DESC' : ''), array(self::get_tablename($tableid), $tid, $start, $end), 'pid');
修改后代码为
if(OPEN_FILE) {
global $_G;
require_once libfile('class/cache');
$extra['filepath'] = './data/postcache/'.get_postpath($tid).'/'.$tid.'_'.$_G['page'].'cachefile.php'; //缓存文件存放位置
$extra['expire'] = 3600;//缓存时间
$data_cache = HfccCache::getCache('file','',$extra);
if(!$data_cache){
$data = DB::fetch_all('SELECT * FROM %t WHERE tid=%d AND position>=%d AND position<%d ORDER BY position'.($ordertype == 1 ? ' DESC' : ''), array(self::get_tablename($tableid), $tid, $start, $end), 'pid');
HfccCache::setCache('file','',$data,3600,$extra);
}else{
$data = $data_cache;
}
}else{
$data = DB::fetch_all('SELECT * FROM %t WHERE tid=%d AND position>=%d AND position<%d ORDER BY position'.($ordertype == 1 ? ' DESC' : ''), array(self::get_tablename($tableid), $tid, $start, $end), 'pid');
}
至此程序修改完成
5、查看效果
文件缓存的目录

文件内容

再看看是否起作用了

OK 说明文件缓存已经生效了 大功告成

浙公网安备 33010602011771号