Skip to content

Commit d245255

Browse files
committed
preserve key TTL when calling (in|de)crement
1 parent ce7afeb commit d245255

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Changelog
22

33
## Unreleased
4-
- Changed `wp_cache_*()` function signatures to match core
4+
- Fixed (in|de)crement calls removing key TTL
5+
- Updated `wp_cache_*()` function signatures to match core
56
- Removed deprecated HHVM support
67
- Removed deprecated `WP_REDIS_SERIALIZER` constant
78

includes/object-cache.php

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,13 @@ class WP_Object_Cache {
384384
*/
385385
private $fail_gracefully = true;
386386

387+
/**
388+
* Whether to use igbinary serialization.
389+
*
390+
* @var bool
391+
*/
392+
private $use_igbinary = false;
393+
387394
/**
388395
* Holds the non-Redis objects.
389396
*
@@ -519,6 +526,8 @@ public function __construct( $fail_gracefully = false ) {
519526

520527
$this->cache_group_types();
521528

529+
$this->use_igbinary = defined( 'WP_REDIS_IGBINARY' ) && WP_REDIS_IGBINARY && extension_loaded( 'igbinary' );
530+
522531
$client = $this->determine_client();
523532
$parameters = $this->build_parameters();
524533

@@ -2342,13 +2351,28 @@ public function increment( $key, $offset = 1, $group = 'default' ) {
23422351
}
23432352

23442353
try {
2345-
$value = (int) $this->parse_redis_response( $this->maybe_unserialize( $this->redis->get( $derived_key ) ) );
2346-
$value += $offset;
2347-
$result = $this->parse_redis_response( $this->redis->set( $derived_key, $this->maybe_serialize( $value ) ) );
2354+
if ( $this->use_igbinary ) {
2355+
$value = (int) $this->parse_redis_response( $this->maybe_unserialize( $this->redis->get( $derived_key ) ) );
2356+
$value += $offset;
2357+
$serialized = $this->maybe_serialize( $value );
2358+
2359+
if ( ($pttl = $this->redis->pttl( $derived_key )) > 0 ) {
2360+
if ( $this->is_predis() ) {
2361+
$result = $this->parse_redis_response( $this->redis->set( $derived_key, $serialized, 'px', $pttl ) );
2362+
} else {
2363+
$result = $this->parse_redis_response( $this->redis->set( $derived_key, $serialized, [ 'px' => $pttl ] ) );
2364+
}
2365+
} else {
2366+
$result = $this->parse_redis_response( $this->redis->set( $derived_key, $serialized ) );
2367+
}
23482368

2349-
if ( $result ) {
2350-
$this->add_to_internal_cache( $derived_key, $value );
2351-
$result = $value;
2369+
if ( $result ) {
2370+
$this->add_to_internal_cache( $derived_key, $value );
2371+
$result = $value;
2372+
}
2373+
} else {
2374+
$result = $this->parse_redis_response( $this->redis->incrBy( $derived_key, $offset ) );
2375+
$this->add_to_internal_cache( $derived_key, (int) $this->redis->get( $derived_key ) );
23522376
}
23532377
} catch ( Exception $exception ) {
23542378
$this->handle_exception( $exception );
@@ -2404,13 +2428,28 @@ public function decrement( $key, $offset = 1, $group = 'default' ) {
24042428
}
24052429

24062430
try {
2407-
$value = (int) $this->parse_redis_response( $this->maybe_unserialize( $this->redis->get( $derived_key ) ) );
2408-
$value -= $offset;
2409-
$result = $this->parse_redis_response( $this->redis->set( $derived_key, $this->maybe_serialize( $value ) ) );
2431+
if ( $this->use_igbinary ) {
2432+
$value = (int) $this->parse_redis_response( $this->maybe_unserialize( $this->redis->get( $derived_key ) ) );
2433+
$value -= $offset;
2434+
$serialized = $this->maybe_serialize( $value );
24102435

2411-
if ( $result ) {
2412-
$this->add_to_internal_cache( $derived_key, $value );
2413-
$result = $value;
2436+
if ( ($pttl = $this->redis->pttl( $derived_key )) > 0 ) {
2437+
if ( $this->is_predis() ) {
2438+
$result = $this->parse_redis_response( $this->redis->set( $derived_key, $serialized, 'px', $pttl ) );
2439+
} else {
2440+
$result = $this->parse_redis_response( $this->redis->set( $derived_key, $serialized, [ 'px' => $pttl ] ) );
2441+
}
2442+
} else {
2443+
$result = $this->parse_redis_response( $this->redis->set( $derived_key, $serialized ) );
2444+
}
2445+
2446+
if ( $result ) {
2447+
$this->add_to_internal_cache( $derived_key, $value );
2448+
$result = $value;
2449+
}
2450+
} else {
2451+
$result = $this->parse_redis_response( $this->redis->decrBy( $derived_key, $offset ) );
2452+
$this->add_to_internal_cache( $derived_key, (int) $this->redis->get( $derived_key ) );
24142453
}
24152454
} catch ( Exception $exception ) {
24162455
$this->handle_exception( $exception );
@@ -2738,7 +2777,7 @@ protected function validate_expiration( $expiration ) {
27382777
* @return mixed Unserialized data can be any type.
27392778
*/
27402779
protected function maybe_unserialize( $original ) {
2741-
if ( defined( 'WP_REDIS_IGBINARY' ) && WP_REDIS_IGBINARY && function_exists( 'igbinary_unserialize' ) ) {
2780+
if ( $this->use_igbinary ) {
27422781
return igbinary_unserialize( $original );
27432782
}
27442783

@@ -2764,7 +2803,7 @@ protected function maybe_serialize( $data ) {
27642803
$data = clone $data;
27652804
}
27662805

2767-
if ( defined( 'WP_REDIS_IGBINARY' ) && WP_REDIS_IGBINARY && function_exists( 'igbinary_serialize' ) ) {
2806+
if ( $this->use_igbinary ) {
27682807
return igbinary_serialize( $data );
27692808
}
27702809

0 commit comments

Comments
 (0)