Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Future.php
  • Loading branch information
chaz6chez committed Sep 4, 2024
2 parents 104f415 + de3d27f commit 3371c84
Show file tree
Hide file tree
Showing 14 changed files with 409 additions and 102 deletions.
1 change: 1 addition & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
- "8.3"
- "8.2"
- "8.1"
- "8.0"
steps:
- uses: actions/checkout@v3
- uses: shivammathur/setup-php@v2
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@


<div align="center">
<!--<a href="https://github.com/workbunny/webman-shared-cache/actions">
<a href="https://github.com/workbunny/webman-shared-cache/actions">
<img src="https://github.com/workbunny/webman-shared-cache/actions/workflows/CI.yml/badge.svg" alt="Build Status">
</a>-->
<a href="https://github.com/workbunny/webman-shared-cache/releases">
<img alt="Latest Stable Version" src="https://badgen.net/packagist/v/workbunny/webman-shared-cache/latest">
</a>
<a href="https://github.com/workbunny/webman-shared-cache/releases">
<img alt="Latest Stable Version" src="https://badgen.net/packagist/v/workbunny/webman-shared-cache/latest">
</a>
<a href="https://github.com/workbunny/webman-shared-cache/blob/main/composer.json">
<img alt="PHP Version Require" src="http://poser.pugx.org/workbunny/webman-shared-cache/require/php">
<img alt="PHP Version Require" src="https://badgen.net/packagist/php/workbunny/webman-shared-cache">
</a>
<a href="https://github.com/workbunny/webman-shared-cache/blob/main/LICENSE">
<img alt="GitHub license" src="http://poser.pugx.org/workbunny/webman-shared-cache/license">
<img alt="GitHub license" src="https://badgen.net/packagist/license/workbunny/webman-shared-cache">
</a>

</div>
Expand Down Expand Up @@ -121,6 +118,7 @@
- 支持 HSet/HGet/HDel/HKeys/HExists
- 支持 HIncr/HDecr,支持浮点运算
- 支持 储存对象数据
- 支持 HashKey的秒级过期时间【版本 ≥ 0.5】

- **通配符/正则匹配Search**
```php
Expand All @@ -138,6 +136,8 @@
}
);
```
**Tips:Cache::Search()本质上是个扫表匹配的过程,是O(N)的操作,如果需要对特定族群的数据进行监听,推荐使用Channel相关函数实现监听。**
- **原子性执行**
```php
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"source": "https://github.com/workbunny/webman-shared-cache"
},
"require": {
"php": "^8.1",
"php": "^8.0",
"ext-apcu": "*"
},
"require-dev": {
Expand Down
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
34 changes: 34 additions & 0 deletions src/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,52 @@

abstract class AbstractCommand extends Command
{
/**
* 兼容webman console,需重写
*
* @var string
*/
protected static string $defaultName = '';

/**
* 兼容webman console,需重写
*
* @var string
*/
protected static string $defaultDescription = '';

/**
* 输出info
*
* @param OutputInterface $output
* @param string $message
* @return void
*/
protected function info(OutputInterface $output, string $message): void
{
$output->writeln("ℹ️ $message");
}

/**
* 输出error
*
* @param OutputInterface $output
* @param string $message
* @return int
*/
protected function error(OutputInterface $output, string $message): int
{
$output->writeln("$message");
return self::FAILURE;
}

/**
* 输出success
*
* @param OutputInterface $output
* @param string $message
* @return int
*/
protected function success(OutputInterface $output, string $message): int
{
$output->writeln("$message");
Expand Down
6 changes: 4 additions & 2 deletions src/Commands/WorkbunnyWebmanSharedCacheClean.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

class WorkbunnyWebmanSharedCacheClean extends AbstractCommand
{
protected static string $defaultName = 'workbunny:shared-cache-clean';
protected static string $defaultDescription = 'Remove all workbunny/webman-shared-cache caches. ';

/**
* @return void
*/
protected function configure(): void
{
$this->setName('workbunny:shared-cache-clean')
->setDescription('Remove all workbunny/webman-shared-cache caches. ');
$this->setName(static::$defaultName)->setDescription(static::$defaultDescription);
}

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

}
48 changes: 48 additions & 0 deletions src/Commands/WorkbunnyWebmanSharedCacheHRecycle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types=1);

namespace Workbunny\WebmanSharedCache\Commands;

use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Workbunny\WebmanSharedCache\Cache;

class WorkbunnyWebmanSharedCacheHRecycle extends AbstractCommand
{
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('key', 'k', InputOption::VALUE_OPTIONAL, 'Cache Key. ');
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$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) {
Cache::HRecycle($key);
$progressBar->advance();
}
$progressBar->finish();
}
return $this->success($output, 'HRecycle Success. ');
}
}
44 changes: 0 additions & 44 deletions src/Commands/WorkbunnyWebmanSharedCacheList.php

This file was deleted.

3 changes: 2 additions & 1 deletion src/Future.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ class Future
/**
* @param Closure $func
* @param array $args
* @param float|int|null $interval
* @return int|false
*/
public static function add(Closure $func, array $args = []): int|false
public static function add(Closure $func, array $args = [], float|int|null $interval = null): int|false
{
if (self::$debug) {
self::$debugFunc = $func;
Expand Down
Loading

0 comments on commit 3371c84

Please sign in to comment.