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.'model', '*', '', '', 'modelid', 1);}//缓存分类表生成文件路径是../data/cachecategory_catid.phpfunction cache_category(){cache_table(db_pre.'category', '*', '', '', 'listorder,catid', 1);}
缓存类别表生成路径
../data/cache/type_typeid.phpfunction cache_type(){cache_table(db_pre.'type', '*', '', '', 'listorder,typeid', 1);}//缓存地区列表
生成路径:../data/cache/area_areaid.php
function cache_area(){cache_table(db_pre.'area', '*', '', '', 'listorder,areaid', 1);}//缓存用户组表//生成路径:../data/cache member_grounp_group_id.phpfunction cache_member_group(){cache_table(db_pre.'member_group', '*', '', '', 'groupid', 1);cache_table(db_pre.'member_group', '*', 'name', '', 'groupid', 0);}//缓存角色表//生成路径:../data/cache/role_roleid.phpfunction cache_role(){cache_table(db_pre.'role', '*', 'name', '', 'listorder,roleid');}//缓存作者表//生成路径:../data/cache/author_authorid.phpfunction cache_author(){cache_table(db_pre.'author', '*', 'name', '', 'listorder,authorid', 0, 100);}function cache_keyword(){cache_table(db_pre.'keyword', '*', 'tag', '', 'listorder,usetimes', 0, 100);}function cache_copyfrom(){cache_table(db_pre.'copyfrom', '*', '', '', 'listorder,usetimes', 0, 100);}function cache_pos(){cache_table(db_pre.'position', '*', 'name', '', 'listorder,posid', 0);}
,大量的免费phpcms教程,欢迎在线学习!
以上就是phpcms v9缓存文件是怎样生成的的详细内容。