PHPCMS2008源码浅析-Cache原理分析 PHPCMS20008二次开发

a.页面缓存,在后台 网站配置-》性能优化中,配置了启用PHP页面缓存

if(CACHE_PAGE && !defined('IN_ADMIN')) cache_page_start(); //看是否调用页面缓存

global.func.php 有如下函数

function cache_page_start()
{
define('CACHE_PAGE_ID', md5(RELATE_URL));  
//定义cache文件的id,其中RELATE_URL在,common.inc.php 中
//define('RELATE_URL', isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : SCRIPT_NAME.(QUERY_STRING ? '?'.QUERY_STRING : PATH_INFO));
//把本身的url+它get后的参数,两者md5加密,用来查找和生成cache-id

define('CACHE_PAGE_DIR', CACHE_PAGE_PATH.substr(CACHE_PAGE_ID, 0, 2).'/');
页面缓存配置(config.inc.php)
//define('CACHE_PAGE', 0); //是否开启PHP页面自动缓存功能
//define('CACHE_PAGE_PATH', PHPCMS_ROOT.'data/cache_page/'); //缓存存储路径
//define('CACHE_PAGE_TTL', 3600); //秒,缓存默认生命周期
//define('CACHE_PAGE_INDEX_TTL', 300); //秒,缓存默认生命周期
//define('CACHE_PAGE_CATEGORY_TTL', 600); //秒,缓存默认生命周期
//define('CACHE_PAGE_LIST_TTL', 900); //秒,缓存默认生命周期
/define('CACHE_PAGE_CONTENT_TTL', 14400); //秒,缓存默认生命周期
//PHPCMS_ROOT.'data/cache_page/+cacheid的前三位
define('CACHE_PAGE_FILE', CACHE_PAGE_DIR.CACHE_PAGE_ID.'.html'); //加载cache文件
$contents = @file_get_contents(CACHE_PAGE_FILE);
if($contents && intval(substr($contents, 15, 25)) > TIME) //判断是否过期,如果没有,就输出文件29个字符以后的所有文件
{
   echo substr($contents, 29);
   exit;
}
return true;
}

//生成cahe文件
function cache_page($ttl = CACHE_PAGE_TTL, $isjs = 0)
{
if($ttl == 0 || !defined('CACHE_PAGE_FILE')) return false;
$contents = ob_get_contents();
if($isjs) $contents = format_js($contents);   //读入数据,格式化js
dir_create(CACHE_PAGE_DIR);
$contents = "<!--expiretime:".(TIME + $ttl)."-->/n".$contents; //加上时间和超时时间,以到后面取的时候好对比
file_put_contents(CACHE_PAGE_FILE, $contents);   //写入文件
@chmod(CACHE_PAGE_FILE, 0777);
}

//清除所有的cache文件,也是data/cache_page下的文件
function cache_page_clear()
{
@set_time_limit(600);
$dirs = glob(CACHE_PAGE_PATH.'*');
foreach($dirs as $dir)
{
   $files = glob($dir.'/*');
   foreach($files as $file)
   {
    @unlink($file);
   }
   @rmdir($dir);
}
}

//统计cache的更新时间和并写到数据库,比如更新时间,更新了多少次
//$M = $TEMP = array();

function cache_count($sql)
{
global $db, $TEMP;
$id = md5($sql);
if(!isset($TEMP['count'][$id]))
{
   if(CACHE_COUNT_TTL)
   {
    $r = $db->get_one("SELECT `count`,`updatetime` FROM `".DB_PRE."cache_count` WHERE `id`='$id'");
    if(!$r || $r['updatetime'] < TIME - CACHE_COUNT_TTL)
    {
     $r = $db->get_one($sql);
     $TEMP['count'][$id] = $r['count'];
     $db->query("REPLACE INTO `".DB_PRE."cache_count`(`id`, `count`, `updatetime`) VALUES('$id', '".$r['count']."', '".TIME."')");
    }
   }
   else
   {
    $r = $db->get_one($sql);
   }
   $TEMP['count'][$id] = $r['count'];
}
return $TEMP['count'][$id];
}
//用户信息的cache文件,现在我还不知道什么意思
function cache_member()
{
global $db;
$status = $db->table_status(DB_PRE.'member_cache');
if($status['Rows'] == 0)
{
   @set_time_limit(600);
   $db->query("INSERT INTO `".DB_PRE."member_cache` SELECT * FROM `".DB_PRE."member`");
   return true;
}
return false;
}
//------------------------
//以上这些是页面cache的部分函数,其实也就是文本cache的一种方式 ,不过这个要在后台设置开启才行
//phpcms 2008默认是不开启,如上开关的,
//下面来介绍phpcms2008默认的文件cahce 模式


b.文件cahce
//在 global.ini.php中发现如下函数
//它的cache文件的目录是daba/cahce/下的很多x.php的文件,这些文件你可以打开看一下发现是一些生成好的数组文件
//平时在后台,点更新首页,其实也就是更新了这些变量,其中用到的var_export()这个php默认函数,大家可以看一下手册

function cache_read($file, $path = '', $iscachevar = 0)
{
if(!$path) $path = CACHE_PATH;
$cachefile = $path.$file;
if($iscachevar)   //判断是否cache变量
{
   global $TEMP;   //temp是全局性的临时变量,记录一些用户信息及配置信息,在commom.ini.php中有定义,
   $key = 'cache_'.substr($file, 0, -4);                      //去掉文件的后四位作为key,这个后四位暂时我也不明白,等后面分析
   return isset($TEMP[$key]) ? $TEMP[$key] : $TEMP[$key] = @include $cachefile;
}
return @include $cachefile;
}
//生成cache文件
function cache_write($file, $array, $path = '')
{
if(!is_array($array)) return false;
$array = "<?php/nreturn ".var_export($array, true).";/n?>";
$cachefile = ($path ? $path : CACHE_PATH).$file;
$strlen = file_put_contents($cachefile, $array);
@chmod($cachefile, 0777);
return $strlen;
}
//删除cache文件
function cache_delete($file, $path = '')
{
$cachefile = ($path ? $path : CACHE_PATH).$file;
return @unlink($cachefile);
}


这只是文本cache的部分东西

全部函数在 cache.func.php
介强一下这个函数,大至过程就是通过数据库调用数据,生成变量,变量写入文件,文件在调用时被引入到模板文件
function cache_all()
{
@set_time_limit(600);
cache_common();
cache_module();
cache_model();
cache_category();
cache_area();
cache_type();
cache_member_group();
    cache_role();
cache_author();
cache_keyword();
cache_copyfrom();
cache_pos();
    cache_status();
cache_workflow();
tags_update();
return TRUE;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值