【问题标题】:How to trigger WordPress hooks in a multi-server setup?如何在多服务器设置中触发 WordPress 挂钩?
【发布时间】:2014-03-19 16:35:05
【问题描述】:

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

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

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

问题:如何在所有 FE 上也触发这些挂钩以重置其缓存?

附言前段时间在W3TC插件支持论坛问过the similar question,没有回复。

【问题讨论】:

    标签: php wordpress w3-total-cache


    【解决方案1】:

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

    1. 在发布后触发的后端、实施和操作
    2. 此触发操作将调用具有特定usernamepasswordpost_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' );
    
    ?>
    

    【讨论】:

    • 嗨@Hüseyin,谢谢你的回复!我会试一试。必须测试它是否存在延迟问题:主从数据库复制通常只需要几毫秒,但是 var 也可能需要更长的时间。原因。使用此解决方案,当它刚刚更新到主数据库时,会在新的帖子保存/发布后立即触发挂钩。 FE 上的从属设备会延迟一些时间:从毫秒到任何时间。这允许在新帖子同步到 FE 数据库之前刷新 FE 上的缓存。
    • 我的解决方案是可以改进的。在服务调用部分,您可以将其放入队列中,而不是立即调用它。您可以将其放入队列中,并在 5 分钟后从队列中一一取出。有很多替代方案可以解决您的延迟问题。希望对你有帮助
    猜你喜欢
    • 2017-07-12
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 2015-09-23
    相关资源
    最近更新 更多