Function Reference/WP Cache:修订间差异

来自站长百科
跳转至: 导航、​ 搜索
(新页面: ==Role of WP_Cache== <tt>WP_Object_Cache</tt> is WordPress' class for caching data which may be computationally intensive to regenerate dynamically on every page load. It's defined in <tt...)
 
无编辑摘要
第1行: 第1行:
==Role of WP_Cache==
==Role of WP_Cache==
== WP_Cache角色==
<tt>WP_Object_Cache</tt> is WordPress' class for caching data which may be computationally intensive to regenerate dynamically on every page load. It's defined in <tt>wp-includes/cache.php</tt>.
<tt>WP_Object_Cache</tt> is WordPress' class for caching data which may be computationally intensive to regenerate dynamically on every page load. It's defined in <tt>wp-includes/cache.php</tt>.
<tt>WP_Object_Cache</tt> 是WordPress' class,是用来储存数据的,这些数据在每次网页载入的时候,可能很难计算。WP_Cache定义在<tt>wp-includes/cache.php</tt>。


Do not use the class directly in your code when writing plugins, but use the wp_cache functions listed here.
Do not use the class directly in your code when writing plugins, but use the wp_cache functions listed here.
编写插件的时候,不要直接地将class用在你的代码中,使用这里列出的wp_cache函数。


Caching to file to store the information across page loads is not enabled by default - this can be enabled by adding <tt>define('ENABLE_CACHE', true);</tt> to your wp-config.php file.
Caching to file to store the information across page loads is not enabled by default - this can be enabled by adding <tt>define('ENABLE_CACHE', true);</tt> to your wp-config.php file.
存储文件,在网页载入时,储存信息,这个功能并不是默认的-通过向wp-config.php文件添加<tt>define('ENABLE_CACHE', true);</tt>可以添加这个功能。


Within a single page loading caching does still occur so as to reduce the number of database queries as much as possible
Within a single page loading caching does still occur so as to reduce the number of database queries as much as possible
单一网页载入时,不会出现高速缓存,这样可以尽量地减少数据库查询


==wp_cache functions==
==wp_cache functions==
==wp_cache 函数==


Almost all of these functions take a:
Almost all of these functions take a:
几乎所有的这些函数都有一个:
* key: the key to indicate the value
* key: the key to indicate the value
*key:暗示参数值的关键词
* $data: the value you want to store
* $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: 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: number of seconds (by default 900)
* expire:秒数(默认为900秒)


<pre>wp_cache_add($key, $data, $flag = '', $expire = 0)</pre>
<pre>wp_cache_add($key, $data, $flag = '', $expire = 0)</pre>
This function first checks if there is a cached object on the given $key. If not, then it is saved, otherwise returns false.
This function first checks if there is a cached object on the given $key. If not, then it is saved, otherwise returns false.
<pre>wp_cache_add($key, $data, $flag = '', $expire = 0)</pre>
这个函数首先检查给出的$key是否有cached object。如果没有,就会保存,否则就会返回错误的。


<pre>wp_cache_delete($id, $flag = '') </pre>
<pre>wp_cache_delete($id, $flag = '') </pre>
Clears a given file.
Clears a given file.
<pre>wp_cache_delete($id, $flag = '') </pre>
清除给定的文件。


<pre>wp_cache_get($id, $flag = '')</pre>
<pre>wp_cache_get($id, $flag = '')</pre>
Gives back the value of the cached object if it did not expired. Otherwise gives back false.
Gives back the value of the cached object if it did not expired. Otherwise gives back false.
<pre>wp_cache_get($id, $flag = '')</pre>
如果储存的object没有期满,返回这个object的参数值。否则返回错误的。


<pre>wp_cache_replace($key, $data, $flag = '', $expire = 0)</pre>
<pre>wp_cache_replace($key, $data, $flag = '', $expire = 0)</pre>
Replaces the given cache if it exists, returns false otherwise.
Replaces the given cache if it exists, returns false otherwise.
<pre>wp_cache_replace($key, $data, $flag = '', $expire = 0)</pre>
如果存在高速缓存,取代给定的高速缓存,否则返回错误的。


<pre>wp_cache_set($key, $data, $flag = '', $expire = 0)</pre>
<pre>wp_cache_set($key, $data, $flag = '', $expire = 0)</pre>
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.
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.


<pre>wp_cache_set($key, $data, $flag = '', $expire = 0)</pre>
设置cache object的参数值。如果object已经存在,就会被覆盖,如果object不存在,会得到创建。
<pre>wp_cache_init() </pre>
<pre>wp_cache_init() </pre>
<pre>wp_cache_init() </pre>
Initializes a new cache object. This function is called by Wordpress at initialization if cacheing is enabled.
Initializes a new cache object. This function is called by Wordpress at initialization if cacheing is enabled.
初始化新的cache object。如果高速缓存正在运行,进行初始化过程时,WordPress会调用这个函数。


<pre>wp_cache_flush() </pre>
<pre>wp_cache_flush() </pre>
Clears all the cache files.
Clears all the cache files.
<pre>wp_cache_flush() </pre>
清除所有高速缓存文件。


<pre>wp_cache_close()</pre>
<pre>wp_cache_close()</pre>
Saves the cached object. This function is called by Wordpress at the shutdown action hook.
Saves the cached object. This function is called by Wordpress at the shutdown action hook.
保存cached object。在shutdown action hook时,WordPress调用了这个函数。


== Examples ==
== Examples ==
== 例子 ==


You can use WP_Object_Cache and Snoopy to cache offsite includes:
You can use WP_Object_Cache and Snoopy to cache offsite includes:
你可以使用WP_Object_Cache 和Snoopy高速缓存offsite includes:


  $news = wp_cache_get('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;
$news = wp_cache_get('news');
  if($news == false) {
  if($news == false) {
  $snoopy = new Snoopy;
  $snoopy = new Snoopy;
第55行: 第120行:


== Additional Resources ==
== Additional Resources ==
== 额外的资源 ==


* '''Dougal Campbell''' wrote a short guide on how to use the Wordpress Cache object from within your own plugins : [http://dougal.gunters.org/blog/2006/07/21/using-the-wordpress-object-cache Using the Wordpress Object Cache]
* '''Dougal Campbell''' wrote a short guide on how to use the Wordpress Cache object from within your own plugins : [http://dougal.gunters.org/blog/2006/07/21/using-the-wordpress-object-cache Using the Wordpress Object Cache]
* '''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 : [http://blog.ftwr.co.uk/wordpress/wp-cache-inspect/ WP Cache Inspect]
* '''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 : [http://blog.ftwr.co.uk/wordpress/wp-cache-inspect/ WP Cache Inspect]
* '''Peter Westwood'''拥有一个插件能够帮助人们分享cache,能使管理员快速浏览cache是怎样运行的以及哪个查询得到了cache: [http://blog.ftwr.co.uk/wordpress/wp-cache-inspect/ WP Cache Inspect]

2008年7月23日 (三) 13:56的版本

Role of WP_Cache

WP_Cache角色

WP_Object_Cache is WordPress' class for caching data which may be computationally intensive to regenerate dynamically on every page load. It's defined in wp-includes/cache.php.

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

Do not use the class directly in your code when writing plugins, but use the wp_cache functions listed here.

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

Caching to file to store the information across page loads is not enabled by default - this can be enabled by adding define('ENABLE_CACHE', true); to your wp-config.php file.

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

Within a single page loading caching does still occur so as to reduce the number of database queries as much as possible

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

wp_cache functions

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