diff --git a/object-cache.php b/object-cache.php index d9d3e9785621b66d0b6adaeb50bf76c9e29bc17d..6fe1600994f6f63fd5f2e732fbda365534082fb1 100644 --- a/object-cache.php +++ b/object-cache.php @@ -468,6 +468,64 @@ class WP_Object_Cache { return $result; } + /** + * Increment a Redis counter by the amount specified + * + * @param string $key + * @param int $offset + * @param string $group + * @return bool + */ + public function increment( $key, $offset = 1, $group = 'default' ) { + $derived_key = $this->build_key( $key, $group ); + $offset = (int) $offset; + + // If group is a non-Redis group, save to runtime cache, not Redis + if ( in_array( $group, $this->no_redis_groups ) ) { + $value = $this->get_from_runtime_cache( $derived_key ); + $value += $offset; + $this->add_to_internal_cache( $derived_key, $value ); + + return true; + } + + // Save to Redis + $result = $this->redis->incrBy( $derived_key, $offset ); + + $this->add_to_internal_cache( $derived_key, (int) $this->redis->get( $derived_key ) ); + + return $result; + } + + /** + * Decrement a Redis counter by the amount specified + * + * @param string $key + * @param int $offset + * @param string $group + * @return bool + */ + public function decrement( $key, $offset = 1, $group = 'default' ) { + $derived_key = $this->build_key( $key, $group ); + $offset = (int) $offset; + + // If group is a non-Redis group, save to runtime cache, not Redis + if ( in_array( $group, $this->no_redis_groups ) ) { + $value = $this->get_from_runtime_cache( $derived_key ); + $value -= $offset; + $this->add_to_internal_cache( $derived_key, $value ); + + return true; + } + + // Save to Redis + $result = $this->redis->decrBy( $derived_key, $offset ); + + $this->add_to_internal_cache( $derived_key, (int) $this->redis->get( $derived_key ) ); + + return $result; + } + /** * Builds a key for the cached object using the blog_id, key, and group values. *