wordpress api_WordPress瞬态API的完整指南

本文详细介绍了WordPress的Transients API,它允许开发者存储具有有效期的数据。不同于Options API,Transients API在持久性缓存未启用时存储数据于MySQL,否则使用Object Cache。文章讲解了创建、检索、删除瞬态的方法,并给出了缓存特定类别帖子的示例,强调了Transients API在缓存数据方面的优势。
摘要由CSDN通过智能技术生成

wordpress api

WordPress has supported the Transients API since version 2.8 but still many WordPress developers are unaware of its existence and uses. In a nutshell, WordPress Transients API lets us store key-value pairs of data with an expiration time.

WordPress自2.8版开始支持Transients API,但是仍然有许多WordPress开发人员不知道它的存在和使用。 简而言之,WordPress Transients API允许我们存储具有有效期的键-值数据对。

In this tutorial, we will cover how to use this API in depth. We will also see how it’s different from Options API, how it interacts with the WordPress caching system and few of its use cases.

在本教程中,我们将深入介绍如何使用此API。 我们还将看到它与Options API的不同之处,它与WordPress缓存系统的交互方式以及一些用例。

WordPress Transients API

选项API与瞬态API (Options API vs Transients API)

Most WordPress developers are aware of WordPress Options API. Options API lets us store key-value pairs of data permanently in the database. What many WordPress developers don’t realise is that Options API implements a caching layer (i.e. WordPress Object Cache) to cache the options. If a persistent cache is not enabled then a new caching session is created on every HTTP request otherwise the persistent cache is used by Options API.

大多数WordPress开发人员都知道WordPress Options API 。 通过Options API,我们可以将键-值数据对永久存储在数据库中。 许多WordPress开发人员没有意识到的是,Options API实现了一个缓存层(即WordPress Object Cache)来缓存选项。 如果未启用持久性缓存,则将在每个HTTP请求上创建一个新的缓存会话,否则,持久性缓存将由Options API使用。

Almost every WordPress API interacts with MySQL using the WordPress Object Cache to cache the data to prevent multiple MySQL hits.

几乎每个WordPress API都使用WordPress对象缓存与MySQL交互,以缓存数据以防止多个MySQL命中。

Transients API works a little different than all the other APIs. It stores the key-value pairs of data in MySQL only if a persistent cache is not enabled otherwise it uses the Object Cache only. Whereas all other APIs use both to synchronise the data to make sure the data is persistent. Therefore Transients are not persistent i.e. shouldn’t be used to store critical data. So Transients API is great for caching data.

Transients API的工作方式与所有其他API略有不同。 仅当未启用持久性高速缓存时,它才在MySQL中存储键/值数据对,否则仅使用对象高速缓存。 而所有其他API都使用这两者来同步数据以确保数据是持久的。 因此,瞬变不是持久的,即不应用于存储关键数据。 因此, Transients API非常适合缓存数据

Note: If a persistent cache is not enabled then Transients API uses Options API to store the key-value pairs of data otherwise it uses the Object Cache directly. Transients are stored in Options table. Every transient is made up of two options i.e. key-value pair data and key-value pair expiration date.

注意:如果未启用持久性缓存,则Transients API将使用Options API来存储数据的键值对,否则将直接使用对象缓存。 瞬态存储在“选项”表中。 每个瞬态都由两个选项组成,即键值对数据和键值对到期日期。

创建一个瞬变 (Create a Transient)

To set a transient we need to use set_transient() function. This function takes three parameters:

要设置瞬变,我们需要使用set_transient()函数。 此函数采用三个参数:

  1. Transient name(required): Must be a string. Length of the string cannot be more than 40 characters otherwise it will fail to create the transient.

    临时名称(必填):必须为字符串。 字符串的长度不能超过40个字符,否则将无法创建过渡。
  2. Transient value(required): Must be a string. If you pass a object or array then it’s serialized i.e. converted to a string.

    瞬态值(必填):必须为字符串。 如果传递对象或数组,则将其序列化,即转换为字符串。

  3. Expiration Seconds(optional): Seconds within which the transient will expire in. Transient may also expire before the expiration time because cached data (i.e. data stored in Object cache) is volatile.

    Expiration Seconds(可选秒数):瞬变将在其中终止的秒数。由于缓存的数据(即存储在对象缓存中的数据)是易失的,因此瞬变也可能在到期时间之前到期。

Here is an example of code using set_transient() function:

这是使用set_transient()函数的代码示例:

set_transient("Website", "SitePoint", 3600);

Here we are storing a key named ‘Website’ with value ‘SitePoint’ for 1 hour. After 1 hour the key will not be accessible anymore.

在这里,我们将一个名为“网站”的密钥存储为1小时,其值为“ SitePoint”。 1小时后,密钥将不再可用。

set_transient returns true if the transient is created successfully otherwise it returns false.

如果成功创建了瞬变,则set_transient返回true,否则返回false。

If you don’t provide an expiration time or provide ‘0’ as the expiration time then it never expires the transient.

