/** * When event completes, allow another */ public static function free_lock($lock, $expires = 0) { if (self::get_lock_value($lock) > 1) { wp_cache_decr(self::get_key($lock)); } else { wp_cache_set(self::get_key($lock), 0, null, $expires); } wp_cache_set(self::get_key($lock, 'timestamp'), time(), null, $expires); return true; }
function wpcom_vip_login_limiter_on_success($username, $user) { $ip = preg_replace('/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR']); $key1 = $ip . '|' . $username; // IP + username $key2 = $ip; // IP only wp_cache_decr($key1, 1, 'login_limit'); wp_cache_decr($key2, 1, 'login_limit'); }
/** * Decrement a value in the object cache. * * @synopsis <key> [<offset>] [<group>] */ public function decr($args, $assoc_args) { $key = $args[0]; $offset = \WP_CLI\Utils\get_flag_value($args, 1, 1); $group = \WP_CLI\Utils\get_flag_value($args, 2, ''); $value = wp_cache_decr($key, $offset, $group); if (false === $value) { WP_CLI::error('The value was not decremented.'); } WP_CLI::print_value($value, $assoc_args); }
/** * Decrement a value in the object cache. * * @synopsis <key> [<offset>] [<group>] */ public function decr($args, $assoc_args) { $key = $args[0]; $offset = isset($args[1]) ? $args[1] : 1; $group = isset($args[2]) ? $args[2] : ''; $value = wp_cache_decr($key, $offset, $group); if (false === $value) { WP_CLI::error('The value was not decremented.'); } WP_CLI::print_value($value, $assoc_args); }
/** * @group 21327 */ public function test_wp_cache_decr() { $key = rand_str(); $this->assertFalse(wp_cache_decr($key)); wp_cache_set($key, 0); wp_cache_decr($key); $this->assertEquals(0, wp_cache_get($key)); wp_cache_set($key, 3); wp_cache_decr($key); $this->assertEquals(2, wp_cache_get($key)); wp_cache_decr($key, 2); $this->assertEquals(0, wp_cache_get($key)); }
/** * Decrement a value in the object cache. * * @uses wp_cache_decr * * @param array $args Function arguments. * @param array $assoc_args Function arguments with parameter key. * @return void */ public function decr($args, $assoc_args) { if (empty($args)) { WP_CLI::line('usage: wp cache decr <key> [offset] [group]'); exit; } $key = $args[0]; $offset = isset($args[1]) ? $args[1] : 1; $group = isset($args[2]) ? $args[2] : ''; $value = wp_cache_decr($key, $offset, $group); if (false === $value) { WP_CLI::error('The value was not decremented.'); exit; } WP_CLI::print_value($value, $assoc_args); }
// test wp_cache_init(); wp_cache_init(); global $wp_object_cache; echo "Cache object exist: " . bool_to_str($wp_object_cache instanceof WP_Object_Cache); wp_cache_flush(); // test wp_cache_incr and wp_cache_get wp_cache_incr('integer', 1, 'test-group'); wp_cache_incr('integer', 3, 'test-group'); $r = wp_cache_get('integer', 'test-group'); $rf = wp_cache_get('integer', 'test-group', true); echo "wp_cache_incr force: " . bool_to_str($r == $rf); echo "wp_cache_incr: " . bool_to_str($r == 4); // test wp_cache_decr wp_cache_decr('integer', 1, 'test-group'); wp_cache_decr('integer', 3, 'test-group'); wp_cache_decr('integer', 1, 'test-group'); $r = wp_cache_get('integer', 'test-group'); $rf = wp_cache_get('integer', 'test-group', true); echo "wp_cache_decr force: " . bool_to_str($r == $rf); echo "wp_cache_decr: " . bool_to_str($r == -1); // test wp_cache_add $true = wp_cache_add('key', 'value', 'test-group'); $false = wp_cache_add('integer', 'value', 'test-group'); echo "wp_cache_add: " . bool_to_str($true === true); echo "wp_cache_add exist: " . bool_to_str($false === false); $r = wp_cache_get('key', 'test-group'); $rf = wp_cache_get('key', 'test-group', true); echo "wp_cache_add > wp_cache_get force: " . bool_to_str($r == $rf); echo "wp_cache_add > wp_cache_get: " . bool_to_str($r == 'value'); $r = wp_cache_get('integer', 'test-group'); $rf = wp_cache_get('integer', 'test-group', true);