|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CloudComponents\Model\Cache; |
| 9 | + |
| 10 | +use Magento\Framework\App\Cache\Type\FrontendPool; |
| 11 | +use Magento\Framework\App\DeploymentConfig; |
| 12 | +use Psr\Log\LoggerInterface; |
| 13 | +use Credis_Client as Client; |
| 14 | +use Cm_Cache_Backend_Redis as Backend; |
| 15 | + |
| 16 | +/** |
| 17 | + * Performs force key eviction with a "scan" command. |
| 18 | + */ |
| 19 | +class Evictor |
| 20 | +{ |
| 21 | + const DEFAULT_EVICTION_LIMIT = 10000; |
| 22 | + const DEFAULT_SLEEP_TIMEOUT = 20000; |
| 23 | + const CONFIG_PATH_ENABLED = 'cache_evict/enabled'; |
| 24 | + const CONFIG_PATH_LIMIT = 'cache_evict/limit'; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var DeploymentConfig |
| 28 | + */ |
| 29 | + private $deploymentConfig; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var LoggerInterface |
| 33 | + */ |
| 34 | + private $logger; |
| 35 | + |
| 36 | + /** |
| 37 | + * @param DeploymentConfig $deploymentConfig |
| 38 | + * @param LoggerInterface $logger |
| 39 | + */ |
| 40 | + public function __construct(DeploymentConfig $deploymentConfig, LoggerInterface $logger) |
| 41 | + { |
| 42 | + $this->deploymentConfig = $deploymentConfig; |
| 43 | + $this->logger = $logger; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Evicts all keys using iterator. |
| 48 | + * |
| 49 | + * @return int |
| 50 | + */ |
| 51 | + public function evict(): int |
| 52 | + { |
| 53 | + $options = $this->deploymentConfig->getConfigData(FrontendPool::KEY_CACHE)[FrontendPool::KEY_FRONTEND_CACHE] |
| 54 | + ?? []; |
| 55 | + $evictedKeys = 0; |
| 56 | + |
| 57 | + foreach ($options as $name => $cacheConfig) { |
| 58 | + $this->logger->info(sprintf( |
| 59 | + 'Scanning keys for "%s" database', |
| 60 | + $name |
| 61 | + )); |
| 62 | + |
| 63 | + if (!isset( |
| 64 | + $cacheConfig['backend_options']['server'], |
| 65 | + $cacheConfig['backend_options']['port'], |
| 66 | + $cacheConfig['backend_options']['database'] |
| 67 | + )) { |
| 68 | + $this->logger->debug(sprintf( |
| 69 | + 'Cache config for database "%s" config is not valid', |
| 70 | + $name |
| 71 | + )); |
| 72 | + |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + $dbKeys = $this->run( |
| 77 | + $cacheConfig['backend_options']['server'], |
| 78 | + $cacheConfig['backend_options']['port'], |
| 79 | + $cacheConfig['backend_options']['database'] |
| 80 | + ); |
| 81 | + $evictedKeys += $dbKeys; |
| 82 | + |
| 83 | + $this->logger->info(sprintf('Keys scanned: %s', $dbKeys)); |
| 84 | + } |
| 85 | + |
| 86 | + return $evictedKeys; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param string $host |
| 91 | + * @param int $port |
| 92 | + * @param int $db |
| 93 | + * @return int |
| 94 | + */ |
| 95 | + private function run(string $host, int $port, int $db): int |
| 96 | + { |
| 97 | + $client = new Client($host, $port, null, '', $db); |
| 98 | + $evictedKeys = 0; |
| 99 | + |
| 100 | + do { |
| 101 | + $keys = $client->scan( |
| 102 | + $iterator, |
| 103 | + Backend::PREFIX_KEY . '*', |
| 104 | + (int)$this->deploymentConfig->get(self::CONFIG_PATH_LIMIT, self::DEFAULT_EVICTION_LIMIT) |
| 105 | + ); |
| 106 | + |
| 107 | + if ($keys === false) { |
| 108 | + $this->logger->debug('Reached end'); |
| 109 | + } else { |
| 110 | + $keysCount = count($keys); |
| 111 | + $evictedKeys += $keysCount; |
| 112 | + } |
| 113 | + |
| 114 | + /* Give Redis some time to handle other requests */ |
| 115 | + usleep(self::DEFAULT_SLEEP_TIMEOUT); |
| 116 | + } while ($iterator > 0); |
| 117 | + |
| 118 | + return $evictedKeys; |
| 119 | + } |
| 120 | +} |
0 commit comments