BVB Source Codes

The latest thinkphp v5.2 work order system Show ReadHtmlCacheBehavior.class.php Source code

Return Download The latest thinkphp v5.2 work order system: download ReadHtmlCacheBehavior.class.php Source code - Download The latest thinkphp v5.2 work order system Source code - Type:.php
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Behavior;
  12. use Think\Storage;
  13. /**
  14.  * 系统行为扩展:静态缓存读取
  15.  */
  16. class ReadHtmlCacheBehavior {
  17.     // 行为扩展的执行入口必须是run
  18.     public function run(&$params){
  19.         // 开启静态缓存
  20.         if(IS_GET && C('HTML_CACHE_ON'))  {
  21.             $cacheTime = $this->requireHtmlCache();
  22.             if( false !== $cacheTime && $this->checkHTMLCache(HTML_FILE_NAME,$cacheTime)) { //静态页面有效
  23.                 // 读取静态页面输出
  24.                 echo Storage::read(HTML_FILE_NAME,'html');
  25.                 exit();
  26.             }
  27.         }
  28.     }
  29.  
  30.     // 判断是否需要静态缓存
  31.     static private function requireHtmlCache() {
  32.         // 分析当前的静态规则
  33.          $htmls = C('HTML_CACHE_RULES'); // 读取静态规则
  34.          if(!empty($htmls)) {
  35.             $htmls = array_change_key_case($htmls);
  36.             // 静态规则文件定义格式 actionName=>array('静态规则','缓存时间','附加规则')
  37.             // 'read'=>array('{id},{name}',60,'md5') 必须保证静态规则的唯一性 和 可判断性
  38.             // 检测静态规则
  39.             $controllerName = strtolower(CONTROLLER_NAME);
  40.             $actionName     = strtolower(ACTION_NAME);
  41.             if(isset($htmls[$controllerName.':'.$actionName])) {
  42.                 $html   =   $htmls[$controllerName.':'.$actionName];   // 某个控制器的操作的静态规则
  43.             }elseif(isset($htmls[$controllerName.':'])){// 某个控制器的静态规则
  44.                 $html   =   $htmls[$controllerName.':'];
  45.             }elseif(isset($htmls[$actionName])){
  46.                 $html   =   $htmls[$actionName]; // 所有操作的静态规则
  47.             }elseif(isset($htmls['*'])){
  48.                 $html   =   $htmls['*']; // 全局静态规则
  49.             }
  50.             if(!empty($html)) {
  51.                 // 解读静态规则
  52.                 $rule   = is_array($html)?$html[0]:$html;
  53.                 // 以$_开头的系统变量
  54.                 $callback = function($match){
  55.                     switch($match[1]){
  56.                         case '_GET':        $var = $_GET[$match[2]]; break;
  57.                         case '_POST':       $var = $_POST[$match[2]]; break;
  58.                         case '_REQUEST':    $var = $_REQUEST[$match[2]]; break;
  59.                         case '_SERVER':     $var = $_SERVER[$match[2]]; break;
  60.                         case '_SESSION':    $var = $_SESSION[$match[2]]; break;
  61.                         case '_COOKIE':     $var = $_COOKIE[$match[2]]; break;
  62.                     }
  63.                     return (count($match) == 4) ? $match[3]($var) : $var;
  64.                 };
  65.                 $rule     = preg_replace_callback('/{\$(_\w+)\.(\w+)(?:\|(\w+))?}/', $callback, $rule);
  66.                 // {ID|FUN} GET变量的简写
  67.                 $rule     = preg_replace_callback('/{(\w+)\|(\w+)}/', function($match){return $match[2]($_GET[$match[1]]);}, $rule);
  68.                 $rule     = preg_replace_callback('/{(\w+)}/', function($match){return $_GET[$match[1]];}, $rule);
  69.                 // 特殊系统变量
  70.                 $rule   = str_ireplace(
  71.                     array('{:controller}','{:action}','{:module}'),
  72.                     array(CONTROLLER_NAME,ACTION_NAME,MODULE_NAME),
  73.                     $rule);
  74.                 // {|FUN} 单独使用函数
  75.                 $rule  = preg_replace_callback('/{|(\w+)}/', function($match){return $match[1]();},$rule);
  76.                 $cacheTime  =   C('HTML_CACHE_TIME',null,60);
  77.                 if(is_array($html)){
  78.                     if(!empty($html[2])) $rule    =   $html[2]($rule); // 应用附加函数
  79.                     $cacheTime  =   isset($html[1])?$html[1]:$cacheTime; // 缓存有效期
  80.                 }else{
  81.                     $cacheTime  =   $cacheTime;
  82.                 }
  83.                
  84.                 // 当前缓存文件
  85.                 define('HTML_FILE_NAME',HTML_PATH . $rule.C('HTML_FILE_SUFFIX',null,'.html'));
  86.                 return $cacheTime;
  87.             }
  88.         }
  89.         // 无需缓存
  90.         return false;
  91.     }
  92.  
  93.     /**
  94.      * 检查静态HTML文件是否有效
  95.      * 如果无效需要重新更新
  96.      * @access public
  97.      * @param string $cacheFile  静态文件名
  98.      * @param integer $cacheTime  缓存有效期
  99.      * @return boolean
  100.      */
  101.     static public function checkHTMLCache($cacheFile='',$cacheTime='') {
  102.         if(!is_file($cacheFile) && 'sae' != APP_MODE ){
  103.             return false;
  104.         }elseif (filemtime(\Think\Think::instance('Think\View')->parseTemplate()) > Storage::get($cacheFile,'mtime','html')) {
  105.             // 模板文件如果更新静态文件需要更新
  106.             return false;
  107.         }elseif(!is_numeric($cacheTime) && function_exists($cacheTime)){
  108.             return $cacheTime($cacheFile);
  109.         }elseif ($cacheTime != 0 && NOW_TIME > Storage::get($cacheFile,'mtime','html')+$cacheTime) {
  110.             // 文件是否在有效期
  111.             return false;
  112.         }
  113.         //静态文件有效
  114.         return true;
  115.     }
  116.  
  117. }
