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.'model', '*', '', '', 'modelid', 1); } //缓存分类表生成文件路径是../data/cachecategory_catid.php function cache_category() { cache_table(DB_PRE.'category', '*', '', '', 'listorder,catid', 1); }
缓存类别表生成路径
../data/cache/type_typeid.php function 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.php function cache_member_group() { cache_table(DB_PRE.'member_group', '*', '', '', 'groupid', 1); cache_table(DB_PRE.'member_group', '*', 'name', '', 'groupid', 0); } //缓存角色表 //生成路径:../data/cache/role_roleid.php function cache_role() { cache_table(DB_PRE.'role', '*', 'name', '', 'listorder,roleid'); } //缓存作者表 //生成路径:../data/cache/author_authorid.php function 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); }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to jump to the details page in phpcms: 1. Use the header function to generate a jump link; 2. Loop through the content list; 3. Get the title and details page link of the content; 4. Generate a jump link.

PHP CMS is a PHP-based open source content management system for managing website content. Its features include ease of use, powerful functionality, scalability, high security, and free open source. It can save time, improve website quality, enhance collaboration and reduce development costs, and is widely used in various websites such as news websites, blogs, corporate websites, e-commerce websites and community forums.

Title: WeChat Login Integration Guide: PHPCMS in Action In today’s Internet era, social login has become one of the essential functions of a website. As one of the most popular social platforms in China, WeChat’s login function is also used by more and more websites. This article will introduce how to integrate the WeChat login function in the PHPCMS website and provide specific code examples. Step 1: Register a WeChat Open Platform Account First, we need to register a developer account on the WeChat Open Platform and apply for the corresponding development permissions. Log in [WeChat open platform]

phpcms is not completely free. phpcms is an open source cms system, but open source does not mean free. It has two versions: free version and commercial version. The free version is limited to personal non-commercial use, while the commercial version requires purchasing a license; individuals can use it for research, and if it is commercial application , you need to pay a certain fee.

PHPCMS is a free and open source content management system (CMS) that features: open source, modularity, flexibility, user-friendliness and community support. It can be used to create various types of websites, including corporate websites, e-commerce websites, blogs, and community forums. Technical requirements include: PHP 5.6 or higher, MySQL, MariaDB or PostgreSQL database, and Apache or Nginx web server.

PHPCMS user name security setting strategy revealed In website development, user account security has always been an aspect that developers attach great importance to. The security settings of the username are also crucial, because the username is not only the user's login credentials, but may also expose the user's personal information and even cause security risks. This article will reveal the username security setting strategy in PHPCMS and give specific code examples for developers to refer to. 1. Prevent common usernames. In order to improve the security of usernames, developers should prevent users from using excessive

There are two well-known versions of phpcms, namely: 1. phpCMS4, which supports custom URL rules. The website management background is beautiful and easy to use, and has many front-end plug-ins, which can freely expand functions; 2. phpCMS2008R1, which supports multi-language, multi-site management, and page The manager is convenient, flexible, very lightweight, and runs fast.

phpcms uses mysql database. phpcms is a PHP open source website management system, developed using PHP+MYSQL as the technical basis. PHPCMS V9 adopts OOP method to build the basic operating framework. The supported PHP version is PHP5 and above, and the supported MYSQL version is MySql 4.1 and above.
