Skip to content

Instantly share code, notes, and snippets.

@sanhuang
Last active November 11, 2016 04:22
Show Gist options
  • Save sanhuang/0ccb517f70975fd8c3e2f31592b6acf6 to your computer and use it in GitHub Desktop.
Save sanhuang/0ccb517f70975fd8c3e2f31592b6acf6 to your computer and use it in GitHub Desktop.
紀錄如何直接調整discuz原生程式對文章內容進行緩存作法!特別針對以文章編號進行目錄分割處理部分可以作為日後程式設計參考。
<?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);
}
}
<?php
/*
* 修改/source/class/table/table_forum_post.php 文件
* 找到方法fetch_all_by_tid_range_position
* 修改下面的的程序
*/
// before
$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');
// after
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');
}
<?php
/*
* 获取回帖内容缓存存放地址
*/
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment