您现在的位置是:首页 > 网站制作 > WordpressWordpress

缓存REST API响应并加速WordPress REST API请求教程

杰帅2023-06-30【Wordpress】人已围观

简介你可以对REST API的响应进行缓存,使WordPress网站加载速度大大提升。这里搬主题就分享一下缓存REST API响应并加速WordPress REST API请求教程。

当我们使用WordPress REST API时,每个请求都会由WordPress处理,根据数据库的大小和服务器的性能,而对于高流量的网站,问题将开始出现。这里我们可以通过使用WP REST Cache这样的插件,你可以对REST API的响应进行缓存,使WordPress网站加载速度大大提升。这里搬主题就分享一下缓存REST API响应并加速WordPress REST API请求教程。

使用插件缓存

这里可以安装插件【WP REST Cache】,安装启用即可。

当然,如果你想总是查看未缓存的REST API响应,你可以在请求头中添加以下一行:

Cache-Control: private or Cache-Control: no-cache

除了缓存响应之外,还有一些方法可以加快WP REST API的响应时间,如下:

WordPress REST API的开发为我们如何从网站和应用程序的前端与数据库通信提供了大量的可能性。无论你是使用register_rest_route创建的自定义端点,还是通过register_rest_field将自定义数据添加到现有端点的输出中,对这些数据进行缓存往往是有意义的。

谨慎使用瞬态的API

WordPress的瞬态基本上可以用来存储任何东西。然而,由于它们被存储在wp_options表中,这种方法应该少用,而且通常是对那些有点全局性的数据。举例来说,如果你有10,000个文章类型的条目,并使用瞬态来存储每个条目的数据,这将导致选项表变长10,000行(不好)。我们将在这篇文章的后面通过使用文章元API来处理这个特殊的使用情况。

现在,让我们设置一个包含第三方API响应的瞬态,然后将其添加到我们自己的自定义端点响应中。

<?php

function yournamespace_rest_transient_example() {

	//check for transient first
	$data = get_transient( 'yournamespace_data_example' );

	if ( !empty( $data ) ) {

		$data = $terms_transient;
	
	//generate output and then save to transient
	} else {

		//do some stuff here and set result to $data
		
		$data = custom_remote_post_call();

		//cache for 2 days
		set_transient( 'yournamespace_data_example', $data, DAY_IN_SECONDS * 2 );
	
	}
	
	$result = new WP_REST_Response( $data, 200 );

	return $result;

}

现在,不要在你的端点中直接调用第三方API,只要调用yournamespace_rest_transient_example函数,先检查缓存的响应。如果瞬态不存在,它将调用API并存储响应。

你可以通过A/B测试你在Postman中的端点的响应时间,并注释掉第一个get_transient()调用,来测试这给你带来的实际性能提升。你的里程可能会有所不同,这取决于你的服务器配置和你所做的查询有多昂贵。

清理瞬态

每当一个文章被保存时,删除你的自定义文章元数据往往是有意义的,尽管你可以选择只是等待它过期。

<?php

function yournamespace_delete_transient( $post_id, $post, $update ) {
  
	delete_transient('yournamespace_data_example');

}

//{your_post_type} with a custom post type
add_action('save_post_{your_post_type}', 'yournamespace_delete_transient', 10, 3);

对查询使用文章元

对于那些生成成本较高且与文章相关的数据,将数据存储在文章的元中,在规定的时间段内是有意义的。本质上是在重现瞬时数据的想法,但将其存储在wp_postmeta表中。首先,我们生成一些数据,并将其与过期日期一起保存在文章的meta中。然后,在未来对终端的调用中,我们将检查这个存储的数据是否存在,并返回存储的版本,而不是从头开始再次生成它。

这只有在生成新数据比从数据库中查找所需时间更长时才有意义。因此,如果你的端点正在处理一些复杂的查询,或使用第三方的API,这可能会产生明显的差异。下面是如何设置的。

注册你的端点或自定义字段和回调函数

<?php

/***
Register custom field for existing endpoint with callback function
Endpoint: /wp/v2/your_post_type
Note: Could also be a custom endpoint using register_rest_route
***/

