php修改缓存文件保存地址,php文件缓存(改进型)
发布日期:2021-08-20 01:25:37 浏览次数:2 分类:技术文章

本文共 5421 字,大约阅读时间需要 18 分钟。

前言:

在开发MooPHP的过程中,为了寻找更为高效的缓存方式,对两种最常用的缓存方式进行了测试。

PHP常用缓存方式:

第一种,把需要缓存的数据进行处理,形成PHP可以直接执行的文件。在需要缓存数据的时候,通过include方式引入,并使用。

第二种,把需要的数据通过serialize函数序列化后直接保存到文件。在需要使用缓存数据的时候,通过反序列化读入文件内容并复制给需要的变量,然后使用。

测试结果:

通过测试我们发现,第二种也就是serialize缓存数据的方式更加高效。(数据略去,最后提供了文章地址下载,大家可以自行测试)

点击查看

接受别人的建议对该class进行改进 memcache的操作方式

支持 serialize存储

支持 可执行文件两种存储方式

1.[代码][PHP]代码

class cache

{

private static $_instance = null;

protected $_options = array(

'cache_dir' => "./",

'file_name_prefix' => 'cache',

'mode' => '1', //mode 1 为serialize model 2为保存为可执行文件

);

/**

* 得到本类实例

*

* @return Ambiguous

*/

public static function getInstance()

{

if(self::$_instance === null)

{

self::$_instance = new self();

}

return self::$_instance;

}

/**

* 得到缓存信息

*

* @param string $id

* @return boolean|array

*/

public static function get($id)

{

$instance = self::getInstance();

//缓存文件不存在

if(!$instance->has($id))

{

return false;

}

$file = $instance->_file($id);

$data = $instance->_fileGetContents($file);

if($data['expire'] == 0 || time() < $data['expire'])

{

return $data['contents'];

}

return false;

}

/**

* 设置一个缓存

*

* @param string $id 缓存id

* @param array $data 缓存内容

* @param int $cacheLife 缓存生命 默认为0无限生命

*/

public static function set($id, $data, $cacheLife = 0)

{

$instance = self::getInstance();

$time = time();

$cache = array();

$cache['contents'] = $data;

$cache['expire'] = $cacheLife === 0 ? 0 : $time + $cacheLife;

$cache['mtime'] = $time;

$file = $instance->_file($id);

return $instance->_filePutContents($file, $cache);

}

/**

* 清除一条缓存

*

* @param string cache id

* @return void

*/

public static function delete($id)

{

$instance = self::getInstance();

if(!$instance->has($id))

{

return false;

}

$file = $instance->_file($id);

//删除该缓存

return unlink($file);

}

/**

* 判断缓存是否存在

*

* @param string $id cache_id

* @return boolean true 缓存存在 false 缓存不存在

*/

public static function has($id)

{

$instance = self::getInstance();

$file = $instance->_file($id);

if(!is_file($file))

{

return false;

}

return true;

}

/**

* 通过缓存id得到缓存信息路径

* @param string $id

* @return string 缓存文件路径

*/

protected function _file($id)

{

$instance = self::getInstance();

$fileNmae = $instance->_idToFileName($id);

return $instance->_options['cache_dir'] . $fileNmae;

}

/**

* 通过id得到缓存信息存储文件名

*

* @param $id

* @return string 缓存文件名

*/

protected function _idToFileName($id)

{

$instance = self::getInstance();

$prefix = $instance->_options['file_name_prefix'];

return $prefix . '---' . $id;

}

/**

* 通过filename得到缓存id

*

* @param $id

* @return string 缓存id

*/

protected function _fileNameToId($fileName)

{

$instance = self::getInstance();

$prefix = $instance->_options['file_name_prefix'];

return preg_replace('/^' . $prefix . '---(.*)$/', '$1', $fileName);

}

/**

* 把数据写入文件

*

* @param string $file 文件名称

* @param array $contents 数据内容

* @return bool

*/

protected function _filePutContents($file, $contents)

{

if($this->_options['mode'] == 1)

{

$contents = serialize($contents);

}

else

{

$time = time();

$contents = "<?php \n".

" // mktime: ". $time. "\n".

" return ".

var_export($contents, true).

"\n?>";

}

$result = false;

$f = @fopen($file, 'w');

if ($f) {

@flock($f, LOCK_EX);

fseek($f, 0);

ftruncate($f, 0);

$tmp = @fwrite($f, $contents);

if (!($tmp === false)) {

$result = true;

}

@fclose($f);

}

@chmod($file,0777);

return $result;

}

/**

* 从文件得到数据

*

* @param sring $file

* @return boolean|array

*/

protected function _fileGetContents($file)

{

if(!is_file($file))

{

return false;

}

if($this->_options['mode'] == 1)

{

$f = @fopen($file, 'r');

@$data = fread($f,filesize($file));

@fclose($f);

return unserialize($data);

}

else

{

return include $file;

}

}

/**

* 构造函数

*/

protected function __construct()

{

}

/**

* 设置缓存路径

*

* @param string $path

* @return self

*/

public static function setCacheDir($path)

{

$instance = self::getInstance();

if (!is_dir($path)) {

exit('file_cache: ' . $path.' 不是一个有效路径 ');

}

if (!is_writable($path)) {

exit('file_cache: 路径 "'.$path.'" 不可写');

}

$path = rtrim($path,'/') . '/';

$instance->_options['cache_dir'] = $path;

return $instance;

}

/**

* 设置缓存文件前缀

*

* @param srting $prefix

* @return self

*/

public static function setCachePrefix($prefix)

{

$instance = self::getInstance();

$instance->_options['file_name_prefix'] = $prefix;

return $instance;

}

/**

* 设置缓存存储类型

*

* @param int $mode

* @return self

*/

public static function setCacheMode($mode = 1)

{

$instance = self::getInstance();

if($mode == 1)

{

$instance->_options['mode'] = 1;

}

else

{

$instance->_options['mode'] = 2;

}

return $instance;

}

/**

* 删除所有缓存

* @return boolean

*/

public static function flush()

{

$instance = self::getInstance();

$glob = @glob($instance->_options['cache_dir'] . $instance->_options['file_name_prefix'] . '--*');

if(empty($glob))

{

return false;

}

foreach ($glob as $v)

{

$fileName = basename($v);

$id = $instance->_fileNameToId($fileName);

$instance->delete($id);

}

return true;

}

}

