php 缓存文件 php 写入缓存文件、读取缓存文件的函数代码

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

php 缓存文件 php 写入缓存文件、读取缓存文件的函数代码

  2021-03-29 我要评论
想了解php 写入缓存文件、读取缓存文件的函数代码的相关内容吗,在本文为您仔细讲解php 缓存文件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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 写入缓存文件、读取缓存文件内容的函数代码,需要的朋友可以参考一下。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们