Skip to content

Commit

Permalink
Merge pull request #6473 from paulbalandan/redis-incr
Browse files Browse the repository at this point in the history
Fix redis cache increment/decrement methods
  • Loading branch information
kenjis committed Sep 2, 2022
2 parents e2606a8 + 698928a commit 924b3c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
6 changes: 2 additions & 4 deletions system/Cache/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,15 @@ public function increment(string $key, int $offset = 1)
{
$key = static::validateKey($key, $this->prefix);

return $this->redis->hIncrBy($key, 'data', $offset);
return $this->redis->hIncrBy($key, '__ci_value', $offset);
}

/**
* {@inheritDoc}
*/
public function decrement(string $key, int $offset = 1)
{
$key = static::validateKey($key, $this->prefix);

return $this->redis->hIncrBy($key, 'data', -$offset);
return $this->increment($key, -$offset);
}

/**
Expand Down
24 changes: 16 additions & 8 deletions tests/system/Cache/Handlers/RedisHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,22 @@ public function testDeleteMatchingSuffix()
$this->assertSame('keys=90', $dbInfo[0]);
}

// FIXME: I don't like all Hash logic very much. It's wasting memory.
// public function testIncrement()
// {
// }

// public function testDecrement()
// {
// }
public function testIncrementAndDecrement()
{
$this->handler->save('counter', 100);

foreach (range(1, 10) as $step) {
$this->handler->increment('counter', $step);
}

$this->assertSame(155, $this->handler->get('counter'));

$this->handler->decrement('counter', 20);
$this->assertSame(135, $this->handler->get('counter'));

$this->handler->increment('counter', 5);
$this->assertSame(140, $this->handler->get('counter'));
}

public function testClean()
{
Expand Down

0 comments on commit 924b3c4

Please sign in to comment.