diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 367c69f9cb37..38bf64071c96 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -16,6 +16,7 @@ Release Date: Unreleased ## Bugs Fixed +- **Cache:** Fixed a bug where `PredisHandler::increment()` and `PredisHandler::decrement()` updated the unused `'data'` hash field instead of `'__ci_value'`, causing `get()` to return stale values for existing counters. - **CORS:** Fixed a bug where `Cors::hasResponseHeaders()` matched header substrings via `str_contains()`, causing false positives when existing exposed headers shared a prefix with configured exposed headers. - **Database:** Fixed a bug where `Postgre`, `OCI8`, and `SQLSRV` `Forge::_dropKeyAsConstraint()` interpolated table, constraint, and schema names without escaping. - **Database:** Fixed a bug where `Postgre\Connection::insertID()` interpolated table, column, and sequence names without escaping. diff --git a/system/Cache/Handlers/PredisHandler.php b/system/Cache/Handlers/PredisHandler.php index 79e7ae98f916..e8c4759ed9eb 100644 --- a/system/Cache/Handlers/PredisHandler.php +++ b/system/Cache/Handlers/PredisHandler.php @@ -155,14 +155,14 @@ public function increment(string $key, int $offset = 1): int { $key = static::validateKey($key); - return $this->redis->hincrby($key, 'data', $offset); + return $this->redis->hincrby($key, '__ci_value', $offset); } public function decrement(string $key, int $offset = 1): int { $key = static::validateKey($key); - return $this->redis->hincrby($key, 'data', -$offset); + return $this->redis->hincrby($key, '__ci_value', -$offset); } public function clean(): bool diff --git a/tests/system/Cache/Handlers/PredisHandlerTest.php b/tests/system/Cache/Handlers/PredisHandlerTest.php index 91949f53238f..9f5b63b13309 100644 --- a/tests/system/Cache/Handlers/PredisHandlerTest.php +++ b/tests/system/Cache/Handlers/PredisHandlerTest.php @@ -162,6 +162,23 @@ public function testDeleteMatchingNothing(): void $this->assertSame(0, $this->handler->deleteMatching('user_1_info*')); } + public function testIncrementAndDecrement(): void + { + $this->handler->save('counter', 100); + + foreach (range(1, 10) as $step) { + $this->handler->increment('counter', $step); + } + + $this->assertSame(155, $this->handler->get('counter')); + + $this->assertSame(135, $this->handler->decrement('counter', 20)); + $this->assertSame(135, $this->handler->get('counter')); + + $this->assertSame(140, $this->handler->increment('counter', 5)); + $this->assertSame(140, $this->handler->get('counter')); + } + public function testClean(): void { $this->handler->save(self::$key1, 1);