Home >CMS Tutorial >PHPCMS >How are phpcms v9 cache files generated?

How are phpcms v9 cache files generated?

藏色散人
藏色散人Original
2020-01-14 09:53:012793browse

How are phpcms v9 cache files generated?

phpcms v9缓存文件是怎样生成的?

这篇文章介绍phpcms的缓存结构

我并没有做深入的学习,但是phpcms的想法上却是有他的过人之处,太令人折服了,这里分享phpcms缓存的一中实现方案

/include/cache.func.php

这里最先主要是定义了一些phpcms的缓存函数,phpcms的缓存分为,表缓存,模型缓存,模型字段缓存,还有模块缓存,首先这些都是基于表的缓存的。

最开始有一个函数

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;
}

这个函数就调用一大堆的缓存函数来生成缓存的。

首先第一个函数 cache_common

大家可以看下面的注释,是将 前缀名_model,前缀名_category ,前缀名_ module,前缀名,前缀名_type,前缀名_area,等等写入到$CACHE数组的对应下表之中 (比如model 表的数据$CACHE["model"]=$arr,$arr为phpcms_model表的数据)

function cache_common()
{
global $db;
$data = array();
$result = $db->query("SELECT `module`,`name`,`path`,`url`,`iscore`,`version` FROM `".DB_PRE."module` WHERE `disabled`=0");
while($r = $db->fetch_array($result))
{
   if(!$r['path']) $r['path'] = $r['module'] == 'phpcms' ? '' : $r['module'].'/';
   if(!$r['url']) $r['url'] = $r['module'] == 'phpcms' ? '' : $r['module'].'/';
   $data[$r['module']] = $r;
}
$db->free_result($result);
$CACHE['MODULE'] = $data;
//以上是将对应的模块写入$CACHE;
$data = array();
$result = $db->query("SELECT * FROM `".DB_PRE."model` WHERE `disabled`=0");
while($r = $db->fetch_array($result))
{
   $data[$r['modelid']] = $r;
}
$db->free_result($result);
$CACHE['MODEL'] = $data;
$data = array();
//以上是对应的 model表里的内容写入数组$CACHE;
$result = $db->query("SELECT `catid`,`module`,`type`,`modelid`,`catname`,`style`,`image`,`catdir`,`url`,`parentid`,`arrparentid`,`parentdir`,`child`,`arrchildid`,`items`,`citems`,`pitems`,`ismenu`,`letter` FROM `".DB_PRE."category` WHERE 1 ORDER BY `listorder`,`catid`");
while($r = $db->fetch_array($result))
{
   $r['url'] = url($r['url']);
   $data[$r['catid']] = $r;
}
$db->free_result($result);
$CACHE['CATEGORY'] = $data;
//以上是将所有的栏目写入$CACHE数组
$data = array();
$result = $db->query("SELECT `typeid`,`modelid`,`module`,`name`,`style`,`typedir`,`url` FROM `".DB_PRE."type` WHERE 1 ORDER BY `listorder`,`typeid`");
while($r = $db->fetch_array($result))
{
   $data[$r['typeid']] = $r;
}
$db->free_result($result);
$CACHE['TYPE'] = $data;
//以上是将所有的 类别表里的数据写入$CACHE
$data = array();
$result = $db->query("SELECT `areaid`,`name`,`style`,`parentid`,`arrparentid`,`child`,`arrchildid` FROM `".DB_PRE."area` WHERE 1 ORDER BY `listorder`,`areaid`");
while($r = $db->fetch_array($result))
{
   $data[$r['areaid']] = $r;
}
$db->free_result($result);
$CACHE['AREA'] = $data;
//所有的地区表写入$CACHE;
$data = array();
$result = $db->query("SELECT `urlruleid`,`urlrule` FROM `".DB_PRE."urlrule` WHERE 1 ORDER BY `urlruleid`");
while($r = $db->fetch_array($result))
{
   $data[$r['urlruleid']] = $r['urlrule'];
}
$db->free_result($result);
$CACHE['URLRULE'] = $data;
//将所有的url规则写入缓存
$data = array();
    $r = $db->get_one("SELECT `setting` FROM `".DB_PRE."module` WHERE `module`='phpcms'");
$setting = $r['setting'];
eval("\$PHPCMS = $setting;");
if($PHPCMS['siteurl'] =='') $PHPCMS['siteurl'] = SITE_URL;
$CACHE['PHPCMS'] = $PHPCMS;
//最后调用cache_write方法将所有的数组写入common.php 位置/date/cache/common.php根据系统变量慧有所改动
cache_write('common.php', $CACHE);
    
return $CACHE;
}