/* 初始化设置cache的配置信息什么的 */

cache::setCachePrefix('core'); //设置缓存文件前缀

cache::setCacheDir('./cache'); //设置存放缓存文件夹路径

//模式1 缓存存储方式

//a:3:{s:8:"contents";a:7:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:34;i:4;i:5;i:5;i:6;i:6;i:6;}s:6:"expire";i:0;s:5:"mtime";i:1318218422;}

//模式2 缓存存储方式

/*

// mktime: 1318224645

return array (

'contents' =>

array (

0 => 1,

1 => 2,

2 => 3,

3 => 34,

4 => 5,

5 => 6,

6 => 6,

),

'expire' => 0,

'mtime' => 1318224645,

)

?>

*

*

*/

cache::setCacheMode('2');

if(!$row = cache::get('zj2'))

{

$array = array(1,2,3,34,5,6,6);

$row = cache::set('zj2',$array);

}

// cache::flush(); 清空所有缓存

print_r($row);

转载地址:https://blog.csdn.net/weixin_32141627/article/details/116187046 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:python渗透工具,基于 Python 的反连 Shell 和后渗透工具
下一篇:php 自定义伪静态,SpeedPHP/自定义伪静态URL

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年03月26日 12时51分02秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

c语言Wndproc未定义,小弟我用c语言写了一个windows窗口,为什么有提示未定义的变量类型... 2019-04-21
c语言调存储过程,写留言板–调用存储过程出问题 2019-04-21
c语言编程max,C语言编程题及答案.doc 2019-04-21
android测试页面,自动执行界面测试 | Android 开发者 | Android Developers 2019-04-21
android 图片点击变色,Android开发实现ListView点击item改变颜色功能示例 2019-04-21
android增删改查布局,Android之父_增删改查 2019-04-21
vowifi android开关,如何配置VoLTE, ViLTE and VoWifi(IMS config for VoLTE, ViLTE and VoWifi) 2019-04-21
电脑端的mafsvr服务关掉_网吧才是电脑优化的精髓!学会3招你也不用羡慕网吧的流畅了... 2019-04-21
html获取文件路径_HTML 文件路径 2019-04-21
mysql滴的一声就关了_关于mysql数据库在输入密码后,滴的一声直接退出界面的解决办法(详细办法)... 2019-04-21
mysql in 有序_mysql中的in排序 mysql按in中顺序来排序 2019-04-21
mysql 行转列 显示_mysql 行转列 (结果集以坐标显示) 2019-04-21
由于连接方在一段时间后没有正确答复或连接的主机_新风换气机使用效果不佳,为何?掌握正确使用方法就好了... 2019-04-21
mysql 查询姓王_MySQL查询语句练习题,测试足够用了 2019-04-21
mysql多实例脚本_mysql多实例脚本 2019-04-21
python如何生成excel文件夹_用python脚本通过excel生成文件夹树结构 2019-04-21
python获取post请求中的所有参数_Django从POST reques获取请求参数 2019-04-21
mysql加密复制_MySQL主从复制使用SSL加密 2019-04-21
python启动远端 exe_python打包exe开机自动启动的实例(windows) 2019-04-21
java当前路径_java获取当前路径的几种方法 2019-04-21