PHP general cache method-PHP source code

Source: Internet
Author: User
Common PHP Cache methods
/** General cache description: it is used to confirm the operation to be performed based on the number of passing parameters. if two parameters are passed, the function is to determine whether the archive is expired. if three parameters are passed, the function is to cache and return the content. if two parameters are passed: if the cache period is not exceeded, the cached content (the content has been decrypted) is returned. if the cache period is exceeded, return the value of bool FALSE. if three parameters are passed: the content is cached (the transmitted content is not encrypted, and encryption is required before storage). If no value is returned, the following parameters are returned: string type, Archive 2nd parameters: int type, cache seconds 3rd parameters: string type, cache what [optional] usage: $ che [0] = Run. '_ cache/_ qian_main_0.che'; // slow archiving $ che [1] = 600; // cache seconds $ che [2] = Fun :: cache ($ che [0], $ che [1]); if (! Is_bool ($ che [2]) {// echo not exceeding the cache duration'
', Var_dump ('read '),'
'; Echo $ che [2]; unset ($ che);} else {// echo expired during caching'
', Var_dump ('write '),'
'; $ Che [2] = '??? '; Fun: Cache ($ che [0], $ che [1], $ che [2]); echo $ che [2]; unset ($ che );} // delete the slow archive Fun: Cache (Run. '_ super_main_0.che', 0, '');/**/public static function Cache () {$ o = func_get_args (); if (! Is_array ($ o) {throw new exception ('Error: '. _ LINE _.', parameters must be passed! '); Die () ;}$ count = count ($ o); switch ($ count) {case 2: // Determine whether the cache has expired (if it has not expired, the cached content is returned; if it has expired, FALSE is returned) // check whether the archive has an if (! File_exists ($ o [0]) {unset ($ o, $ count); return FALSE ;} // Obtain the last modification time $ o [3] = filemtime ($ o [0]); $ o [3] = (! Is_numeric ($ o [3]) or $ o [3] = $ o [1]) {unset ($ o, $ count); return FALSE ;} // return the decrypted content unset ($ count); return base64_decode (file_get_contents ($ o [0]); case 3: // cache the content // if the cache time is less than 1, delete the slow archive if ($ o [1] <1) {if (file_exists ($ o [0]) {@ unlink ($ o [0]);} unset ($ o, $ count); return;} // update the cached file_put_contents ($ o [0], base64_encode (''. $ o [2]. ''), LOCK_EX); if (file_exists ($ o [0]) {chmod ($ o [0], 0777);} unset ($ o, $ count ); return; default: unset ($ o, $ co Unt); throw new exception ('Error: '. _ LINE _.', the number of parameters is incorrect! '); Die ();}}

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.