WordPress.org

Ready to get started?Download WordPress

Forums

WP Super Cache and multiple servers (13 posts)

  1. eltano343
    Member
    Posted 1 year ago #

    Hi. I have a WordPress multisite which works in several servers (in fact, it's an autoscaling platform hosted at Amazon Web Services). I would like that whenever I delete the cache of one site (by the direct button or when I update a post), all the cache files across all the servers be deleted too. So I would like to know if there is an specific action or filter inside WP Super Cache which I could use to achieve this. Or maybe there is already a plugin which does this.

    Thanks.

  2. Hubert Nguyen
    Member
    Posted 1 year ago #

    eltano343, normally you should have some kind of common storage that is shared between all the instances if you want that behavior. NFS technically works, but it's not very fast.

    Having a big NAS is better but probably not an option with AWS. In the end, WPSC is not really built for that type of environment.

    You may want to look into using a Memcached server with Batcache or W3TC. WPSC also as a Memcached option I think, although I'm not sure how solid it actually is... Not a fun choice, but having a shared storage (in RAM or disk) is the best way to scale.

  3. eltano343
    Member
    Posted 1 year ago #

    Thanks for the response. Finally, I modified the WP Super Cache plugin to propagate the cache refresh to the other servers.

    I added my function in wp_cache_clear_cache(), wp_cache_clean_cache() and wp_cache_page_update() and it seems to work fine.

  4. Hubert Nguyen
    Member
    Posted 1 year ago #

    Cool, I'm sure that others would be interested by your findings because it is the easiest way to scale horizontally in my opinion.

    What kind of lag are you seeing between the moment when you modify the "master instance" (if there's one) and the moment where everyone is back in sync again?

  5. eltano343
    Member
    Posted 1 year ago #

    I tested the modification in production and it had to rollback it. It took too much time to run. I am gonna think in a different solution.

  6. Hubert Nguyen
    Member
    Posted 1 year ago #

    Just curious: did you try to use Rsync on the cached directory?

  7. Hubert Nguyen
    Member
    Posted 1 year ago #

    btw, why don't you just let each client generate its own local cache? It may be intense on the DB, but if there's a DB cache, it shouldn't be so bad.

  8. eltano343
    Member
    Posted 1 year ago #

    Finally, I just let each server generate its own cache file. I only modified the plugin to allow deleting the cache in all the servers when the user hits the button "Delete cache".

  9. Hubert Nguyen
    Member
    Posted 1 year ago #

    cool - that's a nice trick. so you can delete the disk cache on all servers when you hit Delete Cache on one of them? How do you sync all that? Anyway, I'm glad this works for you.

  10. eltano343
    Member
    Posted 1 year ago #

    I just modified the file wp-cache.php and added this code when the funcion wp_cache_clean_cache starts:

    if(empty($_GET['no_propagate_cache_clean']))
        @file_get_contents('http://xx.xx.xx.xx/wp-content/plugins/services.php?clean_cache=1&f=wp_cache_clean_cache&p1='.urlencode($file_prefix).'&p2='.($all ? '1' : '0'));

    That file, services.php, is in another server (outside the platform) which has the same wordpress installation than the servers in production. Ant it calls another file, called, multiserver.php, inside the wp-super-cache folder which does only the following:

    <?
    include dirname(__FILE__).'/../../../wp-config.php';
    
    $functions_allowed = array();
    
    //$functions_allowed[] = 'wp_cache_post_change';
    //$functions_allowed[] = 'wp_cache_clear_cache';
    $functions_allowed[] = 'wp_cache_clean_cache';
    
    if(in_array(@$_POST['f'], $functions_allowed))
        call_user_func($_POST['f'], @$_POST['p1'], @$_POST['p2']);
    ?>

    It's not the perfect solution, but it works.

  11. Hubert Nguyen
    Member
    Posted 1 year ago #

    Cool trick, I never heard of that before. Thanks.

  12. anthonyr_complex
    Member
    Posted 11 months ago #

    Hey eltano,

    I was interested in your solution for clearing cache across a multi-server setup with WP Super Cache.

    What does your services.php file look like, meaning how does it call multiserver.php and pass the values to execute the wp_cache_clean_cache function.

  13. eltano343
    Member
    Posted 11 months ago #

    Hi Anthony. The services.php file simply loops over the list of IPs and call this file directly to the server. It is something like this:

    <?
    $context = stream_context_create(array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Host: www.example.com\r\n",
            'content' => 'f='.urlencode(@$_GET['f']).'&p1='.urlencode($_GET['p1']).'&p2='.urlencode($_GET['p2']))));
    
    foreach($ips as $ip)
        @file_get_contents('http://'.$ip.'/wp-content/plugins/wp-super-cache/multiserver.php?no_propagate_cache_clean=1', false, $context);
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic