Function Reference/WP Cache:修订间差异

来自站长百科
跳转至: 导航、​ 搜索
无编辑摘要
无编辑摘要
 
第10行: 第10行:


==wp_cache 函数==
==wp_cache 函数==
Almost all of these functions take a:


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


* key: the key to indicate the value
* key:暗示参数值的关键词
 
*key:暗示参数值的关键词
 
* $data: the value you want to store


* $data:你想要储存的参数值
* $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:这是组合缓存数据的一种方式。如果是使用标记,数据就会储存在高速缓存目录的子目录中
* flag: optional:这是组合缓存数据的一种方式。如果是使用标记,数据就会储存在高速缓存目录的子目录中


* expire: number of seconds (by default 900)
* expire:秒数(默认为900秒)
* expire:秒数(默认为900秒)
<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.


<pre>wp_cache_add($key, $data, $flag = '', $expire = 0)</pre>
<pre>wp_cache_add($key, $data, $flag = '', $expire = 0)</pre>
这个函数首先检查给出的$key是否有cached object。如果没有,就会保存,否则就会返回错误的。
这个函数首先检查给出的$key是否有cached object。如果没有,就会保存,否则就会返回错误的。
<pre>wp_cache_delete($id, $flag = '') </pre>
Clears a given file.


<pre>wp_cache_delete($id, $flag = '') </pre>
<pre>wp_cache_delete($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.
<pre>wp_cache_get($id, $flag = '')</pre>
<pre>wp_cache_get($id, $flag = '')</pre>
如果储存的object没有期满,返回这个object的参数值。否则返回错误的。
如果储存的object没有期满,返回这个object的参数值。否则返回错误的。
<pre>wp_cache_replace($key, $data, $flag = '', $expire = 0)</pre>
Replaces the given cache if it exists, returns false otherwise.


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


<pre>wp_cache_set($key, $data, $flag = '', $expire = 0)</pre>
<pre>wp_cache_set($key, $data, $flag = '', $expire = 0)</pre>
第62行: 第36行:


<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.


初始化新的cache object。如果高速缓存正在运行,进行初始化过程时,WordPress会调用这个函数。
初始化新的cache object。如果高速缓存正在运行,进行初始化过程时,WordPress会调用这个函数。
<pre>wp_cache_flush() </pre>
Clears all the cache files.


<pre>wp_cache_flush() </pre>
<pre>wp_cache_flush() </pre>
第74行: 第43行:


<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.
保存cached object。在shutdown action hook时,WordPress调用了这个函数。
保存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:
你可以使用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');
$news = wp_cache_get('news');
  if($news == false) {
  if($news == false) {
第104行: 第55行:
  }  
  }  
  echo $news;
  echo $news;
== 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'''编写一个简短的指南,关于怎样使用插件中的WordPress Cache object:
* '''Dougal Campbell'''编写一个简短的指南,关于怎样使用插件中的WordPress Cache object:
 
[http://dougal.gunters.org/blog/2006/07/21/using-the-wordpress-object-cache Using the Wordpress Object Cache]
* '''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]
* '''Peter Westwood'''拥有一个插件能够帮助人们分享cache,能使管理员快速浏览cache是怎样运行的以及哪个查询得到了cache: [http://blog.ftwr.co.uk/wordpress/wp-cache-inspect/ WP Cache Inspect]

2008年7月29日 (二) 16:14的最新版本

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 函数[ ]

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

  • key:暗示参数值的关键词
  • $data:你想要储存的参数值
  • flag: optional:这是组合缓存数据的一种方式。如果是使用标记,数据就会储存在高速缓存目录的子目录中
  • expire:秒数(默认为900秒)
wp_cache_add($key, $data, $flag = '', $expire = 0)

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

wp_cache_delete($id, $flag = '') 

清除给定的文件。

wp_cache_get($id, $flag = '')

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

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

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

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

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

wp_cache_init() 

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

wp_cache_flush() 

清除所有高速缓存文件。

wp_cache_close()

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

例子[ ]

你可以使用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;

额外的资源[ ]

  • Dougal Campbell编写一个简短的指南,关于怎样使用插件中的WordPress Cache object:

Using the Wordpress Object Cache

  • Peter Westwood拥有一个插件能够帮助人们分享cache,能使管理员快速浏览cache是怎样运行的以及哪个查询得到了cache: WP Cache Inspect