From 126cadf31af1d2901d62bc3012c5e988214a32ce Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 12 Mar 2024 07:51:13 +0000 Subject: [PATCH] fix cleanup --- src/Abuse/Adapters/Redis.php | 17 +++++++++++++++-- tests/Abuse/AbuseRedisTest.php | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Abuse/Adapters/Redis.php b/src/Abuse/Adapters/Redis.php index da4dc61..e2ebe65 100644 --- a/src/Abuse/Adapters/Redis.php +++ b/src/Abuse/Adapters/Redis.php @@ -174,11 +174,24 @@ public function cleanup(string $datetime): bool // TODO $iterator = NULL; while($iterator !== 0) { - $keys = $this->redis->scan($iterator, self::NAMESPACE . ':*:' . $datetime); - var_dump($keys); + $keys = $this->redis->scan($iterator, self::NAMESPACE . ':*:*', 1000); + $keys = $this->filterKeys($keys, $datetime); $this->redis->del($keys); } return true; } + protected function filterKeys(array $keys, int $timestamp): array { + $filteredKeys = []; + foreach ($keys as $key) { + $parts = explode(':', $key); + $keyTimestamp = (int)end($parts); // Assuming the last part is always the timestamp + if ($keyTimestamp < $timestamp) { + $filteredKeys[] = $key; + } + } + return $filteredKeys; + } + + } \ No newline at end of file diff --git a/tests/Abuse/AbuseRedisTest.php b/tests/Abuse/AbuseRedisTest.php index 7087566..aecedb3 100644 --- a/tests/Abuse/AbuseRedisTest.php +++ b/tests/Abuse/AbuseRedisTest.php @@ -73,6 +73,7 @@ public function testCleanup(): void $logs = $this->abuse->getLogs(0, 10); $this->assertEquals(3, \count($logs)); + sleep(5); // Delete the log $interval = DateInterval::createFromDateString(1 . ' seconds'); $status = $this->abuse->cleanup((new \DateTime())->sub($interval)->getTimestamp());