phpcms表缓存的实现方式主要是:利用一个叫cache_table函数$table是要缓存的表名,$fileds 是查询的字段名字,默认为 ' * ',$where sql语句中的where 子句,$order 排序, $isline是否开启字段缓存默认为不开启,如果开启表字段缓存和表缓存将同时进行

function cache_table($table, $fields = '*', $valfield = '', $where = '', $order = '', $iscacheline = 0, $number = 0)
{
global $db;
$keyfield = $db->get_primary($table);
$data = array();
if($where) $where = " WHERE $where";
if(!$order) $order = $keyfield;
$limit = $number ? "LIMIT 0,$number" : '';
$result = $db->query("SELECT $fields FROM `$table` $where ORDER BY $order $limit");
$table = preg_replace("/^".DB_PRE."(.*)$/", "", $table);
while($r = $db->fetch_array($result))
{
   if(isset($r['setting']) && !empty($r['setting']))
   {
    $setting = $r['setting'];
    eval("\$setting = $setting;");
    unset($r['setting']);
    if(is_array($setting)) $r = array_merge($r, $setting);
        }
   $key = $r[$keyfield];
   $value = $valfield ? $r[$valfield] : $r;
   $data[$key] = $value;
   if($iscacheline) cache_write($table.'_'.$key.'.php', $value); //表字段缓存
}
$db->free_result($result);
cache_write($table.'.php', $data) ;// 表缓存
}

将数据数组写入对应的缓存文件,以上这个函数就是判断下常量CACHE_PATH是否存在默认是data/cache的路径然后用file_put_contents 将缓存的数据写入到对应的cachefile中

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.func.php

//缓存模型表
function cache_model()
{
cache_table(DB_PRE.&#39;model&#39;, &#39;*&#39;, &#39;&#39;, &#39;&#39;, &#39;modelid&#39;, 1);
}
//缓存分类表生成文件路径是../data/cachecategory_catid.php
function cache_category()
{
cache_table(DB_PRE.&#39;category&#39;, &#39;*&#39;, &#39;&#39;, &#39;&#39;, &#39;listorder,catid&#39;, 1);
}

缓存类别表生成路径

../data/cache/type_typeid.php
function cache_type()
{
cache_table(DB_PRE.&#39;type&#39;, &#39;*&#39;, &#39;&#39;, &#39;&#39;, &#39;listorder,typeid&#39;, 1);
}
//缓存地区列表

生成路径:../data/cache/area_areaid.php

function cache_area()
{
cache_table(DB_PRE.&#39;area&#39;, &#39;*&#39;, &#39;&#39;, &#39;&#39;, &#39;listorder,areaid&#39;, 1);
}
//缓存用户组表
//生成路径:../data/cache member_grounp_group_id.php
function cache_member_group()
{
cache_table(DB_PRE.&#39;member_group&#39;, &#39;*&#39;, &#39;&#39;, &#39;&#39;, &#39;groupid&#39;, 1);
cache_table(DB_PRE.&#39;member_group&#39;, &#39;*&#39;, &#39;name&#39;, &#39;&#39;, &#39;groupid&#39;, 0);
}
//缓存角色表
//生成路径:../data/cache/role_roleid.php
function cache_role()
{
cache_table(DB_PRE.&#39;role&#39;, &#39;*&#39;, &#39;name&#39;, &#39;&#39;, &#39;listorder,roleid&#39;);
}
//缓存作者表
//生成路径:../data/cache/author_authorid.php
function cache_author()
{
cache_table(DB_PRE.&#39;author&#39;, &#39;*&#39;, &#39;name&#39;, &#39;&#39;, &#39;listorder,authorid&#39;, 0, 100);
}
function cache_keyword()
{
cache_table(DB_PRE.&#39;keyword&#39;, &#39;*&#39;, &#39;tag&#39;, &#39;&#39;, &#39;listorder,usetimes&#39;, 0, 100);
}
function cache_copyfrom()
{
cache_table(DB_PRE.&#39;copyfrom&#39;, &#39;*&#39;, &#39;&#39;, &#39;&#39;, &#39;listorder,usetimes&#39;, 0, 100);
}
function cache_pos()
{
cache_table(DB_PRE.&#39;position&#39;, &#39;*&#39;, &#39;name&#39;, &#39;&#39;, &#39;listorder,posid&#39;, 0);
}

PHP中文网,大量的免费PHPCMS教程,欢迎在线学习!

The above is the detailed content of How are phpcms v9 cache files generated?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn