🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] * * * * * ## 1 数据缓存文件源代码(/thinkphp/library/think/Cache.php) ~~~ protected static $instance = []; public static $readTimes = 0; public static $writeTimes = 0; protected static $handler = null; ~~~ ~~~ public static function connect(array $options = []) { $md5 = md5(serialize($options)); if (!isset(self::$instance[$md5])) { $type = !empty($options['type']) ? $options['type'] : 'File'; $class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\cache\\driver\\') . ucwords($type); unset($options['type']); self::$instance[$md5] = new $class($options); APP_DEBUG && Log::record('[ CACHE ] INIT ' . $type . ':' . var_export($options, true), 'info'); } self::$handler = self::$instance[$md5]; return self::$handler; } ~~~ ~~~ public static function __callStatic($method, $params) { if (is_null(self::$handler)) { self::connect(Config::get('cache')); } return call_user_func_array([self::$handler, $method], $params); } ~~~ ## 2 文件分析 > $instance:缓存对象实例数组 > $readTimes,$writeTimes:??? > $heandler:当前缓存处理句柄 1 `public static function connect(array $options=[])` > $options 缓存配置参数 这里的参数与缓存驱动目录的缓存驱动文件参数一致, 以/thinkphp/library/think/cache/File.php为例 ~~~ protected $options = [ 'expire' => 0, 'cache_subdir' => false, 'path_level' => 1, 'prefix' => '', 'length' => 0, 'path' => LOG_PATH, 'data_compress' => false, ]; ~~~ `$md5 = md5(serialize($options));` 将配置参数组织为作为缓存对象索引 `if (!isset(self::$instance[$md5])) {}` 检查$instance中是否存在对应索引的缓存对象 `$type = !empty($options['type']) ? $options['type'] : 'File';` 缓存类型,默认为File `$class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\cache\\driver\\') . ucwords($type);` 根据缓存类型,获取缓存驱动目录下的文件默认为File.php `unset($options['type']);` 删除缓存驱动类型配置,防止创建其他类型出错 `self::$instance[$md5] = new $class($options);` 创建对应缓存驱动对象 默认为File() `APP_DEBUG && Log::record('[ CACHE ] INIT ' . $type . ':' . var_export($options, true), 'info');` 日志记录 ~~~ self::$handler = self::$instance[$md5]; return self::$handler; ~~~ 保存创建的缓存对象到处理句柄,并返回 * * * * * 2 `public static function __callStatic($method, $params){}` > $method:调用的缓存句柄的方法 > $params:调用方法传递的参数 使用魔术方法调用缓存驱动方法 关于魔术方式的原理见php的魔术方法 ~~~ if (is_null(self::$handler)) { self::connect(Config::get('cache')); } ~~~ 如果没有self::$handler,则调用self::connect()创建缓存处理句柄 `return call_user_func_array([self::$handler, $method], $params);` 使用call_user_func_array 以$params调用$handler的$method, 关于call_user_func_array的原理见php的反射 ## 3 使用方法 1 缓存配置 在全局默认配置文析convention.php件的分析中,有关缓存的配置参数如下 ~~~ 'cache' => [ 'type' => 'File', 'path' => CACHE_PATH, 'prefix' => '', 'expire' => 0, ], ~~~ > type 缓存类型默认为File > path 缓存目录默认为CACHE_PATH,在base.php中定义, > prefix 缓存前缀 > expire 缓存时间 * * * * * 2 框架使用范例分析 打开文件thinkphp/library/think/Template.php,查找 ` Cache::set($this->config['cache_id'], $content, $this->config['cache_time']);` Cache::set()调用$handler的set方法, 也就是thinkphp/library/think/cache/File.php文件中的set()方法。 其分析见驱动扩展的D:(\cache)缓存驱动 * * * * * 3 缓存方法 在辅助函数helper.php中的S()方法可以用来快速缓存, 使用方法与tp3.2类似,参考[官方手册](http://www.kancloud.cn/manual/thinkphp/1835) ## 4 总结 tp5支持丰富多样的缓存实现,具体的缓存实现见 驱动扩展的[D:(\cache)缓存驱动](http://www.kancloud.cn/zmwtp/tp5/120856) tp5在辅助helper.php文件中使用S()封装了缓存的快捷接口,分析见 [另:(helper.php)辅助函数](http://www.kancloud.cn/zmwtp/tp5/120824) 缓存大量使用在框架的模板解析(Template.php)中。使用方法见[模板解析](http://www.kancloud.cn/zmwtp/tp5/120830)的分析