wordpress缓存类WP_Object_Cache分析【续三】


/**
* 显示缓存状态
*
* 显示缓存取值成功、失败的次数和所有的缓存分组及组下的数据
*
*/
function stats() {
echo "<p>";
echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
echo "</p>";

foreach ($this->cache as $group => $cache) {
echo "<p>";
echo "<strong>Group:</strong> $group<br />";
echo "<strong>Cache:</strong>";
echo "<pre>";
print_r($cache);
echo "</pre>";
}
}

/**
* PHP4风格的构造函数; 调用PHP 5风格的构造函数

* @返回值 WP_Object_Cache
*/
function WP_Object_Cache() {
return $this->__construct();
}

/**
* Sets up object properties; PHP 5 style constructor
*
* @since 2.0.8
* @返回值 null|WP_Object_Cache If cache is disabled, returns null.
*/
function __construct() {
/**
* @todo This should be moved to the PHP4 style constructor, PHP5
* already calls __destruct()
*/
//注册一个结束时的回调函数
register_shutdown_function(array(&$this, "__destruct"));
}

/**
* 析构函数 *
* Called upon object destruction, which should be when PHP ends
*
* @返回值 bool True value. Won't be used by PHP
*/
function __destruct() {
return true;
}

总体分析,wp的缓存类,实现了数据库一样的增删改查功能。只不过数据是保存在内存里,而不是数据库中。

wp中调用缓存对象的方法是并不是直接在代码中像这样调用

global $wp_object_cache;

return $wp_object_cache->add($key, $data, $flag, $expire);

而是封装在一个方法里:

function wp_cache_add($key, $data, $flag = '', $expire = 0) {
global $wp_object_cache;

return $wp_object_cache->add($key, $data, $flag, $expire);
}

调用的时候,像这样调用:

wp_cache_add($comment->comment_ID, $comment, 'comment');

由原来的两层结构,中间又加一层封装,变成三层,提高了代码的重用性、扩展性和可维护性。这正是我们写程序时应该追求的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值