生成缓存文件cache file
生成缓存文件cache file
class Test{
public function index(){
$arrConfig = Array(
'name' => 'daicr',
'age' => 24,
'host' => '127.0.0.1',
'port' => 80,
);
$str = $this -> getcachevars(array("config_im_api" => $arrConfig),'ARRAY');
$this->writeToCache('file_name.php',$str,'cache_','./config');
var_dump($str);
} /**
* @purpose : 字符串处理函数;1、将数组(可以是多维数组)转换成数组样式的字符串;2、将字符串定义为一个常量
* @author : daicr
* @param : 如果传递的$data是数组,则$type传入ARRAY
* @param : 如果传递的$data是字符串,则$type传入STRING
* @return : string $evaluate 返回一个处理好的字符串,' 和 \ 会在前面加一个 \
**/
function getcachevars($data, $type = 'STRING') {
$evaluate = '';
foreach($data as $key => $val) {
if(is_array($val)) {
$evaluate .= "\$$key = ".$this->arrayeval($val).";\n";
} else {
$val = addcslashes($val, '\'\\');
$evaluate .= $type == 'ARRAY' ? "\$$key = '$val';\n" : "define('".strtoupper($key)."', '$val');\n";
}
}
return $evaluate;
} function arrayeval($array, $level = 0) {
$space = '';
for($i = 0; $i <= $level; $i++) {
$space .= "\t";
}
$evaluate = "Array\n$space(\n";
$comma = $space;
foreach($array as $key => $val) {
$key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key; // 将 key 加上一个 ''
if (!is_array($val) && (!preg_match("/^\-?\d+$/", $val) || strlen($val) > 12)) { // /^\-?\d+$/ 匹配数字 或者以 - 开头的数字
$val = '\''.addcslashes($val, '\'\\').'\'';
} else {
if ('\'postcode\'' === $key) { //邮编必须得保存为字符串型
$val = '\''.addcslashes($val, '\'\\').'\'';
}
}
if(is_array($val)) {
$evaluate .= "$comma$key => ".arrayeval($val, $level + 1); // 多维数组递归调用
} else {
$evaluate .= "$comma$key => $val";
}
$comma = ",\n$space";
}
$evaluate .= "\n$space)";
return $evaluate;
} /**
* @purpose : cache写入函数
* @author : daicr
* @param : string $scriptName 生成的文件名
* @param : string $cacheStr 需要写入的字符串
* @param : string $prefix 前缀
* @param : string $dir 写入路径
* @return : string $evaluate 返回一个处理好的字符串,' 和 \ 会在前面加一个 \
**/
public function writeToCache($scriptName,$cacheStr,$prefix='cache_',$dir){
if(!is_dir($dir)){
mkdir($dir,0777);
}
if($fp = fopen("$dir/$prefix$scriptName", 'w')) {
fwrite($fp, "<?php\n//Cache file, DO NOT modify me!\n".
"//Created on ".date("M j, Y, G:i")."\n\n$cacheStr?>");
fclose($fp);
} else {
exit('Can not write to cache files!');
}
}
} $test = new Test();
$test -> index();
2018-02-24
生成缓存文件cache file的更多相关文章
- Dubbo java.io.IOException: Can not lock the registry cache file
跑单测用例的时候,以前执行成功的用例,运行时控制台仍然会报 dubbo 相关的错误: Failed to save registry store file, cause: Can not lock t ...
- 网页验证码出不来,读取验证码时出错:javax.imageio.IIOException: Can't create cache file!
版权声明:本文为博主原创文章,仅作为学习交流使用 转载请注明出处 https://www.cnblogs.com/linck/p/10593053.html 今天打开项目时,发现登陆界面的验证码出不来 ...
- If a cache file exists, it is sent directly to the browser, bypassing the normal system execution.
w开启缓存,缓存视图,用于后续请求. https://www.codeigniter.com/userguide3/overview/appflow.html http://codeigniter.o ...
- javax.imageio.IIOException: Can't create cache file!
javax.imageio.IIOException: Can't create cache file! at javax.imageio.ImageIO.createImageInputStream ...
- Android异常之 unable to write jarlist cache file
异常: android开发调试时候不能运行,出现 unable to write jarlist cache file 错误. 解决方法: 1.找到appcompt文件夹如下的位置.
- 解决Warning: unlink(/storage/cache/cache.catalog.language.1556158719): No such file or directory in /system/library/cache/file.php on line 68问题
ytkah在调试opencart项目时提示Warning: unlink(/storage/cache/cache.catalog.language.1556158719): No such file ...
- php实现数据库数据读取生成缓存文件
有些时候我们希望减少对数据库的 查询来提高程序的性能,因为这些数据不是经常变更的,而是会在很长一段时间内都不会变化,因此,我们每连接一次数据库,都会把相应的结果用文件的形式保存 起来.比如对于一个商城 ...
- [UE4]The global shader cache file missing 运行错误解决办法
UE4项目在VS中对项目代码编译时报如错,找了好久在UE4论坛上查到了别人的解决方案,贴出来仅供大家参考. 看到一位开发者解释出错的原因如下: There are a number of build ...
- tomcat Can't create cache file!
) at javax.imageio.ImageIO.write(ImageIO.java:1558) ... 119 more Caused by: java.io.IOException: 系统找 ...
随机推荐
- idea svn配置报错:Can't use Subversion command line client:svn
1. 在Intellij IDEA里checkout东西时出先这个错误提示:Can't use Subversion command line client:svnSubversion command ...
- JavaMail在Windows平台下正常发送邮件,部署到Linux后则发送失败
问题: 在本机(Windows)环境下可以成功发送邮件,但部署到Linux服务器上后不能成功发送,前台不提示错误或提示502. linux下日志提示:javamail isssl false.... ...
- What is base..ctor(); in C#?
I am disassembling some C# applications and I am trying to reconstruct the source code. I am disasse ...
- Linux使用退格键时出现^H ^?解决方法
Linux使用退格键时出现^H ^?解决方法 在linux下执行脚本不注意输错内容需要删除时总是出现^H ^H不是H键的意思,是backspace.主要是当你的终端backspace有问题的时候才需要 ...
- Redis的搭建和Redis的集群搭建
1.Redis的官网:https://redis.io/ Redis的测试网站:http://try.redis.io/ 2.参考博客:https://www.cnblogs.com/maf ...
- MYSQL数据仓库infobright【备忘】
Infobright是一个基于MySQL开发的开源数据仓库(Data Warehouse)软件,可作为MySQL的一个存储引擎来使用,SELECT查询与普通MySQL无区别. 优点:查询性能高:百万. ...
- MVC区域area
1.当项目业务比较庞大,可以通过区域来分拆. 2.添加区域时,默认会生成一下文件. 3.Application_Start()必需含有AreaRegistration.RegisterAllAreas ...
- Nginx配置项优
1.nginx运行工作进程个数,一般设置cpu的核数或者核心数x2 如果不了解cpu的核数,可以top命令之后按1看出来,也可以查看/proc/cpuinfo文件. [root@localhost~] ...
- Rookey.Frame企业级极速开发框架
项目详细介绍 Rookey.Frame是一套基于.NET MVC + easyui的企业级极速开发框架,支持简单逻辑模块零代码编程.支持工作流(BPM).支持二次开发,具有高扩展性.高复用性.高伸缩性 ...
- lr场景异常Continuing after Error -26479: Conversion of form submission data to the target charset failed: U_TRUNCATED_CHAR_FOUND解决方法
在lr压测场景中执行,发现 一个事务都没有成功,很是奇怪,发现用linux的agent各种问题 查看lr运行日志 看到这里基本确定是编码的问题,然后想起lr设置编码的地方就那么几个,所以逐个尝试 改完 ...