生成缓存文件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的更多相关文章

  1. Dubbo java.io.IOException: Can not lock the registry cache file

    跑单测用例的时候,以前执行成功的用例,运行时控制台仍然会报 dubbo 相关的错误: Failed to save registry store file, cause: Can not lock t ...

  2. 网页验证码出不来,读取验证码时出错:javax.imageio.IIOException: Can't create cache file!

    版权声明:本文为博主原创文章,仅作为学习交流使用 转载请注明出处 https://www.cnblogs.com/linck/p/10593053.html 今天打开项目时,发现登陆界面的验证码出不来 ...

  3. 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 ...

  4. javax.imageio.IIOException: Can't create cache file!

    javax.imageio.IIOException: Can't create cache file! at javax.imageio.ImageIO.createImageInputStream ...

  5. Android异常之 unable to write jarlist cache file

    异常: android开发调试时候不能运行,出现  unable to write jarlist cache file  错误. 解决方法: 1.找到appcompt文件夹如下的位置.

  6. 解决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 ...

  7. php实现数据库数据读取生成缓存文件

    有些时候我们希望减少对数据库的 查询来提高程序的性能,因为这些数据不是经常变更的,而是会在很长一段时间内都不会变化,因此,我们每连接一次数据库,都会把相应的结果用文件的形式保存 起来.比如对于一个商城 ...

  8. [UE4]The global shader cache file missing 运行错误解决办法

    UE4项目在VS中对项目代码编译时报如错,找了好久在UE4论坛上查到了别人的解决方案,贴出来仅供大家参考. 看到一位开发者解释出错的原因如下: There are a number of build ...

  9. tomcat Can't create cache file!

    ) at javax.imageio.ImageIO.write(ImageIO.java:1558) ... 119 more Caused by: java.io.IOException: 系统找 ...

随机推荐

  1. Spring Cloud与Spring Boot版本匹配关系

    Spring Cloud是什么? “Spring Cloud provides tools for developers to quickly build some of the common pat ...

  2. 矩阵乘法的运算量计算(华为OJ)

    题目地址: https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b?tpId=37&&tqId=21293 ...

  3. ElasticSearch文档及分布式文档存储

    1.什么是文档? 文档由索引(_index),类型(_type),唯一标识(_id) 组成,我们为 _index(索引) 分配相关逻辑地址分片,该索引下的数据会根据索引以及类型计算哈希来分配数据存储的 ...

  4. 来一个使用sysbench测试cpu性能的简单脚本

    #!/bin/bash for ((i=1; i<16; i++)); do sysbench cpu run --cpu-max-prime=10000 --threads=4 --time= ...

  5. Python学习(一) —— 基础

    一.计算机的组成 计算机硬件主要由cpu.内存.硬盘组成. cpu:相当于人类的大脑,用于计算 内存:临时加载数据或者程序.缺点:断电即消失. 硬盘:用于永久存放数据或者程序.缺点:运行速度慢. 二. ...

  6. BZOJ3451 Tyvj1953 Normal 点分治 多项式 FFT

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ3451.html 题目传送门 - BZOJ3451 题意 给定一棵有 $n$ 个节点的树,在树上随机点分 ...

  7. AtCoder Regular Contest 080 (ARC080) E - Young Maids 线段树 堆

    原文链接http://www.cnblogs.com/zhouzhendong/p/8934377.html 题目传送门 - ARC080 E - Young Maids 题意 给定一个长度为$n$的 ...

  8. Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第五集之网络配置】

    还有对第五集的补充:https://www.cnblogs.com/lirenhe/p/10405069.html 1,如果不为这个linux系统或者这台虚拟机配置IP,就不能实现通信.这样的之后安装 ...

  9. 041 SparkSql的回顾与复习

    在前面学完了SparkCore后,又学了SparkSQL,在继续加深学习之前,一定的复习还是很重要的.这里大体写一下,加深自己对SparlSQL的理解. 1.SparkSQL 首先是SaprkSQL与 ...

  10. day4 class work answer

    count=0 s = input("请输入内容:") # asd234fsdafa5456fsdaf1 while s: s=s.lstrip("abcdefghijk ...