/*
 * 灵动标签查询缓存
 * 传入生成的灵动标签部分，缓存时间秒,缓存类型$type='file'为文件缓存，$type='memcache'为memcache缓存)
 * 模版中使用方法如下：
*/
<?
$shujv=YL_User_Cache(array(灵动标签代码),缓存时间,'缓存类型');//缓存时间为秒
foreach($shujv as $k=>$v){
?>
<li><a href="<?=$v[bqsr][titleurl]?>"><?=$v[bqr][title]?></a></li>
<?
}
?>


例子1：（调用下载模型下的10条数据,并且使用memcache缓存）
<?php
$shujv=YL_User_Cache(array('download',10,21,0,'','plnum DESC'),10,'memcache');
foreach($shujv as $k=>$v){
?>
<li><a href="<?=$v[bqsr][titleurl]?>"><?=$v[bqr][title]?></a></li>
<?
}
?>

例子2：（调用下载模型下的10条数据,使用文件缓存）
<?php
$shujv=YL_User_Cache(array('download',10,21,0,'','plnum DESC'),10,'file');
foreach($shujv as $k=>$v){
?>
<li><a href="<?=$v[bqsr][titleurl]?>"><?=$v[bqr][title]?></a></li>
<?
}
?>



userfun.php代码：

/****************帝国CMS灵动标签缓存*******************/
function YL_User_Cache($bq, $expire = 3600, $type = 'file') {
    global $empire, $public_r;
    if (!$type) {
        $type = $public_r['add_cache'];
    }
    $expire = (int)$expire;
    $cmd5 = 'yl_' . md5(serialize($bq)); //yl_是缓存前缀
    $data = array(); //返回的数组
    if ($type == 'file') {
        $cname = $cmd5 . '.php'; //缓存名称
        $cdir = ECMS_PATH . 'd/Cache/' . esub(md5($cname), 1) . '/'; //缓存路径
        $path = $cdir . $cname; //完整路径
        createDir($cdir); //创建文件夹
        $head = "<?php if(!defined('InEmpireCMS')){exit();}?>"; //安全头部
        if (file_exists($path) && time() - filemtime($path) < $expire) { //读取缓存内容
            $filecont = str_replace($head, '', file_get_contents($path));
            $data = unserialize($filecont);
        }
    } elseif ($type == 'memcache') {
        $memcache = @memcache_connect('localhost', 11211); //连接memcache
        if ($memcache) {
            $ismemcache = 1;
            $memdata = $memcache->get($cmd5);
            $data = unserialize($memdata);
            if ($data) {
                $memcache->close();
            }
        }
    }
    if (!$data) {
        $bqno = 0;
        $ecms_bq_sql = sys_ReturnEcmsLoopBq($bq[0], $bq[1], $bq[2], $bq[3], $bq[4], $bq[5]);
        if ($ecms_bq_sql) {
            while ($bqr = mysql_fetch_array($ecms_bq_sql, MYSQL_ASSOC)) {
                $bqsr = sys_ReturnEcmsLoopStext($bqr);
                $bqno++;
                $data[$bqno] = array('bqsr' => $bqsr, 'bqr' => $bqr);
            }
        }
        if ($type == 'file') {
            file_put_contents($path, $head . serialize($data)); //写入缓存文件
        } elseif ($type == 'memcache' && $ismemcache == 1) {
            $memcache->set($cmd5, serialize($data), FALSE, $expire);
            $memcache->close();
        }
    }
    return $data;
}
//检查并创建文件夹
function createDir($path) {
    if (!file_exists($path)) {
        createDir(dirname($path));
        mkdir($path, 0777);
    }
}

