1

站点在后端 ( BE ) WP 服务器和多个前端 ( FE ) 服务器上运行。

FE 有BE的 MySQL db r/o Slave,以及带有HyperDB插件的WP 安装,所以它从本地读取,写入BEW3TC插件用于缓存FE的。

在BE上创建新帖子。发布这些帖子只会在BE上触发挂钩。

问题:如何在所有FE上也触发这些钩子以重置它们的缓存?

ps 我前段时间在 W3TC 插件支持论坛问过类似的问题,没有回复。

4

1 回答 1

1

您可以通过在 FE 和 BE 上使用迷你插件来做到这一点。逻辑如下所示;

  1. 在后端,发布后触发的实施和操作
  2. 此触发的操作将调用具有特定的前端服务usernamepassword并且post_id
  3. 在 FE 上,实现另一个插件来检查具有usernamepasswordpost_id
  4. 如果有,实例化W3_CacheFlush并调用特定函数。

我开发了迷你插件,您可以从管理面板安装它们。简单地说,将两个代码保存为 php 文件并压缩它。然后上传到服务器。还有一些关于插件的要点。通过用户名和密码进行服务通信。因此,您需要在两个插件上提供相同的用户名和密码。如果没有,他们就无法沟通。我已经输入了用户名和密码,以防止其他人拨打您的 FE 服务。这里是插件。

W3TC_BE.php

<?php
/*
Plugin Name: W3TC Backend
Plugin URI: http://huseyinbabal.net
Description: Calls wstc cache clean on frontend services when new post published.
Version: 1.0
Author: Hüseyin BABAL
Author URI: http://huseyinbabal.net
*/

function call_cache_clean_service( $url, $post_id ) {
    if ( !function_exists( 'curl_version' ) ) {
        wp_die( 'Curl must be enabled' );
    }

    $url = 'http://frontendservice.com';
    $fields = array(
                    'username' => "your_username", // Username password protection for service usage. Those username password will be same on server side
                    'password' => "your_password",
                    'post_id' => $post_id
                    );
    foreach( $fields as $key => $value ) { 
        $fields_string .= $key . '=' . $value . '&'; 
    }
    rtrim($fields_string, '&');

    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

    $result = curl_exec($ch);
    // To track which posts triggered
    error_log("[W3TC] Cache clean service called for : $post_id to $url", 3, '/path/to/log/file.txt');
    curl_close($ch);
}

// This for triggering service for all frontends
function post_published( $post_id ) {
    $frontend_urls = array (
        "http://frontendservice1.com", 
        "http://frontendservice2.com", 
        "http://frontendservice3.com"
        );
    foreach ($frontend_urls as $url) {
        call_cache_clean_service( $post_id );
    }
}

// Publish action for calling service
add_action( 'publish_post', 'post_published' );

?>

W3TC_FE.php

<?php
/*
Plugin Name: W3TC Frontend
Plugin URI: http://huseyinbabal.net
Description: Check specific request and clear cache
Version: 1.0
Author: Hüseyin BABAL
Author URI: http://huseyinbabal.net
*/

function check_w3c_request() {
    // this username and password must be same as in be
    $original_username = "your_username";
    $original_password = "your_password";

    $username = $_POST["username"];
    $password = $_POST["username"];
    $post_id = $_POST["post_id"];

    if ( (!empty($username) && $username != $original_username) || ( !empty($password) || $password != $original_password ) ) {
        wp_die( "Page not allowed!" );
    } else {
        if ( class_exists('W3_CacheFlush') ) {
            $w3_pgcache = w3_instance('W3_CacheFlush');
            return $w3_pgcache->prime_post( $post_id );     
        }   
    }


}

// Get Posted variables
add_action( 'after_setup_theme', 'check_w3tc_request' );

?>
于 2014-03-19T08:35:05.037 回答