Contributors: prettyboymp, kevinlangleyjr, csloisel
Tags: performance, caching
Requires at least: 3.0
Tested up to: 3.7.1
Stable tag: 1.0
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
A WordPress api to kick off globally singleton actions. It will lock the action to prevent other requests from kicking off the same action
After dropping the plugin into the containing theme or plugin, add the following:
<?php
if( ! class_exists( 'NSA_Action' ) ) {
require_once( $path_to_nsa_actions . '/no-stampede-actions.php' );
}
<?php
function long_request(){
$val = wp_cache_get( 'long_request_key' );
if( $val === false ){
nsa_action('long_request')
->action_callback( 'long_request_callback', array( 'http://example.com/long-running-request.php' ) )
->background_only( false )
->max_tries( 10 )
->do_action( );
$val = wp_cache_get( 'long_request_key' );
}
return $val;
}
function long_request_callback($url){
$content = wp_remote_retrieve_body( wp_remote_get( $url, array( 'timeout' => 30 ) ) );
wp_cache_set( 'long_request_key', $content );
return $content;
}