玄元一墨 发表于 2023-1-4 20:55:56

非常简单的PHP缓存类

我最近在做一个自己的PHP框架,写这个PHP缓存类也是为了可以在框架中更方便的创建、读取缓存。

虽然一开始是为了在框架中使用而写的这个缓存类,不过在原生环境中也可以直接使用。
废话不多说,上代码:


./src/class_cache.php
<?php
namespace YaoGuang;


class cache{

    const EH = 3600;

    const ERROR_NOT_FOUND = "缓存文件不存在.";
    const ERROR_NOT_ARRAY = "传入的数据必须是数组.";

    public function __construct(){
      //检测是否存在缓存目录
      if(!file_exists("./cache/")){
            //创建缓存目录
            mkdir("./cache/");
      }
        }

    public function set_cache($cache_name , $cache_data , $cache_file = "data.json" , $cache_update_time = 120){
      if(!is_array($cache_data)){
            return Cache::ERROR_NOT_ARRAY;
      }
      //检测是否存在缓存目录
      if(!file_exists("./cache/".$cache_name."/")){
            //创建缓存目录及默认文件
            mkdir("./cache/".$cache_name);
      }
      $cache_config = array("cache_name"=>$cache_name,"cache_update_time"=>$cache_update_time);
      $cache_data["cache_regtime"] = time();
      
      if ( file_put_contents("./cache/".$cache_name."/config.json",json_encode($cache_config,JSON_UNESCAPED_UNICODE)) == false
      || file_put_contents("./cache/$cache_name/$cache_file",json_encode($cache_data,JSON_UNESCAPED_UNICODE)) == false ){
            return false;
      }
      return $cache_data;
        }

    public function cache_static_get_data($cache_name , $cache_file = "data.json"){
      //检测是否存在缓存
      if(!file_exists("./cache/".$cache_name."/".$cache_file)){
            return cache::ERROR_NOT_FOUND;
      }
      $cache_data_json = file_get_contents("./cache/".$cache_name."/".$cache_file);
      $cache_data = json_decode($cache_data_json , true);
      return $cache_data;
        }

    public function cache_is_overtime($cache_name , $cache_file = "data.json"){
      //检测是否存在缓存
      if(!file_exists("./cache/".$cache_name."/".$cache_file)){
            return cache::ERROR_NOT_FOUND;
      }
      $cache_config = $this->cache_static_get_data($cache_name , "config.json");
      $cache_data = $this->cache_static_get_data($cache_name , $cache_file);

      $time = time();
      $dvalue = $time-$cache_data["cache_regtime"];
      if($dvalue > $cache_config["cache_update_time"]){
            return true;
      }
      return false;
        }
}
?>

./index.php
<?php
    require_once "./src/class_cache.php";

    $obj = new YaoGuang\cache();
    // var_dump($obj->cache_get_data("test"));
    // var_dump($obj->set_cache("test",array("测试字段A"=>"测试字段值 a","测试字段B"=>"测试字段值 b")));
    // var_dump($obj->cache_is_overtime("test"));
?>

引用./src/calss_cache.php文件后,
请实例化命名空间中的cache()类后再进行操作。

使用以下类方法设置缓存,缓存数据请使用数组存储,建议使用索引数组。
set_cache( 缓存名 , 缓存数据(数组类型) , 缓存文件名(可选) , 缓存超时时间(以秒计时)(可选) )

使用以下类方法静态获取缓存,缓存数据包括缓存设置时间将会以数组形式返回,且在获取后不会刷新缓存注册时间。
cache_static_get_data( 缓存名 , 缓存文件名(可选) )

使用以下类方法查询缓存是否超时,超时返回true,未超时返回false。
cache_is_overtime( 缓存名 , 缓存文件名(可选) )

注意:缓存文件名参数若是不写的话,会自动使用data.json文件作为缓存。

玄元一墨 发表于 2023-1-17 12:35:29

现在这个缓存类已经有点拉了,最新的摇光框架里的缓存类我直接发出来吧。

玄元一墨 发表于 2023-1-17 12:36:13


缓存类代码:

/**
* 缓存类: 在某些场景下,如调用api、对数据库内某些常用的字段进行调用时可以使用缓存,免去多次调用.
*/
class cache
{

