linux wptmp文件分析,wordpress缓存类WP_Object_Cache分析

class WP_Object_Cache {

//所有的缓存数据都保存在这个变量里

var $cache = array ();

//缓存中不存在的变量

//调用get方法是取不到的值,都保存在这里

var $non_existant_objects = array ();

//记数 记录取值时成功的次数

var $cache_hits = 0;

//记数 记录取值时失败的次数

var $cache_misses = 0;

/**

* 向缓存中添加新数据

*

* @使用 WP_Object_Cache::get 检查缓存中是否已存在该数据.

* @使用 WP_Object_Cache::set 检查了缓存中数据不存在后,设置新值

*

* @参数 int|string $id 标识缓存中的数据

* @参数 mixed $data 要保存的数据

* @参数 string $group 缓存内容的分组 wp把数据按不同的类型分组,如comment、bookmark等

* @参数 int $expire 缓存内容的过期时间 从未使用过

* @返回值 bool 缓存ID和组存在时返回false,否则返回true

*/

function add($id, $data, $group = 'default', $expire = '') {

if (empty ($group))

$group = 'default';

//如果组$group下id为$id的值已经存在,则不能新增,返回false

if (false !== $this->get($id, $group, false))

return false;

return $this->set($id, $data, $group, $expire);

}

/**

* 删除组中特定缓存ID的数据

*

* 如果缓存ID不存在并且$force=false时,什么也不会发生,$force默认为false.

*

* 成功的时候$non_existant_objects中会保存该ID,表示这个缓存数据不存在.

*

* @参数 int|string $id 标识缓存中的数据

* @参数 string $group 缓存的分组

* @参数 bool $force Optional. 是否强制删除组中的缓存ID

* @返回值 bool 没有删除时返回false,删除成功时返回true.

*/

function delete($id, $group = 'default', $force = false) {

if (empty ($group))

$group = 'default';

if (!$force && false === $this->get($id, $group, false))

return false;

unset ($this->cache[$group][$id]);

$this->non_existant_objects[$group][$id] = true;

return true;

}

/**

* 清空所有缓存数据

*

* @返回值 bool 永远返回true

*/

function flush() {

$this->cache = array ();

return true;

}

/**

* 取得存在的缓存数据

*

* 根据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;

}

/**

* 显示缓存状态

*

* 显示缓存取值成功、失败的次数和所有的缓存分组及组下的数据

*

*/

function stats() {

echo "

";

echo "Cache Hits: {$this->cache_hits}
";

echo "Cache Misses: {$this->cache_misses}
";

echo "

";

foreach ($this->cache as $group => $cache) {

echo "

";

echo "Group: $group
";

echo "Cache:";

echo "

";

print_r($cache);

echo "

";

}

}

/**

* 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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值