美文网首页
【PHP】---静态缓存 实例

【PHP】---静态缓存 实例

作者: 小气的王二狗 | 来源:发表于2018-03-21 20:33 被阅读12次

index.php

<?php
require 'catchFile.php';
$data=array(
  'name'=>'wlc',
  'age'=>'22',
  'sid'=>'1275591716'
);
$file= new File();
// if($file->catchData('json',$data,'demo/')){
//     echo "存入成功!";
// }else{
//   echo "出现错误!";
// }
// echo "<pre>";

  //print_r($file->catchData('json','','demo/')) ;读取文件中的json
$file->catchData('json',null,'demo/');

catchFile.php

<?php
/**
 *
 */
class File
{
  private $_dir;//当前目录
  const EXT='.txt';//写入文件的文件后缀
  //构造方法
  function __construct()
  {
    $this->_dir=dirname(__FILE__)."/files/";//dirname()方法获取当前目录
  }
  public function catchData($key,$value='',$path='')//$key相当于文件名
  {
    $filename=$this->_dir.$path.$key.self::EXT;
    echo $filename;
    //echo is_null($value);
    if(is_null($value)){
      unlink($filename);
    }
    //echo json_encode($value);
  //  echo $dir=dirname($filename);
      //print_r(json_decode(file_get_contents($filename),true));
    if($value!=''){//将value写入缓存

      $dir=dirname($filename);//$dir 为写入文件的
      if(!is_dir($dir)){//判断这个目录是否存在
        mkdir($dir,0777);//如果不存在,创建这个目录
      }else{
        file_put_contents($filename,json_encode($value));
        return true;
      }
    }
    if(!is_file($filename)){
      return false;

    }else{

      return json_decode(file_get_contents($filename),true);
    }
  }
}

相关文章

  • 【PHP】---静态缓存 实例

    index.php catchFile.php

  • nginx.conf配置

    实例1: 实例2:nginx支持php网页 实例3:nginx配置静态网页 实例4:nginx负载 实例5:实现同...

  • php-面试第三篇

    42、PHP缓存技术有哪些?1)、全页面静态化缓存2)、页面部分缓存3)、数据缓存4)、查询缓存5)、按内容变更进...

  • php静态文件缓存

  • PHP页面静态化...未完

    1.基本方式 file_put_contents()Hanshu使用PHP内置缓存机制实现页面静态化 -outp...

  • PHP中9大缓存技术总结

    1、全页面静态化缓存 也就是将页面全部生成html静态页面,用户访问时直接访问的静态页面,而不会去走php服务器解...

  • PHP中9大缓存技术总结

    1、全页面静态化缓存 也就是将页面全部生成html静态页面,用户访问时直接访问的静态页面,而不会去走php服务器解...

  • Redis+PHP缓存实例

    还在学习redis中,所以本文会持续更新 学习redis一段时间了,发现在网上都有说到使用场景,但是很少具体的实例...

  • php笔试05

    使用正则获取src属性内容 : php 捕获异常的方法try{}catch{} 你知道哪些缓存技术?说明优缺点静态...

  • PHP 实用技巧集锦

    1) 高速缓存动态PHP页面缓存整个网站,你需要考虑哪些网页获得高流量,哪些页面做一些数据库请求。静态HTML页...

网友评论

      本文标题:【PHP】---静态缓存 实例

      本文链接:https://www.haomeiwen.com/subject/lwmwqftx.html