A PHP cache class that compares egg aches

Source: Internet
Author: User
Tags array bool create directory empty key return string access
PHP Cache Class
$oFC = new Filecache ();
$sKey = ' ab_123 ';
$data = $oFC-> get ($sKey);
if (Is_null ($data))
$oFC-> Set ($sKey, Array (' name ' => ' TTT ', ' DateTime ' => date (' y-m-d h:i:s ')), 100000;
Print_r ($data);
	<?php Final class Filecache {/** * * * Cache Directory * * * @var string/private static $MSCAC
    Hepath = null;
    /** * * * * Default cache expiration Time (1 hours) * * * @var int/Const MIEXPIRE = 3600; /** * * * <br/> * Self:: $msCachePath cache directory for shared directory * * * * @param string $sCachePath * * * funct Ion __construct ($sCachePath = './tmp/') {if (Is_null (self:: $msCachePath)) Self:: $msCachePath
    = $sCachePath;  /** * * * * * Read cache <br/> * Return: Cached content, string or array, cache empty or expired return NULL * * * @param string $sKey cache key value (no MD5 ()) * * @return String NULL * * @access public/Public function get ($sKey) {if (empty) ($sK
        EY)) return false;
        $sFile = self:: GetFileName ($sKey);
        if (!file_exists ($sFile)) return null;

            else {$handle = fopen ($sFile, ' RB ');
   if (Intval (fgets ($handle)) > Time ())         {//Check timestamp {//No expiration period, remove data $sData = Fread ($handle, FileSize ($sFile));
                Fclose ($handle);
            Return Unserialize ($sData);
                else {//the expiration period fclose ($handle);
            return null; 
     /** * Write Cache * * * * @param string $sKey Cache key Value * * @param mixed $mVal objects to be saved * * @param int $iExpire failure time * @return BOOL * @access public/Public function set ($sKey, $mVa
        L, $iExpire = null) {if (empty ($sKey)) return false;
        $sFile = self:: GetFileName ($sKey);
            if (!file_exists (DirName ($sFile))) if (!self:: Is_mkdir (DirName ($sFile))) return false;
            $aBuf = Array ();
            $aBuf [] = time () + ((Empty ($iExpire))? Self:: Miexpire:intval ($iExpire));
            $aBuf [] = serialize ($mVal);
/**             * Write File Operation * * $handle = fopen ($sFile, ' WB ');
            Fwrite ($handle, implode ("\ n", $aBuf));
            Fclose ($handle);
        return true; 
         /** * * * * Delete the specified cache key value * * * @param string $sKey cache key value * @return bool
            * * Public Function del ($sKey) {if (empty ($sKey)) return false;
                else {@unlink (self:: GetFileName ($sKey));
            return true; }/** * * * Get cached file full path <br/> * Return: Cached file full path <br/> * $sKey value will be converted to MD5 () and decomposed into Level 3 directory for access

        * * * @param string $sKey Cache Key * * @return String * @access protected * *
            private static function GetFileName ($sKey) {if (empty ($sKey)) return false;
            $key _MD5 = MD5 ($sKey);
           /** * $aFileName = array ();  * $aFileName [] = RTrim (self:: $msCachePath, '/'); * $aFileName [] = $key _md5{0}.
             $key _md5{1}; * $aFileName [] = $key _md5{2}.
             $key _md5{3}; * $aFileName [] = $key _md5{4}.
             $key _md5{5};
             * $aFileName [] = $key _md5; */return Self:: $msCachePath. '/' . 
            $key _md5;
        Return implode ('/', $aFileName); 
         /** * * * * * Create directory <br/> * * * @param string $sDir * @return bool * * private static function is_mkdir ($sDir = ') {if (empty ($sDir)) ret
            Urn false;
            /** * If the cache directory cannot be created, let the system throw the error message directly/echo $sDir;
            if (!mkdir ($sDir, 0666)) return false;
        else return true; 
     } 
    }


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.