thinkphp File Cache class code sharing
From the thinkphp file cache class code, here is not much nonsense, small partners to see their own notes.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 The |
<?php/** * @desc File Cache/class cache{const C_file = '/runtime/'; pri Vate $dir = '; Const EXT = '. TPL '; Private $filename = '; Public function __construct ($dir = ') {$this->dir = $dir }/** * @desc setting File Cache * @param string $key filename * @para M unkonw $data Cache data * @param int $expire Expiration Time/Public function set ($key, $data, $expire = 0) {$this->filename = dirname (__file__). Self::c_file. $this->dir. $key. Self::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 filename */Public function gets ($key) {//$filename = DirName (__file__). Self::c_f ILE. $this->dir. $key. Self::ext; if (!file_exists ($this->filename)) {return ' cached file no longer exists ';} else{$res = file_get_contents ($this->filename);} return $res; /** * @desc Delete file * @param string $key filename/Public Function del ($key) {unlink ($this->filename);} } $d ATA = Array (' name ' => ' song ', ' Age ' =>20, ' sex ' => ' mans ', ' favority ' =>array (' apple ', ' banana ')); $cache = new cache (); $cache->set (' cache ', $data); $cache->get (' cache '); $cache->del (' cache '); |