wordpress缓存类WP_Object_Cache分析【续二】


/**
* 取得存在的缓存数据
*
* 根据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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值