S('test1',$data1,array('type'=>'Memcache'));
S('test2',$data2,array('type'=>'File'));
原因:第一条S方法执行后,则静态$cache对象被设置,而当调用第二条S方法时,因为$cache不为空导致无法设置文件缓存,而以memcache进行缓存。
tp原始代码:
function S($name,$value='',$options=null) {
static $cache = '';
if(is_array($options) && empty($cache)){
// 缓存操作的同时初始化
$type = isset($options['type'])?$options['type']:'';以下是改进的S方法:function S($name, $value = '', $options = null) {
static $cache = array();
!is_array($options) || !$options['type'] || $type = $options['type'];
!is_array($name) || !$name['type'] || $type = $name['type'];
$type = empty($type) ? C('DATA_CACHE_TYPE') : $type;
if(is_array($options) && empty($cache[$type])){
// 缓存操作的同时初始化
$cache[$type] = Cache::getInstance($type,$options);
}
elseif(is_array($name)) { // 缓存初始化
$cache[$type] = Cache::getInstance($type,$name);
return $cache;
}
elseif(empty($cache[$type])) { // 自动初始化
$cache[$type] = Cache::getInstance($type);
}
if('' === $value){ // 获取缓存
return $cache[$type]->get($name);
}elseif(is_null($value)) { // 删除缓存
return $cache[$type]->rm($name);
}else { // 缓存数据
if(is_array($options)) {
$expire = isset($options['expire']) ? $options['expire'] : NULL;
}else{
$expire = is_numeric($options) ? $options : NULL;
}
return $cache[$type]->set($name, $value, $expire);
}
}若我理解有问题,请跟帖指出! 最佳答案