diff --git a/src/Cache.php b/src/Cache.php index 9a2d489..4d8ce9c 100644 --- a/src/Cache.php +++ b/src/Cache.php @@ -40,7 +40,7 @@ public static function __callStatic(string $name, array $arguments) throw new Error('PHP-ext apcu not enable. '); } if (!apcu_enabled()) { - throw new Error('You need run shared-cache-enable.sh. '); + throw new Error('You need run workbunny:shared-cache-enable/shared-cache-enable.sh command to enable APCu. '); } return call_user_func([self::class, "_$name"], ...$arguments); } diff --git a/src/Commands/WorkbunnyWebmanSharedCacheEnable.php b/src/Commands/WorkbunnyWebmanSharedCacheEnable.php new file mode 100644 index 0000000..1c81871 --- /dev/null +++ b/src/Commands/WorkbunnyWebmanSharedCacheEnable.php @@ -0,0 +1,72 @@ +setDescription('Enable APCu cache with specified settings.') + ->addOption('file', 'f', InputOption::VALUE_REQUIRED, 'Specify configuration name', 'apcu-cache.ini') + ->addOption('target', 't', InputOption::VALUE_REQUIRED, 'Specify target location', '/usr/local/etc/php/conf.d') + ->addOption('size', 'si', InputOption::VALUE_REQUIRED, 'Configure apcu.shm_size', '1024M') + ->addOption('segments', 'se', InputOption::VALUE_REQUIRED, 'Configure apcu.shm_segments', 1) + ->addOption('mmap', 'm', InputOption::VALUE_REQUIRED, 'Configure apcu.mmap_file_mask', '') + ->addOption('gc_ttl', 'gc', InputOption::VALUE_REQUIRED, 'Configure apcu.gc_ttl', 3600); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $fileName = $input->getOption('file'); + $target = $input->getOption('target'); + $shmSize = $input->getOption('size'); + $shmSegments = $input->getOption('segments'); + $mmapFileMask = $input->getOption('mmap'); + $gcTtl = $input->getOption('gc_ttl'); + + if (!is_dir($target)) { + return $this->error($output, "Target directory does not exist: $target. "); + } + $configContent = <<getHelper('question'); + $question = new ConfirmationQuestion("Configuration file already exists at $filePath. Overwrite? (y/N) ", false); + + if (!$helper->ask($input, $output, $question)) { + return $this->success($output, "Operation aborted. "); + } + } + + file_put_contents($filePath, $configContent); + return $this->success($output, "Configuration file created at: $filePath. "); + } + +} diff --git a/src/Commands/WorkbunnyWebmanSharedCacheList.php b/src/Commands/WorkbunnyWebmanSharedHRecycle.php similarity index 52% rename from src/Commands/WorkbunnyWebmanSharedCacheList.php rename to src/Commands/WorkbunnyWebmanSharedHRecycle.php index 436b3f4..3662cad 100644 --- a/src/Commands/WorkbunnyWebmanSharedCacheList.php +++ b/src/Commands/WorkbunnyWebmanSharedHRecycle.php @@ -2,15 +2,17 @@ namespace Workbunny\WebmanSharedCache\Commands; +use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Workbunny\WebmanSharedCache\Cache; -class WorkbunnyWebmanSharedCacheList extends AbstractCommand +class WorkbunnyWebmanSharedCacheHRecycle extends AbstractCommand { - protected static string $defaultName = 'workbunny:shared-cache-list'; - protected static string $defaultDescription = 'Show workbunny/webman-shared-cache caches list. '; + protected static string $defaultName = 'workbunny:shared-cache-hrecycle'; + protected static string $defaultDescription = 'Manually recycle expired hashKeys. '; /** * @return void @@ -18,8 +20,7 @@ class WorkbunnyWebmanSharedCacheList extends AbstractCommand protected function configure(): void { $this->setName(static::$defaultName)->setDescription(static::$defaultDescription); - $this->addOption('page', 'p', InputOption::VALUE_OPTIONAL, 'Page. ', 1); - $this->addOption('size', 's', InputOption::VALUE_OPTIONAL, 'Page size. ', 20); + $this->addOption('key', 'k', InputOption::VALUE_OPTIONAL, 'Cache Key. '); } /** @@ -29,8 +30,22 @@ protected function configure(): void */ protected function execute(InputInterface $input, OutputInterface $output): int { - $page = $input->getOption('page'); - $size = $input->getOption('size'); + $key = $input->getOption('key'); + if ($key) { + Cache::HRecycle($key); + } else { + $progressBar = new ProgressBar($output); + $progressBar->start(); + $keys = Cache::Keys(); + $progressBar->setMaxSteps(count($keys)); + foreach ($keys as $key) { + $value = Cache::Get($key); + if ( + ($value['_ttl'] ?? null) and ($value['_timestamp'] ?? null)) { + Cache::HRecycle($key); + } + } + } $headers = ['name', 'value']; $rows = []; // todo diff --git a/src/Traits/HashMethods.php b/src/Traits/HashMethods.php index 92ea1e5..aba6970 100644 --- a/src/Traits/HashMethods.php +++ b/src/Traits/HashMethods.php @@ -196,15 +196,21 @@ protected static function _HRecycle(string $key): void $key, $func, $params ) { $hash = self::_Get($key, []); - $now = time(); - foreach ($hash as $hashKey => $hashValue) { - $ttl = $hashValue['_ttl'] ?? 0; - $timestamp = $hashValue['_timestamp'] ?? 0; - if ($ttl > 0 and $timestamp > 0 and $timestamp + $ttl < $now) { - unset($hash[$hashKey]); + if (isset($hash['_ttl']) and isset($hash['_timestamp'])) { + $now = time(); + $set = false; + foreach ($hash as $hashKey => $hashValue) { + $ttl = $hashValue['_ttl'] ?? 0; + $timestamp = $hashValue['_timestamp'] ?? 0; + if ($ttl > 0 and $timestamp > 0 and $timestamp + $ttl < $now) { + $set = true; + unset($hash[$hashKey]); + } + } + if ($set) { + self::_Set($key, $hash); } } - self::_Set($key, $hash); return [ 'timestamp' => microtime(true), 'method' => $func, diff --git a/src/config/plugin/workbunny/webman-shared-cache/command.php b/src/config/plugin/workbunny/webman-shared-cache/command.php new file mode 100644 index 0000000..b872723 --- /dev/null +++ b/src/config/plugin/workbunny/webman-shared-cache/command.php @@ -0,0 +1,17 @@ + + * @copyright chaz6chez + * @link https://github.com/workbunny/webman-push-server + * @license https://github.com/workbunny/webman-push-server/blob/main/LICENSE + */ +declare(strict_types=1); + +return [ + Workbunny\WebmanSharedCache\Commands\WorkbunnyWebmanSharedCacheEnable::class, + Workbunny\WebmanSharedCache\Commands\WorkbunnyWebmanSharedCacheClean::class, +];