/**
* 取得存在的缓存数据
*
* 根据ID和分组搜索数据,如果数据存在,返回数据
*
* 失败时检查$non_existant_objects属性,如果缓存ID和分组在这个属性中存在,直接返回.
* 如果不存在,则cache_misses加1,并把ID和分组添加到$non_existant_objects中。
*
*
* @参数 int|string $id 标识缓存中的数据
* @参数 string $group 缓存所在的分组
* @返回值 bool|mixed 失败时返回false,成功时返回ID对应的数据
*/
function get($id, $group = 'default') {
if (empty ($group))
$group = 'default';
if (isset ($this->cache[$group][$id])) {
$this->cache_hits += 1;
if ( is_object($this->cache[$group][$id]) )
return wp_clone($this->cache[$group][$id]);
else
return $this->cache[$group][$id];
}
if ( isset ($this->non_existant_objects[$group][$id]) )
return false;
$this->non_existant_objects[$group][$id] = true;
$this->cache_misses += 1;
return false;
}
/**
* 替换已经存在的数据
*
* @see WP_Object_Cache::set()
*
* @参数 int|string $id 标识缓存中的数据
* @参数 mixed $data 要保存的数据
* @参数 string $group 缓存分组
* @参数 int $expire 缓存过期时间
* @返回值 bool 不存在时返回false,替换成功时返回true
*/
function replace($id, $data, $group = 'default', $expire = '') {
if (empty ($group))
$group = 'default';
if (false === $this->get($id, $group, false))
return false;
return $this->set($id, $data, $group, $expire);
}
/**
* 把数据保存在缓存中
*
* 缓存数据按照$group进行分组. 不同组中可以添加相同ID的数据.
*
* $expire参数没有使用,因为php页面执行结束时,缓存会自动结束. 一般在使用文件来缓存数据的插件中使用。
*
* @参数 int|string $id 标识缓存中的数据
* @参数 mixed $data The 要保存的数据
* @参数 string $group 数据所在的分组
* @参数 int $expire 过期时间,未使用
* @返回值 bool 永远返回true
*/
function set($id, $data, $group = 'default', $expire = '') {
if (empty ($group))
$group = 'default';
if (NULL === $data)
$data = '';
if ( is_object($data) )
$data = wp_clone($data);
//保存值到全局变量中
$this->cache[$group][$id] = $data;
//如果这个选项在non_existant_objects中存在,则取消存在
if(isset($this->non_existant_objects[$group][$id]))
unset ($this->non_existant_objects[$group][$id]);
return true;
}
续三: [url]http://baiyuxiong.iteye.com/blog/769002[/url]