php如何写入缓存文件、读取缓存文件的函数

本篇内容主要讲解“php如何写入缓存文件、读取缓存文件的函数”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“php如何写入缓存文件、读取缓存文件的函数”吧!

一、写结果缓存文件

/**
 * 写结果缓存文件
 *
 * @params string $cache_name
 * @params string $caches
 *
 * @return
 */
function write_static_cache($cache_name, $caches)
{
  if ((DEBUG_MODE & 2) == 2)
  {
    return false;
  }
  $cache_file_path = ROOT_PATH . '/temp/static_caches/' . $cache_name . '.php';
  $content = "<?php\r\n";
  $content .= "\$data = " . var_export($caches, true) . ";\r\n";
  $content .= "?>";
  file_put_contents($cache_file_path, $content, LOCK_EX);
}

二、读结果缓存文件

/**
 * 读结果缓存文件
 *
 * @params string $cache_name
 *
 * @return array  $data
 */
function read_static_cache($cache_name)
{
  if ((DEBUG_MODE & 2) == 2)
  {
    return false;
  }
  static $result = array();
  if (!empty($result[$cache_name]))
  {
    return $result[$cache_name];
  }
  $cache_file_path = ROOT_PATH . '/temp/static_caches/' . $cache_name . '.php';
  if (file_exists($cache_file_path))
  {
    include_once($cache_file_path);
    $result[$cache_name] = $data;
    return $result[$cache_name];
  }
  else
  {
    return false;
  }
}

到此,相信大家对“php如何写入缓存文件、读取缓存文件的函数”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!