zoukankan      html  css  js  c++  java
  • 夺命雷公狗---PHP开发APP接口---5(核心技术之缓存技术)

    缓存技术:

    1.静态缓存

    保存在磁盘上的静态文件,用PHP生成数据放入静态文件中

    2.Memcache和redis缓存

    缓存主要是为了减少服务器压力。

    PHP操作缓存

    1.生成缓存

    2.获取缓存

    3.删除缓存

    <?php
        class File{
            private $_dir;
            const EXT = '.txt';
            public function __construct(){
                $this -> _dir = dirname(__FILE__).'/files'; //默认存放缓存目录的文件夹
            }
            public function cacheData($k,$v='',$path=''){
                $filename = $this->_dir.$path.$k.self::EXT;
                
                if($v!==''){//将$v值写入缓存
                    if(is_null($v)){
                        return @unlink($filename);
                    }
                    $dir = dirname($filename);
                    if(!is_dir($dir)){ //判断目录是否存在
                        mkdir($dir,0777); //不存在那就创建目录
                    }
                    
                    return file_put_contents($filename,json_encode($v));
                }
                
                if(!is_file($filename)){
                    return false;
                }else{
                    return json_decode(file_get_contents($filename),true);
                }
            }
        }

    测试代码如下:

    <?php
        include('./7.php');
        //测试的xml数据
        $data = array(
            'id' => 1,
            'name' => 'lisi',
            'type' => array(1,7,89,array('5','a','d','d'))
        );
        $file = new File();
        //这里传的$data的意思是如果值存在就打开缓存,如果不存在就生成缓存
        if($file -> cacheData('index_mk_cache'/*,$data*/,null)){
            //var_dump($file->cacheData('index_mk_cache'));exit;
            echo "success";
        }else{
            echo "error";
        }
  • 相关阅读:
    内置函数02
    生成器
    OpenJudge 2979 陪审团的人选 / Poj 1015 Jury Compromise
    OpenJudge/Poj 1936 All in All
    模板:各类型的最大数和最小数表示
    OpenJudge/Poj 1661 帮助 Jimmy
    OpenJudge/Poj 1915 Knight Moves
    OpenJudge 2757 最长上升子序列 / Poj 2533 Longest Ordered Subsequence
    OpenJudge/Poj 1163 The Triangle
    OpenJudge/Poj 1844 Sum
  • 原文地址:https://www.cnblogs.com/leigood/p/4955184.html
Copyright © 2011-2022 走看看