Copy Code code as follows:
<?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 Set File cache
* @param string $key filename
* @param 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 files
* @param string $key filename
*/
Public function Get ($key) {
$filename = DirName (__file__). Self::c_file. $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 Files
* @param string $key filename
*/
Public Function del ($key) {
Unlink ($this->filename);
}
}
$data = Array (' name ' => ' song ', ' Age ' =>20, ' sex ' => ' mans ', ' favority ' =>array ' (' apple ', ' banana '));
$cache = new cache ();
$cache->set (' cache ', $data);
$cache->get (' cache ');
$cache->del (' cache ');