mogu file cache
<?php
$config = array(
'work_dir'=>'D:SoftEasyPHP-12.1wwwhtmlcache',
'default_timeout'=>600,
);
class mogu_file_cache{
public $work_dir;
public $default_timeout;
function __construct(){
global $config;
if(is_writeable($config['work_dir'])){
$this->work_dir = $config['work_dir'];
$this->default_timeout = $config['default_timeout'];
}else{
return false;
}
}
function exists($key){
return $this->get($key);
}
/*
*
*/
function set($key, $value, $time = 0){
if($time == 0){
$time = $this->default_timeout;
}
$key = md5($key);
$cache_dir = $this->work_dir.'/'.substr($key, 0, 3).'/'.substr($key, 3, 3);
if(!file_exists($cache_dir)){
mkdir($cache_dir, 0755, true);
}
$result = array(
'value'=>$value,
'time'=>time() + $time
);
return file_put_contents($cache_dir.'/'.$key, serialize($result));
}
function get($key){
$key = md5($key);
$cache_dir = $this->work_dir.'/'.substr($key, 0, 3).'/'.substr($key, 3, 3);
$cache_file = $cache_dir.'/'.$key;
if(file_exists($cache_file)){
$result = unserialize(file_get_contents($cache_file));
if( $result['time'] > time() ){
return $result['value'];
}else{
unlink($cache_file);
return false;
}
}else{
return false;
}
}
function replace($key, $value, $time = 0){
return $this->set($key, $value, $time);
}
function del($key){
$key = md5($key);
$cache_dir = $this->work_dir.'/'.substr($key, 0, 3).'/'.substr($key, 3, 3);
unlink($cache_dir.'/'.$key);
}
function clear(){
}
}
$cache = new mogu_file_cache();
$cache->set('user%&^&*^%', 'mogu');
$result = $cache->get('user%&^&*^%');
var_dump($result);