    const EH = 3600;

    const ERROR_NOT_FOUND = "错误: 缓存文件不存在.";

    /**
   * 构造方法: 检测是否存在缓存目录.
   */
    public function __construct(){
      if(!file_exists("./cache/")){
            mkdir("./cache/");
      }
        }

    /**
   * 成员方法: 设置缓存内容.
   * $cache_name: 缓存集合名.
   * $cache_data: 缓存数据正文.
   * $cache_file: 缓存数据存贮的文件名 (可选).
   * $cache_update_time: 缓存超时时间 (可选).
   */
    public function set_cache($cache_name , $cache_data , $cache_file = "data.json" , $cache_update_time = 120){
      //检测是否为数组
      if(!is_array($cache_data)){
            TpPrint::common_print(ErrorGroup::ERROR_NOT_ARRAY , "错误提示");
            return ErrorGroup::ERROR_NOT_ARRAY;
      }
      if(!file_exists("./cache/".$cache_name."/")){
            //创建缓存目录及默认文件
            mkdir("./cache/".$cache_name);
      }
      $cache_config = array("cache_name"=>$cache_name,"cache_update_time"=>$cache_update_time);
      $cache_data["cache_regtime"] = time();
      
      if ( file_put_contents("./cache/".$cache_name."/config.json",json_encode($cache_config,JSON_UNESCAPED_UNICODE)) == false
      || file_put_contents("./cache/$cache_name/$cache_file",json_encode($cache_data,JSON_UNESCAPED_UNICODE)) == false ){
            return false;
      }
      return $cache_data;
        }

    /**
   * 成员方法: 静态获取缓存数据.
   * $cache_name: 缓存集合名.
   * $cache_file: 缓存数据存贮的文件名 (可选).
   */
    public function cache_static_get_data($cache_name , $cache_file = "data.json"){
      //检测是否存在缓存
      if($this->cache_exists($cache_name , $cache_file) != true){
            TpPrint::common_print($this::ERROR_NOT_FOUND , "错误提示");
            return $this::ERROR_NOT_FOUND;
      }
      $cache_data_json = file_get_contents("./cache/".$cache_name."/".$cache_file);
      $cache_data = json_decode($cache_data_json , true);
      return $cache_data;
        }

    /**
   * 成员方法: 检测缓存数据是否超时.
   * $cache_name: 缓存集合名.
   * $cache_file: 缓存数据存贮的文件名 (可选).
   */
    public function cache_is_overtime($cache_name , $cache_file = "data.json"){
      //检测是否存在缓存
      if($this->cache_exists($cache_name , $cache_file) != true){
            TpPrint::common_print($this::ERROR_NOT_FOUND , "错误提示");
            return $this::ERROR_NOT_FOUND;
      }
      $cache_config = $this->cache_static_get_data($cache_name , "config.json");
      $cache_data = $this->cache_static_get_data($cache_name , $cache_file);

      $time = time();
      $dvalue = $time-$cache_data["cache_regtime"];
      if($dvalue > $cache_config["cache_update_time"]){
            return true;
      }
      return false;
        }
       
        /**
   * 成员方法: 检测缓存数据是否存在.
   * $cache_name: 缓存集合名.
   * $cache_file: 缓存数据存贮的文件名 (可选).
   */
    public function cache_exists($cache_name , $cache_file = "data.json"){
      //检测是否存在缓存
      if(file_exists("./cache/".$cache_name."/".$cache_file)){
            return true;
      }
      return false;
        }

    /**
   * 成员方法: 删除缓存
   * $cache_name: 缓存集合名.
   * $cache_file: 缓存数据存贮的文件名 (可选).
   */
    public function cache_delete($cache_name , $cache_file = "data.json"){
      //检测是否存在缓存
      if($this->cache_exists($cache_name , $cache_file)){
            if(unlink("$cache_name/$cache_file")){
                return true;
            }
            return false;
      }
      return $this::ERROR_NOT_FOUND;
        }
}

玄元一墨 发表于 2023-1-17 12:37:43

玄元一墨 发表于 2023-1-17 12:36
缓存类代码:

/**


将最新的缓存类代码替换掉之前的./src/class_cache.php即可!
页: [1]
查看完整版本: 非常简单的PHP缓存类