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

How are phpcms v9 cache files generated?

Jan 14, 2020 am 09:53 AM
phpcms

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;
}
Copy after login

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

首先第一个函数 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;
}
Copy after login

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) ;// 表缓存
}
Copy after login

将数据数组写入对应的缓存文件,以上这个函数就是判断下常量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;
}
Copy after login

至于其他的可以参照以上的方法进行添加,大家可以查查看对应的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);
}
Copy after login

缓存类别表生成路径

../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);
}
//缓存地区列表
Copy after login

生成路径:../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);
}
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to jump to the details page in phpcms How to jump to the details page in phpcms Jul 27, 2023 pm 05:23 PM

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.

What framework is phpcms? What framework is phpcms? Apr 20, 2024 pm 10:51 PM

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.

WeChat Login Integration Guide: PHPCMS Practical Combat WeChat Login Integration Guide: PHPCMS Practical Combat Mar 29, 2024 am 09:18 AM

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]

Isn't phpcms free? Isn't phpcms free? Mar 01, 2023 am 10:24 AM

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.

What does phpcms mean? What does phpcms mean? Apr 20, 2024 pm 10:39 PM

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 username security setting strategy revealed PHPCMS username security setting strategy revealed Mar 14, 2024 pm 12:06 PM

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

What versions of phpcms are there? What versions of phpcms are there? Jun 14, 2023 pm 01:13 PM

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.

What database does phpcms use? What database does phpcms use? Feb 21, 2023 pm 06:57 PM

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.

See all articles