Skip to content

Commit

Permalink
feat: Add HRecycle() to manually recycle expired hashKey
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Aug 21, 2024
1 parent 596dba9 commit 273cf9c
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
72 changes: 72 additions & 0 deletions src/Commands/WorkbunnyWebmanSharedCacheEnable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types=1);

namespace Workbunny\WebmanSharedCache\Commands;

use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

class WorkbunnyWebmanSharedCacheEnable extends AbstractCommand
{
protected static string $defaultName = 'workbunny:shared-cache-enable';
protected static string $defaultDescription = 'Enable APCu cache with specified settings. ';

/**
* @return void
*/
protected function configure(): void
{
$this
->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 = <<<EOF
apc.enabled=1
apc.enable_cli=1
apc.shm_segments=$shmSegments
apc.shm_size=$shmSize
apc.mmap_file_mask=$mmapFileMask
apc.gc_ttl=$gcTtl
EOF;
$filePath = "$target/$fileName";

if (file_exists($filePath)) {
/** @var QuestionHelper $helper */
$helper = $this->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. ");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

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
*/
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. ');
}

/**
Expand All @@ -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
Expand Down
20 changes: 13 additions & 7 deletions src/Traits/HashMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions src/config/plugin/workbunny/webman-shared-cache/command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* This file is part of workbunny.
*
* Redistributions of files must retain the above copyright notice.
*
* @author chaz6chez<[email protected]>
* @copyright chaz6chez<[email protected]>
* @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,
];

0 comments on commit 273cf9c

Please sign in to comment.