own PHP Cache class php own write template engine own computer run PHP website own write MVC php box

Source: Internet
Author: User
Tags php website
<?php/** * Cache class, implement data, output cache * @author Zhouhr 2012-11-09 http://www.ketann.com * @copyright version 0.1 */class cache{p    Rivate static $_instance;    protected $_cacheid = null;    Const Cleaning_mode_all = ' all ';    Const Cleaning_mode_old = ' old ';                  Protected $_options = Array (' cache_dir ' = null,//Data cache directory ' Life_time ' = 7200, Cache time ' page_dir ' = null,//Text cache directory ' Cache_prefix ' = ' cache_ '//    cache prefix); Private Function __construct () {}///Create __clone method prevents object from being copied clone private Function __clone () {}/** * Fetch cache object If there is a direct Returns if the instantiation itself does not exist * @return Object Cache */public static function getinstance () {if (! Self::$_instan        CE) {self::$_instance = new self ();    } return self::$_instance; /** * Set the cache parameter set * @param array $options The set of cache parameter sets */Public function setoptions ($options = Array ( )) {WHIle (List ($name, $value) = each ($options)) {$this->setoption ($name, $value); }}/** * Gets the current cache parameter, returns the parameter value if $name returns all parameters as null @param string $name The parameter name to return * @return string or array     $option;           */Public Function getOption ($name = null) {if (null = = = $name) return $this->_options;        if (!is_string ($name)) {throwexception ("Incorrect parameter name: $name");        } if (Array_key_exists ($name, $this->_options)) {return $this->_options[$name];           }}/** * Set cache parameters * @param array $options The cache parameter to set */protected function setOption ($name, $value) {        if (!is_string ($name)) {throwexception ("Incorrect parameter name: $name");        } $name = Strtolower ($name);        if (Array_key_exists ($name, $this->getoption ())) {$this->_options[$name] = $value; } if ($this->_options[' cache_dir ') = = = null) {$this-> SetOption (' Cache_dir ', $this->gettmpdir ().        Directory_separator); } if ($this->_options[' page_dir ') = = = null) {$this->setoption (' Page_dir ', $this->gettmp Dir ().        Directory_separator); }}/** * Read data cache, if not present or expired, returns false * @param string $id Cache ID * @return false or data */public F               Unction Load ($id) {$this->_cacheid = $id; $file = $this->getoption (' Cache_dir '). $this->getoption (' Cache_prefix ').                       $this->_cacheid;         if (@filemtime ($file) >= time ()) {return unserialize (file_get_contents ($file));            } else {@unlink ($file);        return false;     }}/** * Save data cache and set cache expiration time * @param array or string $data data to be cached * @param int $lifeTime Cache Expiration Time */ Public function Save ($data, $lifeTime = null) {if (null!== $lifeTime) $this->setoption (' life_t           IME ', $lifeTime); $filE = $this->getoption (' Cache_dir '). $this->getoption (' Cache_prefix ').        $this->_cacheid;        $data = serialize ($data);        @file_put_contents ($file, $data);        @chmod ($file, 0777);    @touch ($file, Time () + $this->getoption (' life_time '));        /** * Read output cache, if not present or cache expires will reopen output cache * @param string $id Cache ID */Public Function start ($id) {        $this->_cacheid = $id; $file = $this->getoption (' Page_dir '). $this->getoption (' Cache_prefix ').                       $this->_cacheid;        if (@filemtime ($file) >= time ()) {return file_get_contents ($file);            } else {@unlink ($file);            Ob_start ();        return false;  }}/** * Delete the specified ID cache * @param string $id Cache ID */Public Function remove ($id) {$this->_cacheid        = $id; Delete the data cache $file = $this->getoption (' Cache_dir ') of the attached condition. $this->getoption (' Cache_prefix ').        $this->_cacheid; @unLink ($file); Delete the output cache $file = $this->getoption (' Page_dir ') of the attached condition. $this->getoption (' Cache_prefix ').        $this->_cacheid;    @unlink ($file);        }/** * Save output cache and set cache expiration time * @param int $lifeTime Cache Expiration Time */Public function end ($lifeTime = null) {           if (null!== $lifeTime) $this->setoption (' Life_time ', $lifeTime); $file = $this->getoption (' Page_dir '). $this->getoption (' Cache_prefix ').        $this->_cacheid;        $data = Ob_get_contents ();        Ob_end_clean ();        @file_put_contents ($file, $data);        @chmod ($file, 0777);    @touch ($file, Time () + $this->getoption (' life_time '));    /** * Clear Cache according to parameters * @param string $mode cache type, including (Cleaning_mode_all: All caches, Cleaning_mode_old: Expired cache) */        Public Function Clear ($mode = cleaning_mode_old) {$dirs = array (' Cache_dir ', ' page_dir '); foreach ($dirs as $value) {if (null! = $this->getoption ($value)) {$fIles = Scandir ($this->getoption ($value)); Switch ($mode) {case CLEANING_MODE_ALL:default:foreach ($f                        Iles as $val) {@unlink ($this->getoption ($value). $val);                    } break;                            Case CLEANING_MODE_OLD:default:foreach ($files as $val) { if (Filemtime ($this->getoption ($value). $val) < time ()) {@unlink ($this->ge Toption ($value).                             $val);                }} break; }}}}/** * Take temporary folder as cache folder * @return $dir Temp Folder path */Public Function Gettmpdir ()        {$tmpdir = array (); foreach (Array ($_env, $_server) as $tab) {foreach (Array (' TMPDIR ', ' TEMP ', ' TMP ', ' windir ', ' SystemRoot ') as $ Key) {                if (Isset ($tab [$key])) {if ($key = = ' windir ') or ($key = = ' SystemRoot ')) { $dir = Realpath ($tab [$key].                    ' \\temp ');                    } else {$dir = Realpath ($tab [$key]);                    } if ($this->_isgoodtmpdir ($dir)) {return $dir;        }}}} $upload = Ini_get (' Upload_tmp_dir ');            if ($upload) {$dir = Realpath ($upload);            if ($this->_isgoodtmpdir ($dir)) {return $dir;            }} if (Function_exists (' Sys_get_temp_dir ')) {$dir = Sys_get_temp_dir ();            if ($this->_isgoodtmpdir ($dir)) {return $dir;        }}//By attempting to create a temporary file to detect $tempFile = Tempnam (MD5 (Uniqid (rand (), TRUE), ");            if ($tempFile) {$dir = Realpath (dirname ($tempFile)); Unlink ($tempFile);            if ($this->_isgoodtmpdir ($dir)) {return $dir;        }} if ($this->_isgoodtmpdir ('/tmp ')) {return '/tmp ';        } if ($this->_isgoodtmpdir (' \\temp ')) {return ' \\temp ';    } throw new Exception (' cannot determine temporary directory, please manually specify Cache_dir ', E_user_error);     }/** * Verify that the given temp directory is readable and writable * * @param string $dir temporary folder path * @return Boolean true or False if the temporary folder path is readable or writable                */protected function _isgoodtmpdir ($dir) {if (is_readable ($dir)) {if (is_writable ($dir)) {            return true;    }} return false; }}//endclass

The above describes their own PHP cache class, including PHP, their own aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.