pluginkollektiv / cachify

Smart but efficient cache solution for WordPress. Use DB, HDD, APC or Memcached for storing your blog pages. Make WordPress faster!
https://wordpress.org/plugins/cachify/
GNU General Public License v2.0
100 stars 31 forks source link

Cron script to recachify all previously cached pages on HD #146

Open snrbrnjna opened 6 years ago

snrbrnjna commented 6 years ago

Hi there, I got a script, that regularly recaches every previously cached page via HD cache method with wordpress wp_cron (that rellay better is carried out via system cron):

<?php
namespace My\Namespace;
/*
 * Get recursively Direntries
 */
function getDirContents($dir, &$results = array()){
    $files = scandir($dir);

    foreach($files as $key => $value){
        $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
        if(!is_dir($path)) {

            $results[] = $path;
        } else if($value != "." && $value != "..") {
            getDirContents($path, $results);
            $results[] = $path;
        }
    }

    return $results;
}

function getIndexPaths($dirContents) {
    return array_filter($dirContents, function($v) {
        return preg_match('/index\.html$/', $v);
    });
}

function curlUrls($urls) {
  $exitCodes = array();
  foreach ($urls as $key => $url) {
    $cmd = 'curl -L '. $url . ' > /dev/null 2>&1 &';
    exec($cmd, $output, $exit);
    $exitCodes[$url] = $exit;
  }
  return $exitCodes;
}

/**
 * get all cached urls, flush cache and curl every previously cached page again.
 */
function recache() {
  $cacheDir = \CACHIFY_CACHE_DIR;

  if (substr($cacheDir, -1) != '/') { $cacheDir = $cacheDir . '/'; }

  $dirContents = getDirContents($cacheDir);
  $indexFilePaths = getIndexPaths($dirContents);

  $urls = preg_replace(array('/'.preg_replace('/\//', '\/', $cacheDir).'/', '/index\.html$/'), '', $indexFilePaths);
  $urls = preg_replace('/^https-/', '', $urls);

  if (has_action('cachify_flush_cache')) {
    do_action('cachify_flush_cache');
    // DEBUG
    // debug_log('cachify cache flushed');
  }

  $exitCodes = curlUrls($urls);

  // DEBUG
  // debug_log($exitCodes);
}

// Register wp-cron for a daily recache at 2 am in the night.
if (defined('\CACHIFY_CACHE_DIR') && !empty(\CACHIFY_CACHE_DIR)) {
  if (!wp_next_scheduled('recache_cachify')) {
    $start_cron = strtotime('today') + 3600 *2;
    wp_schedule_event(time(), 'daily', 'recache_cachify');
  }
} else {
  if (wp_next_scheduled('recache_cachify')) {
    wp_clear_scheduled_hook('recache_cachify');
  }
}

add_action('recache_cachify', __NAMESPACE__.'\\recache');

just in case, perhaps you want to put a reference to it on your wiki?

Zodiac1978 commented 2 years ago

Related #18