Function Reference/WP Cache

来自站长百科
Xxf3325讨论 | 贡献2008年7月29日 (二) 16:01的版本
跳转至: 导航、​ 搜索

WP_Cache角色

WP_Object_Cache 是WordPress' class,是用来储存数据的,这些数据在每次网页载入的时候,可能很难计算。WP_Cache定义在wp-includes/cache.php

编写插件的时候,不要直接地将class用在你的代码中,使用这里列出的wp_cache函数。

存储文件,在网页载入时,储存信息,这个功能并不是默认的-通过向wp-config.php文件添加define('ENABLE_CACHE', true);可以添加这个功能。

单一网页载入时,不会出现高速缓存,这样可以尽量地减少数据库查询

wp_cache 函数

Almost all of these functions take a:

几乎所有的这些函数都有一个:

  • key: the key to indicate the value
  • key:暗示参数值的关键词
  • $data: the value you want to store
  • $data:你想要储存的参数值
  • flag: optional: this is a way of grouping your cache data. If you use a flag, your data will be stored in a subdirectory of your cache directory
  • flag: optional:这是组合缓存数据的一种方式。如果是使用标记,数据就会储存在高速缓存目录的子目录中
  • expire: number of seconds (by default 900)
  • expire:秒数(默认为900秒)
wp_cache_add($key, $data, $flag = '', $expire = 0)

This function first checks if there is a cached object on the given $key. If not, then it is saved, otherwise returns false.

wp_cache_add($key, $data, $flag = '', $expire = 0)

这个函数首先检查给出的$key是否有cached object。如果没有,就会保存,否则就会返回错误的。


wp_cache_delete($id, $flag = '') 

Clears a given file.

wp_cache_delete($id, $flag = '') 

清除给定的文件。

wp_cache_get($id, $flag = '')

Gives back the value of the cached object if it did not expired. Otherwise gives back false.

wp_cache_get($id, $flag = '')

如果储存的object没有期满,返回这个object的参数值。否则返回错误的。

wp_cache_replace($key, $data, $flag = '', $expire = 0)

Replaces the given cache if it exists, returns false otherwise.

wp_cache_replace($key, $data, $flag = '', $expire = 0)

如果存在高速缓存,取代给定的高速缓存,否则返回错误的。

wp_cache_set($key, $data, $flag = '', $expire = 0)

Sets the value of the cache object. If the object already exists, then it will be overwritten, if the object does not exists it will be created.

wp_cache_set($key, $data, $flag = '', $expire = 0)

设置cache object的参数值。如果object已经存在,就会被覆盖,如果object不存在,会得到创建。

wp_cache_init() 
wp_cache_init() 

Initializes a new cache object. This function is called by Wordpress at initialization if cacheing is enabled.

初始化新的cache object。如果高速缓存正在运行,进行初始化过程时,WordPress会调用这个函数。

wp_cache_flush() 

Clears all the cache files.

wp_cache_flush() 

清除所有高速缓存文件。

wp_cache_close()

Saves the cached object. This function is called by Wordpress at the shutdown action hook.

保存cached object。在shutdown action hook时,WordPress调用了这个函数。

Examples

例子

You can use WP_Object_Cache and Snoopy to cache offsite includes:

你可以使用WP_Object_Cache 和Snoopy高速缓存offsite includes:

$news = wp_cache_get('news');
if($news == false) {
	$snoopy = new Snoopy;
	$snoopy->fetch('http://example.com/news/'); 
	$news = $snoopy->results;
	wp_cache_set('news', $news);
} 
echo $news;


$news = wp_cache_get('news');

if($news == false) {
	$snoopy = new Snoopy;
	$snoopy->fetch('http://example.com/news/'); 
	$news = $snoopy->results;
	wp_cache_set('news', $news);
} 
echo $news;


Additional Resources

额外的资源

  • Dougal Campbell编写一个简短的指南,关于怎样使用插件中的WordPress Cache object:
  • Peter Westwood has a plugin to help people analyse the behaviour of the cache, provides the administrator with a quick overview of how the cache is performming and what queries are cached : WP Cache Inspect
  • Peter Westwood拥有一个插件能够帮助人们分享cache,能使管理员快速浏览cache是怎样运行的以及哪个查询得到了cache: WP Cache Inspect