如果您不提供到期时间或不提供“ 0”作为到期时间,则它永远不会使瞬变到期。

Note: If expiration time is not provided or if expiration time is ‘0’ then transients are autoloaded (i.e. loaded into memory whenever a page is requested).

注意:如果未提供到期时间或到期时间为“ 0”,则瞬态将自动加载(即,每当请求页面时都将其加载到内存中)。

Transients API also provides another function to create a transient i.e. set_site_transient. It also takes the same three parameters like set_transient. Most of the functionality between them is the same. The differences between set_transient and set_site_transient are:

瞬态API还提供了另一个创建瞬态的函数,即set_site_transient 。 它还采用相同的三个参数,例如set_transient 。 它们之间的大多数功能是相同的。 set_transientset_site_transient之间的区别是:

  1. When set_site_transient is used with a Multi Site network then the transient is available for all the sites in the network.

    set_site_transient与多站点网络一起使用时,该瞬变可用于网络中的所有站点。

  2. Whether there is a expiration time or not, transients created using set_site_transient are always auto loaded.

    无论是否有到期时间,使用set_site_transient创建的瞬变始终会自动加载。

Finally, if you run set_transient of an existing transient key, then the value and expiration time is updated with the newly provided value and expire time. The expire time is calculated from the first time the transient was set.

最后,如果运行现有瞬态键的set_transient ,那么将使用新提供的值和到期时间来更新值和到期时间。 失效时间是从第一次设置瞬变开始计算的。

检索瞬态 (Retrieving a Transient)

To retrieve a stored transient you need to use get_transient function. It just takes one parameter i.e. the name of the transient.

要检索存储的瞬态,您需要使用get_transient函数。 它只需要一个参数,即瞬变的名称。

$value = get_transient("Website");

if($value === false)
{
	echo "Expired or not found";
}

get_transient returns false if the transient is expired or is not present. Otherwise it returns the value of the transient.

如果瞬变已过期或不存在,则get_transient返回false。 否则,它将返回瞬态值。

A transient will return false if it’s expired or not found, therefore you should never store boolean values in a transient. If you want to store boolean values then use the integers form i.e 0 or 1.

如果瞬变已过期或未找到,则瞬变将返回false,因此您永远不应在瞬变中存储布尔值。 如果要存储布尔值,请使用整数形式,即0或1。

If you have set a transient using set_site_transient then use get_site_transient to retrieve it instead of get_transient.

如果你已经使用设置了短暂set_site_transient然后使用get_site_transient来检索,而不是它get_transient

删除瞬态 (Deleting a Transient)

To delete a transient you need to use delete_transient function. It takes only one parameter i.e. the name of the transient.

要删除瞬变,您需要使用delete_transient函数。 它仅采用一个参数,即瞬变的名称。

Here’s an example:

这是一个例子:

delete_transient("Website");

It returns true if the transient was deleted successfully. If the transient is not found or due to another reason, it can’t delete the transient and then it returns false.

如果瞬态已成功删除,则返回true。 如果找不到瞬态或由于其他原因,则无法删除瞬态,然后返回false。

If you have set a transient using set_site_transient then use delete_site_transient to delete it instead of delete_transient.

如果使用set_site_transient设置了瞬态,则使用delete_site_transient而不是delete_transient删除它。

检索和缓存特定类别的帖子 (Retrieving and Caching Posts of a Particular Category)

The transients API can be used to cache anything. Most plugins use this API to cache data. For the sake of an example, let’s see how to retrieve and cache posts of a category.

瞬态API可用于缓存任何内容。 大多数插件使用此API来缓存数据。 为了举例说明,让我们看看如何检索和缓存类别的帖子。

<?php
  if(($posts = get_transient("sitepoint_wordpress_posts")) === false) 
  {
    $posts = new WP_Query("category_name=wordpress");
    set_transient("sitepoint_wordpress_posts", $posts, 3600);
  } 

?>
 
<?php if($posts->have_posts()): ?>
   <?php while($posts->have_posts()) : $posts->the_post(); ?>
    //display the post
   <?php endwhile; ?>
   <?php else: ?>
    //display no posts found message
<?php endif; ?>
<?php wp_reset_postdata(); ?>

Here we are caching the category posts for an hour. We are retrieving the posts using WP_Query class. WP_Query is serialized and stored as a transient. While retrieving it gets deserialized.

在这里,我们将类别帖子缓存了一个小时。 我们正在使用WP_Query类检索帖子。 WP_Query被序列化并存储为瞬态。 检索时会反序列化。

最后的想法 (Final Thoughts)

This article demonstrates how easily we can cache data in WordPress using the Transients API.

本文演示了如何使用Transients API在WordPress中缓存数据。

You can enable a persistent cache in WordPress using either Memcached Object Cache or the WP Redis plugins.

您可以使用Memcached对象缓存WP Redis插件在WordPress中启用持久缓存。

Please let me know your experiences with this API in the comments below.

请在下面的评论中告诉我您使用此API的经验。

翻译自: https://www.sitepoint.com/complete-guide-wordpress-transients-api/

wordpress api

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值