Skip to content

Commit 924b3c4

Browse files
authored
Merge pull request #6473 from paulbalandan/redis-incr
Fix redis cache increment/decrement methods
2 parents e2606a8 + 698928a commit 924b3c4

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

system/Cache/Handlers/RedisHandler.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,15 @@ public function increment(string $key, int $offset = 1)
200200
{
201201
$key = static::validateKey($key, $this->prefix);
202202

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

206206
/**
207207
* {@inheritDoc}
208208
*/
209209
public function decrement(string $key, int $offset = 1)
210210
{
211-
$key = static::validateKey($key, $this->prefix);
212-
213-
return $this->redis->hIncrBy($key, 'data', -$offset);
211+
return $this->increment($key, -$offset);
214212
}
215213

216214
/**

tests/system/Cache/Handlers/RedisHandlerTest.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,22 @@ public function testDeleteMatchingSuffix()
166166
$this->assertSame('keys=90', $dbInfo[0]);
167167
}
168168

169-
// FIXME: I don't like all Hash logic very much. It's wasting memory.
170-
// public function testIncrement()
171-
// {
172-
// }
173-
174-
// public function testDecrement()
175-
// {
176-
// }
169+
public function testIncrementAndDecrement()
170+
{
171+
$this->handler->save('counter', 100);
172+
173+
foreach (range(1, 10) as $step) {
174+
$this->handler->increment('counter', $step);
175+
}
176+
177+
$this->assertSame(155, $this->handler->get('counter'));
178+
179+
$this->handler->decrement('counter', 20);
180+
$this->assertSame(135, $this->handler->get('counter'));
181+
182+
$this->handler->increment('counter', 5);
183+
$this->assertSame(140, $this->handler->get('counter'));
184+
}
177185

178186
public function testClean()
179187
{

0 commit comments

Comments
 (0)