CreateIT
CreateIT
BLOG

WP postmeta cache optimization

Ninja with ok sign and an app windows in the background
TAGS: WordPress

WP postmeta cache optimization

SHARE

Challenge:
add cache for data fetched from external API
Solution:
keep data in the wp_postmeta table and refresh before expiration

Some of the performance bottlenecks in WordPress come from external requests to APIs. There might be a situation in which on every page request WordPress needs to connect to some other server and fetch data. This will have a negative impact on TTFB (Server response time). In addition, it is not necessary to fetch data so often, we should be able to cache the response and refresh it every couple of minutes. This will save a lot of requests and improve page speed.

WP Transient approach

The first solution that comes to mind is to use the built-in WordPress caching mechanism WP transient. It stores the key value in the wp_options table. Transient also contains expiration time, after which cache will be deleted from DB and recreated after next page visit.

The weak point of this solution is the time when transient is expired. Cached data does not exist in the database anymore, so WP needs to connect to an external API. If multiple users are visiting the site at the same time, all of them will wait until the API completes the response with activity details.

/**
 * Display recent activities
 * data is fetched from external service
 * refresh data every 10 minutes
 */
public function handle($atts, $content = null)
{
    $key_name = 'recentActivity1';
    $activity = get_transient($key_name);
    if (!$activity) {
        $activity = getActivityFromExternalAPI();
        set_transient($key_name, $activity, 10 * MINUTE_IN_SECONDS);
    }
    $html = '';
    if (!empty($activity)) {
        $html .= '
            <div class="recent-activity collapse in" id="activity">
              <ul>';
        foreach ($activity['users'] as $user) {
            $html .= '
                <li>                    
                  <strong>' . $user['personFirstName'] . '</strong><span class="__country">' . $user['country'] . '</span>
                  <time datetime="' . gmdate('Y-m-d\TH:i:s\Z', strtotime($user['created_at'])) . '">' . date('g:i A, F j, Y', strtotime($user['created_at'])) . '</time>            
                </li>';
        }
        $html .= '    
              </ul>
            </div>
            ';
    }
    return do_shortcode($html);
}

Postmeta as cache

To avoid a situation when the cache is empty, we came up with a solution that uses the wp_postmeta table. The data is stored as normal metas for post, but uses the ‘expires’ key which defines when to recreate the cache. It is worth to remember that cache always exists in the database. We overwrite cache value using the update_post_meta() function. The rebuilding of cache occurs on page load at the time when the value of the “expires” parameter is older than the current time (the timestamp for “expires” is bigger than the current timestamp).

/**
 * Display recent activities
 * data is fetched from external service
 * refresh data every 10 minutes
 * method 2: using postmeta cache
 */
public function handle($atts, $content = null)
{
    $post_id = get_the_ID();
    $metaKey = 'recentActivity1_' . $post_id;
    $activityData = get_post_meta($post_id, $metaKey);
    $activityData = is_array($activityData) ? $activityData[0] : $activityData;
    if (!isset($activityData) || $activityData['expires'] <= time()) {
        $activity = getActivityFromExternalAPI();
        // Put the results in a postmeta.
        $activityDataNew = [
            'expires' => time() + (10 * MINUTE_IN_SECONDS),
            'response' => $activity,
        ];
        update_post_meta($post_id, $metaKey, $activityDataNew);
    } else {
        $activity = $activityData['response'];
    }
    (...)
}

Postmeta cache – better than transients?

It’s not determined yet which solution is better. For high traffic websites, recreating cache can be an expensive transaction. If multiple users trigger the getActivityFromExternalAPI() function at the same time, the server load can increase. To resolve this competition, deploying the code to production environment will be needed, followed by an analysis of Transactions in NewRelic reports.

That’s t for today’s guidelines. Make sure to follow us for other tips and sign up for our newsletter.

 

Need help?

  • Looking for support from experienced programmers?

  • Need to fix a bug in the code?

  • Want to customize your webste/application?

1 comment

Hi,

can you make comparsion with and without the transients? I wonder how much faster the meta data solution is?

Thank you.

ADD COMMENT

Your email address will not be published.

createIT Contact