本文介绍了Wordpress REST API 响应时间慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 WordPress REST API 的速度有疑问.我想要做的是尽可能快地为一份报告获取大约 26,000 条记录的数据,以便为用户提供流畅的用户体验.我遇到的问题是 WordPress 似乎在调用 REST API 时加载了核心、插件和主题.

I have an issue with the speed of the WordPress REST API. What I’m trying to do is get data for a report about 26k records in total as fast as possible to give the user a fluid user experience. The issue I’m running into is it seems that WordPress loads core, plugins and themes when the REST API is called.

我知道的优化代码的方法已经用完了,是否有任何人知道可以提高速度的 WordPress 调整?对于使用 REST API 的人来说,这些结果是否正常?如您所见,运行我的代码的时间不是问题,而是 WordPress 的开销.

I’ve run out of ways I know to optimize the code, is there some WordPress tweaks anyone knows to improve the speed? Are these results normal for people using the REST API? As you can see the time to run my code isn't the issue but the WordPress overhead is.

推荐答案

概述:所以问题是 WordPress 4.8 版的限制.WordPress 旨在为每个 REST API 请求加载插件和主题及其所有核心.这是响应时间慢的原因.

Overview: So the issue is a limitation of WordPress as of version 4.8. WordPress is designed to load plugins and themes and all of its core every REST API request. Here is the reason for the slow response time.

解决方案:当前唯一的解决方案是对插件中的文件进行 ajax 调用,并且仅加载 WordPress 核心的一部分.下面的代码是直接文件访问,同时仍然能够以快速响应时间使用 WordPress 功能.

Solution: The only current solution is an ajax call to a file in your plugin and loads only part of the WordPress core. The code below is direct file access while still being able to use WordPress functions with fast response time.

//Tell WordPress to only load the basics
define('SHORTINIT',1);

//get path of wp-load.php and load it
require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';

// register global database
global $wpdb;

// return data selected from DB to user

结果:响应时间降至 100 毫秒.从 1069 毫秒到 108 毫秒,这是一个巨大的差异.

Results: Response times are down to 100ms. That's a huge difference from 1069ms to 108ms.

参考资料:https://deliciousbrains.com/wordpress-rest-api-vs-自定义请求处理程序/

最后说明:Wordpress REST API 非常新,功能非常强大,您应该在大多数响应时间不成问题的情况下使用.

Last notes:The Wordpress REST API is very new, quite powerful and you should be using in most situations where response time is not an issue.

这篇关于Wordpress REST API 响应时间慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:01