register_rest_field('your_post_type', 'custom_field_name', array(
    'get_callback' => 'yournamespace_endpoint_callback',
    'schema' => null,
));

/***
Callback function – if you use a custom endpoint you may have to change how you get $post_id
***/

function yournamespace_endpoint_callback( $object ) {
  
  $post_id = $object['id'];

  return yournamespace_get_post_transient( $post_id, 'yournamespace_data_example', 'yournamespace_get_custom_meta' );

}

生成数据并保存在有过期密钥的文章元中

这就是奇迹发生的地方。在我们的辅助函数yournamespace_get_post_transient()中,我们首先检查数据是否存在于post meta中。如果它存在并且没有过期,我们可以直接返回它。如果不存在,那么我们就从头开始生成数据,但将其与过期时间一起存储在一个数组中。同样,这可能只有在我们生成的数据很复杂时才有意义。

<?php

/***
Stores persisent data as post meta instead of using Transient API
@param	$post_id        int     post ID to store post meta for
@parm	$meta_key       str     "transient" name
@param	$update_func    str     function to update the meta key with a new value
@param	$expiration     time    when value should expire 
return  $value          array   stored value or updated value
***/

function yournamespace_get_post_transient( $post_id, $meta_key, $update_func, $expiration = null ) {

    if ( null == $expiration ) {
        $expiration = strtotime( '+7 days' );
    }

    //uncomment and request endpoint to clear cache:
    //delete_post_meta($post_id, $meta_key);

    $current_value = get_post_meta( $post_id, $meta_key, true );
    
    //this is the "cached" value
    //if the meta value is in the right format and it's not expired, we return it and we are done
    if ( is_array( $current_value ) && $current_value['expiration'] > time() ) {
        return $current_value['data'];
    }
    
    //either expired or didn't exist so let's call our "update" function
    $new_data = call_user_func( $update_func, $post_id );
    
    //store output in an array so that we can check expiration later
    $value = array(
        'expiration' => $expiration,
        'data' => $new_data
    );
    
    //save this new value to post meta
    update_post_meta( $post_id, $meta_key, $new_value );
    
    //return just the data for use in the endpoint
    return $value['data'];
}

/***
Update function example – this is the data you're actually using in your endpoint or custom field
@param	$post_id    int     post ID to store post meta for
return  $meta       array   Your value  
***/

function yournamespace_get_custom_meta( $post_id ) {
	
	$test_meta_value = get_post_meta('test_meta_value', $post_id);
	$some_other_thing = get_post_meta('some_other_thing', $post_id);
        $expensive = maybe_a_really_crazy_query($post_id);
	
	$meta = array(
		'test_thing' => $test_meta_value,
		'some_thing' => $some_other_thing,
                'expensive_thing' => $expensive
	);
	
	return $meta;
}

/***
Clear yc_collection_meta post_meta transient on post save/update
***/

function yournamespace_delete_meta_transient( $post_id, $post, $update ) {
        delete_post_meta( $post_id, 'yc_collection_meta' );
	yc_get_post_transient( $post_id, 'yc_collection_meta', 'yc_get_collection_meta' );
}

add_action( 'save_post_{your_post_type}', 'yournamespace_delete_meta_transient', 10, 3 );

在你的应用程序中,每当你调用你的端点,你现在可以期望看到你在yournamespace_get_custom_meta()函数中定义的响应输出的缓存版本。

删除文章元值

注意最后的函数yournamespace_delete_meta_transient,它与save_post挂钩。这将通过删除自定义元值来 "破坏缓存",然后在更新文章时重新生成它。

请务必测试

请注意,如果查找你所保存的瞬时或文章元数据并从数据库中返回的过程比即时生成数据的成本,如时间都划不来更高,那么就即时生成数据。毕竟除了用各种缓存方法测试你的端点并比较结果外,没有其他办法。

Tags:SSH教程   WordPress   WordPress优化教程   WordPress教程

很赞哦! ()

文章评论

本站推荐

站点信息

  • 建站时间:2018-10-24
  • 网站程序:帝国CMS7.5
  • 主题模板《今夕何夕》
  • 文章统计1172篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 微信公众号:扫描二维码,关注我们