downloadReadHtmlCacheBehavior.class.php Source code - Download The latest thinkphp v5.2 work order system Source code
Related Source Codes/Software:
V3.3.6 Workerman high-performance PHP Socket (framework) - High performance PHP Socket framework Workerman re... 2017-05-03
The phosphor meeting room reservation system MRBS (loop reservation v16.0) - The phosphor meeting room reservation system MRBS ... 2017-05-03
IDC v5.2.3 agent center - IDC Centre IDC for exploitation is a PHP+MySQL age... 2017-05-03
Zen master v9.0.1 project management software - Zen is the first domestic open source project mana... 2017-05-03
Ray speed v7.03 employee files management system - Employee files project is various, and some conten... 2017-05-03
HITCMS Haitian responsive electronic mechanical v3.0 foreign trade website source code - HITCMS Haitian response electronic machinery trade... 2017-05-03
Ray v7.02 speed survey system - Lightning speed survey system is a. NET+MSSQL deve... 2017-05-03
Cool HITCMS reactive black v3.0 enterprise website source code - HITCMS-Haitian response black enterprises website ... 2017-05-03
WebOA v17.2 network office automation system - WebOA network using b/s structure development of o... 2017-05-03
Pure imitation of han site source code, one a 】 【 c9cms kernel v1.0 - Generic http://www.wufazhuce.com/Han one · Config... 2017-05-04
zone.js - Implements Zones for JavaScript ... 2017-05-11
dragonfly - A Ruby gem for on-the-fly processing - suitable fo... 2017-05-11
oh-my-fish - The Fishshell Framework. 2017-05-11
canvas-lms - The open LMS by Instructure, Inc. 2017-05-11
ledger - Double-entry accounting system with a command-line... 2017-05-11
Python - My Python Examples http://w... 2017-05-11
blade - 2017-05-11
LaZagne - Credentials recovery project 2017-05-10
Pull-to-Refresh.Rentals-iOS - This project aims to provide a simple and customiz... 2017-05-10
spoon - Distributing instrumentation tests to all your And... 2017-05-10

 Back to top