这里有新鲜出炉的PHP设计模式,程序狗速度看过来!
PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适用于web开发领域。PHP的文件后缀名为php。
有时候我们需要将一些配置文件保存到静态文件中,方便后期调用,这里就为大家分享两个函数,需要的朋友可以参考一下
一、写结果缓存文件
- /**
- * 写结果缓存文件
- *
- * @params string $cache_name
- * @params string $caches
- *
- * @return
- */
- function write_static_cache($cache_name, $caches)
- {
- if ((DEBUG_MODE & 2) == 2)
- {
- return false;
- }
- $cache_file_path = ROOT_PATH . '/temp/static_caches/' . $cache_name . '.php';
- $content = "<?php\r\n";
- $content .= "\$data = " . var_export($caches, true) . ";\r\n";
- $content .= "?>";
- file_put_contents($cache_file_path, $content, LOCK_EX);
- }
二、读结果缓存文件
- /**
- * 读结果缓存文件
- *
- * @params string $cache_name
- *
- * @return array $data
- */
- function read_static_cache($cache_name)
- {
- if ((DEBUG_MODE & 2) == 2)
- {
- return false;
- }
- static $result = array();
- if (!empty($result[$cache_name]))
- {
- return $result[$cache_name];
- }
- $cache_file_path = ROOT_PATH . '/temp/static_caches/' . $cache_name . '.php';
- if (file_exists($cache_file_path))
- {
- include_once($cache_file_path);
- $result[$cache_name] = $data;
- return $result[$cache_name];
- }
- else
- {
- return false;
- }
- }
以上就是php 写入缓存文件、读取缓存文件内容的函数代码,需要的朋友可以参考一下。
来源: http://www.phperz.com/article/17/0908/345368.html