生成缓存文件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: 系统找 ...
随机推荐
- OS模块常用方法
#OS模块 #os模块就是对操作系统进行操作,使用该模块必须先导入模块: import os #getcwd() 获取当前工作目录(当前工作目录默认都是当前文件所在的文件夹) result = os. ...
- CSS预处理器—Sass、LESS和Stylus
http://www.w3cplus.com/css/css-preprocessor-sass-vs-less-stylus-2.html 一.什么是CSS预处器 CSS预处理器定义了一种新的语言, ...
- VS2015 将*.xaml.cs文件包裹在*.xaml文件下
原文:https://stackoverflow.com/questions/13387527/add-existing-xaml-files-to-visual-studio-2012 Right- ...
- json2csharp & json 格式化
json2csharp: http://json2csharp.com/ bejson: http://www.bejson.com/
- Evaluation map and reflexive space
For a normed space \(X\), an isometric isomorphism can be defined from \(X\) to its second dual spac ...
- Python_python内置加密模块
数据加密: 对称加密:数据加密和解密使用相同的密钥,主要解决数据的机密性(DES,AES) 非对称加密(公匙加密):数据加密和解密使用的不同密钥,主要用于身份的验证(DSA,RSA) 单向加密:只能加 ...
- Codeforces 542A Place Your Ad Here
Place Your Ad Here 把没用的第一类区间去掉之后, 排序, 然后枚举第二类区间, 在上面死命二分就好了. #include<bits/stdc++.h> #define L ...
- Codeforces 982E Billiard 扩展欧几里德
原文链接http://www.cnblogs.com/zhouzhendong/p/9055728.html 题目传送门 - Codeforces 928E 题意 一束与坐标轴平行或者成$45^\ci ...
- java读取配置文件方法以及工具类
第一种方式 : java工具类读取配置文件工具类 只是案例代码 抓取异常以后的代码自己处理 import java.io.FileNotFoundException; import java.io. ...
- 写面向对象的新Process
import multiprocessing class mypro(multiprocessing.Process): def __init__(self,a,b): super().__init ...