php 实现文件缓存函数代码
Release: 2016-06-13 10:39:22
Original
759 people have browsed it
- /**
- * 读取或设置缓存
- *
- * @access public
- * @param string $name 缓存名称
- * @param mixed $value 缓存内容, null删除缓存
- * @param string $path 缓存路径
- * @return mixed
- */
- function cache($name, $value = , $path = )
- {
- return false; //调试阶段, 不进行缓存
- $path = empty($path) ? ROOT_PATH . /Runtime/Data/ : $path;
- $file = $path . $name . .php;
-
- if (empty($value)) {
- //缓存不存在
- if (!is_file($file)) {
- return false;
- }
-
- // 删除缓存
- if (is_null($value)) {
- unlink($file);
- return true;
- }
-
- $data = include $file;
- return $data;
- }
-
- $value = var_export($value, true);
- $value = "";
- return file_put_contents($file, $value);
- }
-
- //函数调用
- cache(name, array(a, b, c)); //写入缓存 name为缓存名称, 后面那个数组是缓存的内容
- cache(name); //读取缓存
- cache(name, null); //删除缓存
- ?>
-
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31