zoukankan      html  css  js  c++  java
  • PHP 开发 APP 接口 学习笔记与总结

    存储静态缓存即把缓存写入文件。

    file.php

    <?php
    class Cache{
        //静态缓存文件后缀名
        const EXT = 'txt';
        //定义缓存文件存放路径
        private $_dir;
        public function __construct(){
            $this->_dir = dirname(__FILE__).'/files/';
        }
    
        public function cacheData($k,$v = '',$path = ''){
            //文件名
            $filename = $this->_dir.$path.$k.'.'.self::EXT;
            //$v不为‘’:存储缓存或者删除缓存
            if($v !== ''){
    
                //删除缓存
                if(is_null($v)){
                    return @unlink($filename);
                }
    
                //存储缓存
                $dir = dirname($filename);
                if(!is_dir($dir)){
                    mkdir($dir,0777);
                }
                //把$v转成string类型
                return file_put_contents($filename,json_encode($v));
            }
    
            //读取缓存
            if(!is_file($filename)){
                return false;
            }else{
                return json_decode(file_get_contents($filename),true);
            }
        }
    }

    testfile.php

    <?php
    require 'file.php';
    
    $data = array(
        'id'=>1,
        'name'=>'Mary',
        'type'=>array(1,3,6)
    );
    
    $file_cache = new Cache();
    //存储缓存
    if($file_cache->cacheData('index_cache',$data)){
        echo 'success';
    }else{
        echo 'error';
    }
    
    //读取缓存
    if($con = $file_cache->cacheData('index_cache')){
        var_dump($con);
    }else{
        echo 'error';
    }
    
    //删除缓存
    if($con = $file_cache->cacheData('index_cache',null)){
        echo 'delete success';
    }else{
        echo 'error';
    }

     ======

    稍微修改一下,设置n分钟的缓存,超过n分钟则重新生成缓存,否则从缓存中读取数据。

    在file.php 中,存储数据时把文件名和文件修改时间也同时存入缓存数据

    <?php
    class Cache{
        //静态缓存文件后缀名
        const EXT = 'txt';
        //定义缓存文件存放路径
        private $_dir;
        public function __construct(){
            $this->_dir = dirname(__FILE__).'/files/';
        }
    
        public function cacheData($k,$v = '',$path = ''){
            //文件名
            $filename = $this->_dir.$path.$k.'.'.self::EXT;
            //$v不为‘’:存储缓存或者删除缓存
            if($v !== ''){
    
                //删除缓存
                if(is_null($v)){
                    return @unlink($filename);
                }
    
                //存储缓存
                $dir = dirname($filename);
                if(!is_dir($dir)){
                    mkdir($dir,0777);
                }
                //把$v转成string类型
                $_return = array(
                    'filename' => $filename,
                    'filetime' => @filemtime($filename), //文件创建(修改)时间
                    'con' => json_encode($v)
                );
                return file_put_contents($filename,json_encode($_return));
            }
    
            //读取缓存
            if(!is_file($filename)){
                return false;
            }else{
                return json_decode(file_get_contents($filename),true);
            }
        }
    }

    testfile.php

    <?php
    require 'file.php';
    
    $data = array(
        'id'=>1,
        'name'=>'Mary',
        'type'=>array(1,3,6)
    );
    
    $file_cache = new Cache();
    
    //设置5min的缓存,超过30s则重新生成缓存,否则从缓存中读取数据
    $k = 'index_cache';
    $countdown = 5*60;
    $con = $file_cache->cacheData($k);
    
    if($con){ //如果能够读取缓存
        if(time()-$con['filetime'] > 30){
            $file_cache->cacheData($k,$data);
            var_dump($data);
        }else{
            $res = $file_cache->cacheData($k);
            if($res){
                var_dump(json_decode($res['con'],true));
            }    
        }
    }else{    //如果缓存不存在则创建缓存
        $file_cache->cacheData($k,$data);
        var_dump($data);
    }
  • 相关阅读:
    SQL学习
    FOR XML PATH
    IOS学习网址
    weak nonatomic strong等介绍(ios)
    UVALive3045 POJ2000 ZOJ2345 Gold Coins
    UVA713 UVALive5539 POJ1504 ZOJ2001 Adding Reversed Numbers
    UVA713 UVALive5539 POJ1504 ZOJ2001 Adding Reversed Numbers
    UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves【BFS】
    UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves【BFS】
    UVA10905 Children's Game
  • 原文地址:https://www.cnblogs.com/dee0912/p/4321798.html
Copyright © 2011-2022 走看看