zoukankan      html  css  js  c++  java
  • php cache 缓存方法类一

     <?

    php cache 缓存方法类一
    //用法:

    //Example :



    $ch = new cache();

    echo $a=ip2long ("202.97.224.68");
    echo "<br />-<br />";
    echo long2ip ($a);
    echo date("D M j G:i:s T Y");
    $ch->close();

    class cache
    {
    var $cache_dir = './cache/';//This is the directory where the cache files will be stored;
    var $cache_time = 120;//How much time will keep the cache files in seconds.

    var $caching = false;
    var $file = '';

    function cache()
    {
    //Constructor of the class
    $this->file = $this->cache_dir . urlencode( $_SERVER['REQUEST_URI'] );
    if(file_exists($this->file)) $expired = $this->check_expire();
    else $expired = false;
    if ( file_exists ( $this->file ) && ( filemtime ( $this->file ) + $this->cache_time ) > time() && !$expired )
    {
    //Grab the cache:
    $handle = fopen( $this->file , "r");
    do {
    $data = fread($handle, 8192);
    if (strlen($data) == 0) {
    break;
    }
    echo $data;
    } while (true);
    fclose($handle);
    exit();
    }
    else
    {
    //create cache :
    $this->caching = true;
    ob_start();
    $now = time();
    echo "<!--last modified:".$now."-->/n";
    }
    }

    function close()
    {
    //You should have this at the end of each page
    if ( $this->caching )
    {
    //You were caching the contents so display them, and write the cache file
    $data = ob_get_clean();
    echo $data;
    $fp = fopen( $this->file , 'w' );
    fwrite ( $fp , $data );
    fclose ( $fp );
    }
    }
    function check_expire(){
    $fp = fopen($this->file,"r");
    preg_match("//:([/d]+)/-/",fread($fp,200),$time);
    $modify_time = $time[1];
    if($modify_time<filemtime($_SERVER['SCRIPT_FILENAME'])){
    return true;
    }
    else{
    return false;
    }

    }
    }


    ?>

  • 相关阅读:
    MPS和MRP之间有什么样的关系呢
    java中静态代码块详解
    SQL server 分组后每组取出任意一行
    人是否能成功,其实可能很早就能看出来
    国内外产品经理的区别
    Yarn 和 NPM 国内快速镜像(淘宝镜像)
    vue-cli 使用less 遇到的问题 || vue-cli 使用less
    布隆过滤器
    PHP性能优化
    Redis-高并发代言词,为什么做分布式要Redis?
  • 原文地址:https://www.cnblogs.com/fengju/p/6173834.html
Copyright © 2011-2022 走看看