How programmatically create APO cached version of URL?

After URL purged first visitor see URL with DYNAMIC in cf-cache-status in response headers. And the second visit already returns HIT.

How i can create fresh cache of URL programmatically after purging so that both the old cache is reset and the first user gets an already updated cached copy of the page?

Resolved.

  1. I turn on W3TC plugin with page caching and Garbage collection interval with 2592000 seconds.
  2. When i flush the URLs (after edit/update the post, add new comment, update some meta fields, edit tag, other custom events and actions…), first i make my own relevant URls list (filter ‘cloudflare_purge_by_url’ action: Post ID, Post’s feed, Post’s categories, tags, terms, comment…).
  3. Then i purge W3TC cache for each relevant URL.
  4. Then i clear cache with CF API.
  5. And finally i got CF API purge_cache response and
  6. Schedule task with URLs list with wp_schedule_single_event():
if ($result['success'] === true && !$this->disable_crawling) {
    // Schedule crawling URLs (see functions.php for action "custom_cron_cache")
    if (!wp_next_scheduled('custom_cron_cache', $urls, $result)) {
        $time = time() + 30;
             $args  = [
                 $urls, // array
                 $result['result']['id'] // string
             ];
             return wp_schedule_single_event($time, 'custom_cron_cache', $args);
    }
}

After these actions i have fresh cache of Post and relevant URLs on CF servers and first visitor gets the last version of content with cf-cache-status: HIT (from CF servers) and bot crawlers gets fresh cached version of content from origin server.

1 Like

My crawler function:

/**
    * Ф-ция ждет запланированное событие custom_cron_cache (см. класс Wpshop_CloudFlareApi)
    * и при наличии задания, обходит полученные URLs
    */
    add_action('custom_cron_cache', 'custom_cron_cache_doing', 10, 2);
    function custom_cron_cache_doing(array $urls, $cf_result_id = null) {
        $crawled_urls = [];
        $uncrawled_urls = [];

        foreach ($urls as $i => $url) {
            // Refresh cache of URL
            $response = wp_remote_get($url, [
                'timeout' => 5,
                'sslverify' => false,
                'headers' => [
                    'accept' => 'text/html',
                ],
            ]);

            $cf_cache_status = isset($response['headers']['cf-cache-status']) ? $response['headers']['cf-cache-status'] : 'unknown';
            $response_code = wp_remote_retrieve_response_code($response);
            $response_headers = $response['headers'];

            if (is_wp_error($response)) {
                $error_msg = $response->get_error_message();
                error_log('Error ' . __FUNCTION__ . ': ' . print_r($error_msg, true));
                wp_mail(SHOP_EMAIL, 'Error: Crawler of purged URLs', print_r($error_msg, true));

                // Add uncrawled URL to URLs list
                $uncrawled_urls[$i] = [
                    'url' => $url,
                    'code' => $response_code,
                    'cf-cache-status' => $cf_cache_status,
                ];
            }

            if ($response_code === 200) {
                // Add crawled URL to URLs list
                $crawled_urls[$i] = [
                    'url' => $url,
                    'code' => $response_code,
                    'cf-cache-status' => $cf_cache_status,
                ];
            }
        }

        if (!empty($uncrawled_urls)) {
            if (!1) wp_mail(SHOP_EMAIL, __FUNCTION__, 'Cron: list of uncrawled URLs: ' . print_r($uncrawled_urls, true));
        }

        if (!empty($crawled_urls)) {
            if (!0) wp_mail(SHOP_EMAIL, __FUNCTION__, 'Cron: crawled and refreshed URLs: ' . print_r($crawled_urls, true));

            do_action('wpshop_cloudflare_crawled_urls', $crawled_urls);

            return $crawled_urls;
        }

        if (!0) wp_mail(SHOP_EMAIL, __FUNCTION__, 'Cron: failed serve of the URLs: ' . print_r($urls, true));
    }
1 Like