-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add task to remove cache entries in redis with specified prefix
- Loading branch information
Showing
4 changed files
with
113 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Frosh\Tools\Task; | ||
|
||
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTask; | ||
|
||
class RedisPrefixCleanupTask extends ScheduledTask | ||
{ | ||
final public const NAME = 'frosh.tools.redis_prefix_cleanup'; | ||
|
||
public static function getTaskName(): string | ||
{ | ||
return self::NAME; | ||
} | ||
|
||
public static function getDefaultInterval(): int | ||
{ | ||
return 86400; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Frosh\Tools\Task; | ||
|
||
use Frosh\Tools\Components\CacheRegistry; | ||
use Psr\Log\LoggerInterface; | ||
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; | ||
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler; | ||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
use Symfony\Component\Messenger\Attribute\AsMessageHandler; | ||
|
||
#[AsMessageHandler(handles: RedisPrefixCleanupTask::class)] | ||
class RedisPrefixCleanupTaskHandler extends ScheduledTaskHandler | ||
{ | ||
public function __construct( | ||
EntityRepository $scheduledTaskRepository, | ||
LoggerInterface $logger, | ||
private readonly CacheRegistry $cacheRegistry, | ||
#[Autowire('%shopware.cache.redis_prefix%')] | ||
private readonly string $redisPrefix, | ||
) { | ||
parent::__construct($scheduledTaskRepository, $logger); | ||
} | ||
|
||
public function run(): void | ||
{ | ||
if ($this->redisPrefix === '') { | ||
return; | ||
} | ||
|
||
try { | ||
$redis = $this->cacheRegistry->get('cache.http')->getRedis(); | ||
} catch (\Throwable) { | ||
return; | ||
} | ||
|
||
$cursor = null; | ||
$count = 50; | ||
$deleteKeys = []; | ||
|
||
do { | ||
$keys = $redis->scan($cursor, '*', $count); | ||
|
||
if (!\is_array($keys)) { | ||
return; | ||
} | ||
|
||
foreach ($keys as $key) { | ||
if (\str_starts_with($key, $this->redisPrefix)) { | ||
continue; | ||
} | ||
|
||
/** | ||
* there could be shared database with session, cart or others, | ||
* so we need to make sure that we only delete keys with no ttl | ||
*/ | ||
if ($redis->ttl($key) !== -1) { | ||
continue; | ||
} | ||
|
||
$deleteKeys[] = $key; | ||
} | ||
} while ($cursor !== 0); | ||
|
||
foreach (\array_chunk($deleteKeys, $count) as $chunk) { | ||
$redis->del($chunk); | ||
} | ||
} | ||
} |