ThinkPHP File Cache class code sharing,

Source: Internet
Author: User

ThinkPHP File Cache class code sharing,

The file cache code taken from ThinkPHP is not nonsense here. Let's take a look at the comments.

<? Php/*** @ desc File Cache */class Cache {const C_FILE = '/Runtime/'; private $ dir = ''; const EXT = '. tpl '; private $ filename = ''; public function _ construct ($ dir ='') {$ this-> dir = $ dir ;} /*** @ desc sets File Cache * @ param string $ key file name * @ param unkonw $ data cached data * @ param int $ expire expiration time */public function set ($ key, $ data, $ expire = 0) {$ this-> filename = dirname (_ FILE __). self: C_FILE. $ this-> dir. $ key. se Lf: EXT; if (file_exists ($ this-> filename) {$ res = $ this-> get ($ key); if (md5 ($ res) = md5 (json_encode ($ data) {return true ;}} if (! Is_dir (dirname ($ this-> filename) {mkdir (dirname ($ this-> filename), 0777);} $ source = fopen ($ this-> filename, 'W + '); fwrite ($ source, json_encode ($ data); fclose ($ source );} /*** @ desc get FILE * @ param string $ key FILE name */public function get ($ key) {// $ filename = dirname (_ FILE __). self: C_FILE. $ this-> dir. $ key. self: EXT; if (! File_exists ($ this-> filename) {return 'the cache file does not exist ';} else {$ res = file_get_contents ($ this-> filename);} return $ res ;} /*** @ desc delete file * @ param string $ key file name */public function del ($ key) {unlink ($ this-> filename );}} $ data = array ('name' => 'song', 'age' => 20, 'sex' => 'man ', 'favority '=> array ('apple', 'Banana'); $ cache = new Cache (); $ cache-> set ('cache', $ data ); // $ cache-> get ('cache'); // $ cache-> del ('cache ');

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.