生成缓存文件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: 系统找 ...
随机推荐
- 升级到 Android Studio 3.0 + Gradle 4.1 遇到的一些坑及解决方案
问题一: Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkD ...
- ajax后台输出有红点
转自 百度了很多,说有utf8的bom头,通过dw,sublime软件,各种清除格式无果. 后来直接在返回结果之前,执行一下ob_clean(); 完美解决问题
- 【CF809D】Hitchhiking in the Baltic States
题意: 给你n个区间[li,ri],让你选出从中一个子序列,然后在子序列的每个区间里都选择一个tj,满足t1<t2<...<tlent1<t2<...<tlen.最 ...
- [转]Tor Browser在国内Windows平台下的超详细教程
https://cloudfra.com/tor-browser-windows.html 下载与安装 首先,你必须身处科学式网络(实在怕网站再出问题),接着就可以点击打开Tor Browser官网, ...
- Python_冒泡排序
从小到大的排序:(最前面的数和一步步和后面的数比较,如果大于则交换,如果不大于则继续循环) 方法1: data = [65, 1, 45, 77, 3, 9, 43, 23, 7, 53, 213, ...
- HDU4185 Oil Skimming 二分图匹配 匈牙利算法
原文链接http://www.cnblogs.com/zhouzhendong/p/8231146.html 题目传送门 - HDU4185 题意概括 每次恰好覆盖相邻的两个#,不能重复,求最大覆盖次 ...
- SSM知识点与整合之Spring知识点(pom.xml需要依赖的jar或者plugin)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 007 numpy数组文件的存取
不知道这个有没有用,都整理了一番. 一:数组以二进制格式进行存储 1.说明 np.save与np.load是读写磁盘数组数据的两个重要函数. 默认情况下,数组以压缩的原始二进制格式保存在扩展名为npy ...
- K线图
1.程序 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...
- IDEA添加源码包
1.在项目中选中左上角的File--->Project Structure 2.选择需要添加的源码包 3.源码已经加入