function set_cache($key,$value,$timeout=0){
$cache_path = _SITE_CACHE."_cache_".$key;
//cache content
$cache_content = base64_encode(serialize($value));
//cache file
$cache_file_x = $cache_path."x";
$cache_file_y = $cache_path."y";
//加入缓存时间
if( $timeout > 0 ) $cache_content.= "\n".$timeout;
//cache time
if( file_exists($cache_file_x) ) $cache_x_time = filemtime($cache_file_x);
if( file_exists($cache_file_y) ) $cache_y_time = filemtime($cache_file_y);
//write cache
if( !file_exists($cache_file_x) ) file_put_contents($cache_file_x,$cache_content,LOCK_EX);
if( !file_exists($cache_file_y) ) file_put_contents($cache_file_y,$cache_content,LOCK_EX);
//update cache
if( $cache_x_time>$cache_y_time ){
#echo "write y [".$cache_file_y."]<br/>\n";
file_put_contents($cache_file_y,$cache_content,LOCK_EX);
}
else{
#echo "write x [".$cache_file_x."]<br/>\n";
file_put_contents($cache_file_x,$cache_content,LOCK_EX);
}
}
function get_cache($key,$model=0){
//cache path
$cache_path = _SITE_CACHE."_cache_".$key;
//cache file
$cache_file_x = $cache_path."x";
$cache_file_y = $cache_path."y";
if( !file_exists($cache_file_x) && !file_exists($cache_file_y) ) return false;
//cache time
$cache_x_time = filemtime($cache_file_x);
$cache_y_time = filemtime($cache_file_y);
//
if( $cache_x_time>$cache_y_time ){
$cache_file = $cache_file_x;
$update_time = $cache_x_time;
}
else{
$cache_file = $cache_file_y;
$update_time = $cache_y_time;
}
#echo "use [".$cache_file."]<br/>\n";
$cache_arrays = explode("\n",file_get_contents($cache_file));
if( count($cache_arrays) == 2 && 0 === $model && time()-$update_time > $cache_arrays[1] ){
return false;
}
else return unserialize(base64_decode($cache_arrays[0]));
}