简体   繁体   English

Wordpress REST API 缓存问题 - WP 引擎

[英]Wordpress REST API Caching Issue - WP Engine

I have created a custom plugin that actually just registeres a few API endpoints.我创建了一个自定义插件,它实际上只注册了几个 API 端点。

Right now the issue I'm having is that all of the endpoints are working fine locally, but when I push this code to WpEngine where I've hosted my WordPress site the API responses are getting cached.现在我遇到的问题是所有端点都在本地正常工作,但是当我将此代码推送到我托管我的 WordPress 站点的 WpEngine 时,API 响应正在被缓存。

If I clear the cache through WPEngine and make the request again the API is working fine until a 200 success response is received for the first time, once success is received then from the point the endpoint is always returning the same response no matter what header, parameter value I give to that endpoint.如果我通过 WPEngine 清除缓存并再次发出请求,API 工作正常,直到第一次收到 200 成功响应,一旦收到成功,那么无论 header 是什么,端点总是返回相同的响应,我给那个端点的参数值。

In the wp-config.php file I've disabled the cache - define( 'WP_CACHE', false );在 wp-config.php 文件中我禁用了缓存 - define( 'WP_CACHE', false );

also tried adding也尝试添加

wp_cache_flush(); wp_cache_flush(); nocache_headers(); nocache_headers();

in the request action call back functions too, still no success, always the response are cached.在请求操作回调函数中,仍然没有成功,总是缓存响应。

Few Code snippets for your reference -几个代码片段供您参考 -

// This is the route I've registered

public function register_routes()
{
   register_rest_route($namespace, '/config' ,[
                'methods' => 'GET',
                'callback' => array($this, 'Action_GetConfig'),
                'permission_callback' => 'authCheck'
            ]);
}

add_action( 'rest_api_init', array( $this, 'register_routes' ) );


        function Action_GetConfig(WP_REST_Request $request)
        {
            try {
                // wp_cache_flush();
                // nocache_headers();
  
                    $headers = $request->get_headers();

                    // Basic Validation
                    if (IsNullOrEmptyString($headers['platform'][0]) or IsNullOrEmptyString($headers['version'][0])) {
                        $resp = new WP_HTTP_Response();
                        // $resp->set_headers( array('Cache-Control' => 'no-cache, must-revalidate, max-age=0'));
                        $resp->set_status(400);
                        return $resp;
                    }
        
                    // Service Invocation
                    $results = $this->getConfig($headers['platform'][0], $headers['version'][0]);
                    $resp = new WP_HTTP_Response($results);
                    $resp->set_status(200);
                    return $resp;

            } catch (Throwable $e) {
                //$log->error($e);
                $resp = new WP_HTTP_Response($e);
                $resp->set_status(500);
                return $resp;
            }
        }

Can someone help in solving this API response caching issue?有人可以帮助解决这个 API 响应缓存问题吗? Thanks!谢谢!

I believe WP ENGINE USES, WP Engine MU PLUGIN.相信WP ENGINE USES,WP Engine MU PLUGIN。 If so with this plugin you will have access to a few functions called wpecommon::purge_varnish_cache() , if you pass the ID of a particular post that you were targeted for, this function will clear the post cache.如果使用此插件,您将可以访问一些名为wpecommon::purge_varnish_cache()的函数,如果您传递目标帖子的 ID,则此 function 将清除帖子缓存。 But if you use the function without passing the ID then it will clear all the cache of the entire domain( I would certainly not recommend this way, as it will cause a performance issue on a large site).但是如果你使用 function 而不传递 ID 那么它将清除整个域的所有缓存(我当然不推荐这种方式,因为它会导致大型网站的性能问题)。

function your_call_of_action(WP_REST_Request $request){
 $id = $request->get_param( 'id' );
 if ( FALSE === get_post_status( $id ) ) {
   $resp = new WP_HTTP_Response([]);
   $resp->set_status(500);
   return $resp;
 }
 
 wpecommon::purge_varnish_cache( $id )

// rest of your code goes here
 
}

Also, you can call the following API to clear the cache:也可以拨打以下API清除缓存:

/installs/{install_id}/purge_cache

Read the following documents:阅读以下文档:

Read this document: https://wpengine.com/support/cache/阅读本文档: https://wpengine.com/support/cache/
API Document: https://wpengineapi.com/reference API 文档: https://wpengineapi.com/reference

I solved this issue by adding a Cache Exclusion Policy in WPEngine.我通过在 WPEngine 中添加缓存排除策略解决了这个问题。 Because by default in WPEngine all the Endpoints responses are cached.因为默认情况下在 WPEngine 中所有端点响应都被缓存。 So if we want NOT to cache a specific route, then we have to add that route to the Exclusion list.因此,如果我们不想缓存特定路由,则必须将该路由添加到排除列表中。

path: ^/wp-json/nx/services/?

I added this above Route RegEx in WPEngine.我在 WPEngine 的 Route RegEx 上面添加了这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM