When I was browsing in an Internet cafe, due to the movie search function (user input) on the page
Since this function cannot be made static, it can only be made dynamic. When used dynamically, it will bring great challenges to the database and server pressure
So we can only use the method of caching data
The forms of data caching include:
1. Cache data into memory. I believe everyone will think of Memcached. Memcached is a high-performance distributed memory cache server. The general purpose of use is to reduce the number of database accesses by caching database query results to increase the speed and scalability of dynamic web applications.
2. Use files to cache data. Save the data to the file in the form of key=>value, where key refers to the file name. The uniqueness of the key must be ensured here
Set the cache time of the file. If it is out of date, get the data from the database and save it to the file,
The following is a file cache class:
1. Caching data
2. Get data
3. Determine whether cached data exists
4. Delete a certain cached data
5. Clear outdated cache data
6. Clear all cached data
class Inc_FileCache{
Private $cacheTime = 3600; //Default cache time
Private $cacheDir = CACHE_DIR; //Cache absolute path
private $md5 = true; //Whether to encrypt the key
private $suffix = ".php"; //Set file suffix
Public function __construct($config){
If( is_array( $config ) ){
foreach( $config as $key=>$val ){
$this->$key = $val;
}
}
//Set cache
Public function set($key,$val,$leftTime=null){
$key = $this->md5 ? md5($key) : $key;
$leftTime = $leftTime ? $leftTime : $this->cacheTime;
!file_exists($this->cacheDir) && mkdir($this->cacheDir,0777);
$file = $this->cacheDir.'/'.$key.$this->suffix;
$val = serialize($val);
@file_put_contents($file,$val) or $this->error(__line__,"File writing failed");
@chmod($file,0777) or $this->error(__line__,"Failed to set file permissions");
@touch($file,time()+$leftTime) or $this->error(__line__,"Failed to change file time");
}
//Get cache
Public function get($key){
$this->clear();
If( $this->_isset($key) ){
$key_md5 = $this->md5 ? md5($key) : $key;
$file = $this->cacheDir.'/'.$key_md5.$this->suffix;
$val = file_get_contents($file);
return unserialize($val);
}
return null;
}
//Determine whether the question is valid
Public function _isset($key){
$key = $this->md5 ? md5($key) : $key;
$file = $this->cacheDir.'/'.$key.$this->suffix;
if( file_exists($file) ){
if( @filemtime($file) >= time() ){
return true;
}else{
@unlink($file);
return false;
}
}
return false;
}
//删除文件
public function _unset($key){
if( $this->_isset($key) ){
$key_md5 = $this->md5 ? md5($key) : $key;
$file = $this->cacheDir.'/'.$key_md5.$this->suffix;
return @unlink($file);
}
return false;
}
//清除过期缓存文件
public function clear(){
$files = scandir($this->cacheDir);
foreach ($files as $val){
if (@filemtime($this->cacheDir."/".$val) < time()){
@unlink($this->cacheDir."/".$val);
}
}
}
//清除所有缓存文件
public function clear_all(){
$files = scandir($this->cacheDir);
foreach ($files as $val){
@unlink($this->cacheDir."/".$val);
}
}
private function error($line,$msg){
die("出错文件:".__file__."/n出错行:$line/n错误信息:$msg");
}
}
在页面中的调用方法如下:
$cacheFile = new Inc_FileCache(array('cacheTime'=>60,'suffix'=>'.php'));
//得到电影热播榜
$where = " where pid=75";
$moviehotModel = $this->getM('moviehot');
$moviehotCount = $moviehotModel->getCount($where);
if( !$cacheFile->_isset($where.$moviehotCount.'moviehot') ){
$moviehotResult = $moviehotModel->getList(" WHERE pid=75 ",'0,10',"orderby desc");
if(count($moviehotResult) > 0) {
$cacheFile->set($where.$moviehotCount.'moviehot',$moviehotResult);
}
}else{
$moviehotResult = $cacheFile->get($where.$moviehotCount.'moviehot');
}
$this->tpl['moviehotResult'] = $moviehotResult;
大家如果还有什么好的文件缓存的代码可以拿来共享一下
摘自 ms_X0828的专栏