Home >php教程 >php手册 >php 实现文件缓存函数代码

php 实现文件缓存函数代码

WBOY
WBOYOriginal
2016-06-13 10:39:22765browse

  1.  /**
  2.  * 读取或设置缓存
  3.  *
  4.  * @access  public
  5.  * @param   string  $name   缓存名称
  6.  * @param   mixed   $value  缓存内容, null删除缓存
  7.  * @param   string  $path   缓存路径
  8.  * @return  mixed
  9.  */
  10.  function cache($name, $value = , $path = )
  11.  {
  12.      return false;   //调试阶段, 不进行缓存
  13.      $path = empty($path) ? ROOT_PATH . /Runtime/Data/ : $path;
  14.      $file = $path . $name . .php;
  15.      if (empty($value)) {
  16.          //缓存不存在
  17.          if (!is_file($file)) {
  18.              return false;
  19.          }
  20.          // 删除缓存
  21.          if (is_null($value)) {
  22.              unlink($file);
  23.              return true;
  24.          }
  25.          $data = include $file;
  26.          return $data;
  27.      }
  28.      $value = var_export($value, true);
  29.      $value = "";
  30.      return file_put_contents($file, $value);
  31. }
  32. //函数调用
  33. cache(name, array(a, b, c)); //写入缓存 name为缓存名称, 后面那个数组是缓存的内容
  34. cache(name); //读取缓存
  35. cache(name, null); //删除缓存
  36. ?>

Statement:
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