I'm using Laravel's cache system with Redis (phpredis driver) and encountered an issue where $connection->del($key) does not delete the key, but using executeRaw(['DEL', $key]) does.
Setup Details: Laravel version: 10.0.3 Redis client: phpredis Redis version: 7.4.1 Cache prefix: laravel_cache_solar_cache For the project we use docker containers, Redis is in a separate container, but I don't think this is the issue.
I have the following Redis key stored:
laravel_cache_solar_cache:v1/verticals-menu:userId:4:b6f331f8f609d4bf3e9f4568bd69aacba4e464aa
When I try to delete it using Laravel's Redis connection:
$connection->del($key);
it does not get deleted.
However, using executeRaw(['DEL', $key]) does work:
Additionally, Cache::delete($key) also works, but since we have a cluster configuration, it is not a suitable approach for us.
P.S. This is how the redis connection is initialised:
foreach ($this->clusterConnectionNames as $clusterConnectionName) {
try {
$this->logService->info("Processing crossClusterFlushByKeys for cluster '$clusterConnectionName'");
/** @var RedisStore $store */
$store = Cache::store($clusterConnectionName)->getStore();
/** @var PhpRedisConnection $connection **/
$connection = $store->connection();
// Retrieve Redis prefixes from configuration
$prefix = Config::get('database.redis.options.prefix');
$cachePrefix = $store->getPrefix();
$scanPatternBase = $prefix . $cachePrefix;
// Initialize a counter for the total number of keys deleted in this cluster
$totalDeleted = 0;
foreach ($keysForDeletion as $baseKey) {
$matchPattern = $scanPatternBase . $baseKey . "*";
$cursor = null;
do {
$scanResult = $connection->scan($cursor, [
'match' => $matchPattern,
'count' => 1000
]);
if ($scanResult === false) {
$this->logService->warning("SCAN command failed for pattern '$matchPattern' on cluster '$clusterConnectionName'");
break;
}
[$cursor, $matches] = $scanResult;
if (!empty($matches)) {
$this->logService->info("Deleting " . count($matches) . " keys from cluster '$clusterConnectionName'", [
'keys' => $matches
]);
foreach ($matches as $key) {
$connection->executeRaw(['DEL', $key]);
}
}
} while ($cursor != 0);
}
$this->logService->info("Total keys deleted from cluster '$clusterConnectionName': $totalDeleted");
This implementation works, but if we use $connection->del($key); It is not deleting the key.