PHP File Cache class implementation code, _php tutorial

Source: Internet
Author: User
Tags echo date

PHP File Cache class implementation code,


PHP Cache Classification Database cache, file cache and memory cache , let me give you a detailed introduction of the PHP file cache class implementation code, there is a need to understand the friend can refer to.
Page Cache class
The code is as follows:

<?php/*include ("cache.php");  $cache = new cache (30);    $cache->cachecheck ();    echo Date ("y-m-d h:i:s"); $cache->caching ();   */class Cache {//cache directory var $cacheRoot = "./cache/";  Cache update time seconds, 0 is not cached var $cacheLimitTime = 3;   Cache filename var $cacheFileName = "";      Cache extension var $cacheFileExt = "PHP"; /* * constructor * int $cacheLimitTime Cache Update Time */function cache ($cacheLimitTime) {if (Intval ($cacheLimitTime)    ) $this->cachelimittime = $cacheLimitTime;    $this->cachefilename = $this->getcachefilename ();   Ob_start (); }/* * Check that the cache file is returned within the SET update time *: If the contents of the file are returned within the update time, the reverse returns the failure */function Cachecheck () {if (File_exists ($this     ->cachefilename) {$cTime = $this->getfilecreatetime ($this->cachefilename);      if ($cTime + $this->cachelimittime > Time ()) {echo file_get_contents ($this->cachefilename);      Ob_end_flush ();     Exit   }} return false;  }/* * cache files or output static  * String $staticFileName static file name (with relative path) */function caching ($staticFileName = "") {if ($this->cachefilename)     {$cacheContent = Ob_get_contents ();     Echo $cacheContent;        Ob_end_flush ();     if ($staticFileName) {$this->savefile ($staticFileName, $cacheContent);    } if ($this->cachelimittime) $this->savefile ($this->cachefilename, $cacheContent); }}//* Clear Cache file * String $fileName Specify file name (with function) or all * return: Clear returns True, Reverse returns false */function CLEARCAC He ($fileName = "All") {if ($fileName! = "All") {$fileName = $this->cacheroot. Strtoupper (MD5 ($fileName)). " .".     $this->cachefileext;     if (file_exists ($fileName)) {return @unlink ($fileName);    }else return false; if (Is_dir ($this->cacheroot)) {if ($dir = @opendir ($this->cacheroot)) {while ($file = @read       Dir ($dir)) {$check = Is_dir ($file); if (! $check) @unlink ($this,CacheRoot.      $file);      } @closedir ($dir);     return true;     }else{return false;    }}else{return false; The cache filename is generated from the current dynamic file */function Getcachefilename () {return $this->cacheroot. Strtoupper (MD5 ($_ server["Request_uri"]). ".".   $this->cachefileext; }/* * Cache File setup Time * string $fileName cache filename (with relative path) * Returned: File generation time in seconds, file does not exist return 0 */function getfilecreatetime (       $fileName) {if (! Trim ($fileName)) return 0;    if (file_exists ($fileName)) {return intval (Filemtime ($fileName));   }else return 0; }/* * Save File * String $fileName file name (with relative path) * String $text file Contents * Return: Successfully returned ture, failed to return false */function       SaveFile ($fileName, $text) {if (! $fileName | |! $text) return false;  if ($this->makedir (dirname ($fileName))) {if ($fp = fopen ($fileName, "w")) {if (@fwrite ($FP, $text)       ) {fclose ($FP);      return true;       }else {fclose ($FP); RetuRN false;   }}} return false; }/* * Continuous Directory * String $dir directory String * int $mode Permission number * return: Successfully created or all built returns True, other methods return false */function mak    Edir ($dir, $mode = "0777") {if (! $dir) return 0;        $dir = Str_replace ("", "/", $dir);    $mdir = ""; foreach (Explode ("/", $dir) as $val) {$mdir. = $val. "     /";          if ($val = = ":" | | $val = = "." | | Trim ($val) = = "") continue;      if (! file_exists ($mdir)) {if (! @mkdir ($mdir, $mode)) {return false;   }}} return true;  }}?>

The above use is a page cache, each time you visit the page, will first detect the corresponding cache page file exists, if not exist, connect to the database, get data, display the page and also generate a cache page file, so the next time the page file will play a role. (the template engine and some common caching classes on the Web typically have this feature)
Let's introduce a memcache cache , which is a memory cache.
The code is as follows

<?php$memcache = new Memcache; $memcache->connect (' localhost ', 11211) or Die ("Could not connect") $version = $MEMCA Che->getversion (); echo "Server ' s version:". $version. " n "; $tmp _object = new StdClass; $tmp _object->str_attr = ' test '; $tmp _object->int_attr = 123; $memcache->set ('  Key ', $tmp _object, False, Ten) or Die ("Failed-to-save data at the server"); echo "Store data in the" cache (data would expire in seconds) n ", $get _result = $memcache->get (' key '); echo" Data from the Cache:n "; Var_dump ($get _result);? >

Memcached is a high-performance, distributed memory object caching system for reducing database load and increasing access speed in dynamic applications.

The above is the whole content of this article, I hope you learn PHP cache to help.

http://www.bkjia.com/PHPjc/1065153.html www.bkjia.com true http://www.bkjia.com/PHPjc/1065153.html techarticle PHP File Cache class implementation code, PHP cache classification Database cache, file cache and memory cache, let me give you a detailed introduction of PHP file cache class implementation code, there is a need ...

  • 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.