The principle of sending a PHP cache class

Source: Internet
Author: User
Tags define chmod mkdir

<?php

Define (' Cache_root ', DirName (__file__). /cache '); Cache storage Directory define (' Cache_time ', 1800);//cache time Unit seconds define (' Cache_fix ', '. html '); $CacheName =md5 ($_server[' Request_uri ')). Cache_fix; Cached filename $CacheDir =cache_root. ' /'. substr ($CacheName, 0, 1);//cache file storage Directory $CacheUrl = $CacheDir. ' /'. $CacheName//The full path of the cached file//get the request is cached, after post generally want to see the latest results   if ($_server[' request_method ']== ' get ') {   //if the cached file exists and does not expire, read it.   if (file_exists ($CacheName) && time ()-filemtime ($CacheName) <cache_time) {       $FP =fopen ($CacheName, ' RB ');      fpassthru ($fp);      fclose ($FP);       exit;   &nbsp}   //to determine whether a folder exists or not, create   elseif (!file_exists ( $CacheDir)) {      if (!file_exists (cache_root)) {        mkdir (cache_ root,0777);        chmod (cache_root,0777);      }      mkdir ($CacheDir, 0777;      chmod ($CacheDir, 0777);    }   //callback function, which automatically calls this function when the program ends     function Autocache ($contents) {      global $CacheUrl;       $fp = fopen ($CACHEURL, ' WB ');      fwrite ($fp, $contents);      fclose ($fp);      chmod ($CACHEURL, 0777);      //generates a new cache while automatically deleting all the old caches to save space and can be ignored.       //deloldcache ();     return $contents; &NBSP;&NBSP}   function Deloldcache () {      chdir (cache_root);      foreach (Glob ("*/*"). Cache_fix) as $file) {        if (Time ()-filemtime ($file) >cache_time) unlink ($file);     &NBSP}  &nbsp}   //callback function auto_cache    ob_start (' Autocache '); }else{    //Deletes the cached file if it is not a GET request.     if (file_exists ($CACHEURL)) unlink ($CACHEURL); }?>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.