您的位置:首页 > 其它

模块化与缓存结合的小功能实现,可用于传递参数调用对应模块查询并缓存之用

2013-04-09 21:11 435 查看
-------code----------
<?php
$loads = array(
'blog' => 0
);//允许的 load名字 => '过期时间'

header('content-type:text/html; charset=UTF-8');
define('THIS_DIR', str_replace("\\", '/', dirname(__FILE__)) .'/');//本目录
define('THIS_CACHE', THIS_DIR .'cache/');//缓存目录
define('THIS_LOADS', THIS_DIR .'loads/');//load文件前缀
define('DOMAIN', 'http://community.chinahrd.net/');//本平台的域名

if (empty($_GET['funcLeft']) || empty($_GET['funcRight']) || empty($_GET['load'])) {
exit('/*必须提供funcLeft/funcRight/load参数*/');//必须条件不满足,应该是写代码问题
}

$load = $_GET['load'];
define('FUNCL', $_GET['funcLeft']);
define('FUNCR', $_GET['funcRight']);

if (! array_key_exists($_GET['load'], $loads)) {
outTip('load 参数出现不允许的值');exit;
}

outData(THIS_CACHE, THIS_LOADS, $load, $loads[$load]);//调用模块

/*
* 正常查询到数据,输出对象
* 注意参数只能是一维(只有一条记录)或二维数组(有一条或是多条记录)
* 注意输入数据必须是utf-8编码,否则非ansii码的内容将全部变成null
*/
function format($out) {
if (empty($out)) {//空对象
outTip('无数据');
return FUNCL .FUNCR;
}

if (is_array($out[0])) {//二维数组
$a = array();

foreach($out as $row) {
$a[] = json_encode($row);
}

return FUNCL .", " .implode($a, ','). " ".FUNCR;
} else {//一维
return FUNCL.", " .json_encode($out). " ".FUNCR;
}
}

/*
* 无法正常获取,提示错误,支持换行或是任何特殊字符
*/
function outError($tip = '参数错误') {
echo(FUNCL.", decodeURIComponent('" .rawurlencode($tip). "') ".FUNCR);//传递值有问题
}

/*
* 输出注释
*/
function outTip ($tip = '') {
echo "/*
{$tip}
*/" ;
}

/*
* 获取文件内容接口
* $jsPath 缓存文件目录
* $phpPath 生成缓存内容php目录
* $name 缓存/php的名字(共用)
* $life 缓存过期时间,负数:永不过期; 0:不缓存; 正数:生成后[正数]小时过期
* 逻辑是:检测过期要求,如果是0或是缓存文件不存在,直接include searchPhp进来,且调用searchInit方法(必须存在,不存在提示出错中止),此方法必须处理成功必须返回数据数组,失败调用outError提示错误,get参数自己获取
* 接到返回数组后,将写入缓存且输出
* 如果没有过期,且文件存在,将直接读取缓存内容输出
*/
function outData($jsPath, $phpPath, $name, $life = -1) {
if (empty($name)) {
return outTip('name为空');
}

if ($life == 0) {//实时直接输出
exit(format(callFunc($phpPath, $name)));
} else if ($life < 0) {// 永久
if (file_exists($jsPath.$name)) {//缓存存在,直接输出结束
outCache($jsPath.$name);
} else {//不存在,生成缓存且输出
saveCache($jsPath.$name, format(callFunc($phpPath, $name)));
}
} else {//定时更新
if (! file_exists($jsPath.$name) || (filemtime($jsPath.$name) + $life * 3600 < time()) ) {//缓存不存在 或过期了
saveCache($jsPath.$name, format(callFunc($phpPath, $name)));
} else {//存在,未过期,直接输出
outCache($jsPath.$name);
}
}
}

/*
* 载入php方法并执行
*/
function callFunc ($phpPath, $name) {

if (! (@include_once $phpPath .$name.'.php')) {
outTip('无法载入php文件:' .$phpPath.$name.'.php');exit;
}

if (! function_exists('get_'.$name)) {
outTip($phpPath.$name .' php中并没有编写方法: get_'.$name);exit;
}

return call_user_func('get_'.$name);
}

/*
* 写入缓存内容
*/
function saveCache($path, $out) {
@file_put_contents($path, $out) or die (outTip('写入文件失败:' .$path));
exit($out);
}

/*
* 输出缓存内容
*/
function outCache($path) {
$out = @file_get_contents($path) or die (outTip('读取文件失败:' .$path));
exit($out);
}

/*
* 载入dz
*/
function loadCore() {
unset($_SERVER['REQUEST_URI']);//因为core中检测是否包含有特殊字符,需要清空它,防止dx中止页面
global $_G;
require_once THIS_DIR.'../source/class/class_core.php';

$discuz = & discuz_core::instance();
$discuz->init_cron = false;
$discuz->init_session = false;
$discuz->init();
}

-----------------使用说明--------------
传入load参数,调用同名的模块,查询返回数据并对应缓存,最后输出是一个js callback形式,所以,是用于跨域获取数据给js用的完整功能代码.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