WordPress可以使用插件缓存,但其实在WordPress内部也有一套缓存机制,你可以使用很多函数轻松实现缓存。 为了让大家认识和了解这个功能,我使用一个简单的例子,缓存一个feed并在前端显示。

最先的缓存方案是WordPress 2.3中基于文件的缓存。该缓存是可选的,并需配置一些参数。 可通过下面的常量激活缓存: define ( ‘ENABLE_CACHE’, true);

缓存最大改进的还是在2.6版本,此时变为基于对象的缓存。缓存主要依赖于服务器速度,这主要是考虑到最大化地利用服务器资源而不是什么都交由 WordPress处理。这样一来,WordPress中的缓存就无所谓的“被激活” ,因为它一直处于激活的状态。因此,服务器是否有足够的存储就显得尤为重要了, WordPress需要最低需要32兆的存储空间- 但并非每次都如此。例如,当进行更新的时候,需要128兆的空间,很多情况下都是无法满足的,因此就无法更新。

不过这个不是我们今天讨论的重点。我想向大家介绍如何利用自己的扩展使用缓存。我举个利用关键函数来实现缓存的例子。 你可以在wp-includes/cache.php或 Codex里查找到所有的函数。

你可以使用下列函数来实现添加缓存。

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

相反的,要删除缓存数据可以通过下面的方法实现。

wp_cache_delete($id, $flag = ”)

提取缓存数据:

wp_cache_get($id, $flag = ”)

替换缓存数据:

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

至于缓存feed,WordPress1.5以后版本的feed都是通过fetch_rss()函数来加载的。

$mycache = wp_cache_get( ‘mycache’ ); // 获取键名为 “mycache” 的缓存
if ($mycache == false) { // 如果没有数据,那么
$mycache = fetch_rss(“http://mycache.com/feed/”); // 解析feed
wp_cache_set( ‘mycache’, $mycache ); // 将内容保存进 “mycache” 中
}
var_dump( $mycache ); // 显示内容

可通过变量$wp_object_cache或使用插件Debug Objects WP Cache Inspect 进一步了解WordPress缓存。Debug Objects 就是为此而产生,并只能用于开发环境中。

附原文:

Use WordPress Cache

WordPress has an internal cache, also for extensions can be used. There are various functions available and you don’t have to create something new, you can easily use the cache functionality of WordPress.
To get to know and understand the features a little bit, I use a small example, therefore I cache in the following tutorial a feed, which should be displayed in the frontend.

All functions of cache are in the Codex by WordPress listed, so a look at the Codex is worthwhile if you deal with the syntax.

The first cache solution came with WordPress 2.3 and was file based. The cache was optional and had some parameters to configure.
You were able to activate via following constant: define ( 'ENABLE_CACHE', true);

The biggest improvement happened in version 2.6, in which the cache has changed to an object-oriented solution. Therefore the opportunities for cache usage are lying rather on the server and not explicitly on WordPress. This was mainly realized in order to maximize the resources of the server and not to be handed over to WordPress. With this introduction, the cache of WordPress has no longer explicitly be activated, it is always active. Therefore, it is important that the server has a certain minimum amount of RAM available, WordPress requires 32 MByte – but that is not always the case, for example, when updating the core, it contains a call which defines the RAM to 128MByte, which in many cases is not available and therefore the update does not work.
But this is not the topic of this post today, because I want to explain how to use the cache in your own extensions. So back to the syntax and I just start with the key functions to realize a small example.

All functions can be found in wp-includes/cache.php, or alternatively in Codex.

To reset the cache, insofar there is no data for this key, you can use the following function.

/**
 * @param int|string $key The cache ID to use for retrieval later
 * @param mixed $data The data to add to the cache store
 * @param string $flag The group to add the cache to
 * @param int $expire When the cache data should be expired
 */
wp_cache_add($key, $data, $flag = '', $expire = 0)

To delete cache data for a key, here is the opposite.

/**
 * @param int|string $id What the contents in the cache are called
 * @param string $flag Where the cache contents are grouped
 * @return bool True on successful removal, false on failure
 */
wp_cache_delete($id, $flag = '')

Fetching data for a key is done by using:

/**
 * @param int|string $id What the contents in the cache are called
 * @param string $flag Where the cache contents are grouped
 * @return bool|mixed False on failure to retrieve contents or the cache
 */
wp_cache_get($id, $flag = '')

Should within the cache to a key the content to be replaced, then the following function will work.

/**
 * @param int|string $id What to call the contents in the cache
 * @param mixed $data The contents to store in the cache
 * @param string $flag Where to group the cache contents
 * @param int $expire When to expire the cache contents
 * @return bool False if cache ID and group already exists, true on success
 */
wp_cache_replace($key, $data, $flag = '', $expire = 0)

But now a small example, which caches the feed. The feed gets loaded by fetch_rss(), a function of WordPress which is available since version 1.5.

$mycache = wp_cache_get( 'mycache' ); // fetch data from cache to the key "mycache"
if ($mycache == false) { // if no data, then
	$mycache = fetch_rss("http://mycache.com/feed/"); // parse feed
	wp_cache_set( 'mycache', $mycache ); // save feed content to key "mycache"
}
var_dump( $mycache ); // display content

FYI: You get an insight into the cache of WordPress easily via the variable $wp_object_cache or using the Plugin Debug Objects or WP Cache Inspect; whereas Debug Objects explicitly has been made for this and should be used in development environments only.

来自yaronspace.cn  本文链接:http://yaronspace.cn/blog/archives/400