Skip to content

Commit

Permalink
Improve cache invalidation using scan (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethphough committed May 27, 2024
1 parent b06c968 commit 070664d
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/Core/DBI.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,24 @@ public static function delete($table, $id) {
}

private static function invalidateCache($table) {
if (self::$redis) {
$keys = self::$redis->keys(self::generateCacheKey("select:$table", "*"));
foreach ($keys as $key) {
self::$redis->del($key);
}
}
}
if (self::$redis) {
try {
$pattern = self::generateCacheKey("select:$table", "*");
$iterator = null;
do {
$keys = self::$redis->scan($iterator, $pattern);
if ($keys !== false) {
foreach ($keys as $key) {
self::$redis->del($key);
}
}
} while ($iterator > 0);
} catch (\Exception $e) {
error_log("Redis error while invalidating cache for table $table: " . $e->getMessage());
}
}
}


/*
* Return table count
Expand Down

0 comments on commit 070664d

Please sign in to comment.