Cache rate limit values.
This commit is contained in:
parent
070dc4fcd1
commit
a8b4f83c9c
|
@ -42,6 +42,37 @@ class WC_Rate_Limiter {
|
||||||
return $action_id;
|
return $action_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a cache prefix.
|
||||||
|
*
|
||||||
|
* @param string $action_id Identifier of the action.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function get_cache_key( $action_id ) {
|
||||||
|
return WC_Cache_Helper::get_cache_prefix( 'rate_limit' . $action_id );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a cached rate limit.
|
||||||
|
*
|
||||||
|
* @param string $action_id Identifier of the action.
|
||||||
|
* @return bool|int
|
||||||
|
*/
|
||||||
|
protected static function get_cached( $action_id ) {
|
||||||
|
return wp_cache_get( self::get_cache_key( $action_id ), 'wc_rate_limit' );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cache a rate limit.
|
||||||
|
*
|
||||||
|
* @param string $action_id Identifier of the action.
|
||||||
|
* @param int $expiry Timestamp when the limit expires.
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected static function set_cache( $action_id, $expiry ) {
|
||||||
|
return wp_cache_set( self::get_cache_key( $action_id ), $expiry, 'wc_rate_limit' );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the action is not allowed to be run by the rate limiter yet, false otherwise.
|
* Returns true if the action is not allowed to be run by the rate limiter yet, false otherwise.
|
||||||
*
|
*
|
||||||
|
@ -51,6 +82,9 @@ class WC_Rate_Limiter {
|
||||||
public static function retried_too_soon( $action_id ) {
|
public static function retried_too_soon( $action_id ) {
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
|
|
||||||
|
$next_try_allowed_at = self::get_cached( $action_id );
|
||||||
|
|
||||||
|
if ( false === $next_try_allowed_at ) {
|
||||||
$next_try_allowed_at = $wpdb->get_var(
|
$next_try_allowed_at = $wpdb->get_var(
|
||||||
$wpdb->prepare(
|
$wpdb->prepare(
|
||||||
"
|
"
|
||||||
|
@ -62,6 +96,9 @@ class WC_Rate_Limiter {
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
self::set_cache( $action_id, $next_try_allowed_at );
|
||||||
|
}
|
||||||
|
|
||||||
// No record of action running, so action is allowed to run.
|
// No record of action running, so action is allowed to run.
|
||||||
if ( null === $next_try_allowed_at ) {
|
if ( null === $next_try_allowed_at ) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -97,6 +134,8 @@ class WC_Rate_Limiter {
|
||||||
array( '%s', '%d' )
|
array( '%s', '%d' )
|
||||||
);
|
);
|
||||||
|
|
||||||
|
self::set_cache( $action_id, $next_try_allowed_at );
|
||||||
|
|
||||||
return false !== $result;
|
return false !== $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue