From e12ccfb1796ff63397cfcaa0de404aa4e5879c74 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 26 Mar 2025 21:12:07 +0800 Subject: [PATCH 1/7] Add CacheFlushed Event --- src/cache/src/Contract/CacheInterface.php | 4 ++++ src/cache/src/Event/CacheFlushed.php | 19 +++++++++++++++++++ src/cache/src/Event/CacheFlushing.php | 19 +++++++++++++++++++ src/cache/src/Repository.php | 18 ++++++++++++++++-- 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/cache/src/Event/CacheFlushed.php create mode 100644 src/cache/src/Event/CacheFlushing.php diff --git a/src/cache/src/Contract/CacheInterface.php b/src/cache/src/Contract/CacheInterface.php index d43d0d604..bc322002a 100644 --- a/src/cache/src/Contract/CacheInterface.php +++ b/src/cache/src/Contract/CacheInterface.php @@ -37,6 +37,10 @@ public function add($key, $value, $ttl = null): bool; */ public function flexible($key, $ttl, $callback, $lock = null); + /** + * Alias for the "clear" method. + * @deprecated since v3.1, use "clear" instead, will removed at v3.2 + */ public function flush(): bool; /** diff --git a/src/cache/src/Event/CacheFlushed.php b/src/cache/src/Event/CacheFlushed.php new file mode 100644 index 000000000..cacb50eb8 --- /dev/null +++ b/src/cache/src/Event/CacheFlushed.php @@ -0,0 +1,19 @@ +flush(); + $this->event(new CacheFlushing($this->getName())); + + $result = $this->driver->clear(); + + if ($result) { + $this->event(new CacheFlushed($this->getName())); + } + + return $result; } public function add($key, $value, $ttl = null): bool @@ -125,9 +135,13 @@ public function flexible($key, $ttl, $callback, $lock = null) return $value; } + /** + * Alias for the "clear" method. + * @deprecated since v3.1, use "clear" instead, will removed at v3.2 + */ public function flush(): bool { - return $this->driver->clear(); + return $this->clear(); } public function forever($key, $value): bool From d26504a787502708cbe62c7fb76ac11c8ce848a8 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 26 Mar 2025 21:17:09 +0800 Subject: [PATCH 2/7] =?UTF-8?q?feat(=E7=BC=93=E5=AD=98):=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=8E=B7=E5=8F=96=E9=A9=B1=E5=8A=A8=E5=92=8C=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 `Repository` 类中添加 `getDriver` 和 `getStore` 方法,用于获取缓存驱动实例。同时在 `CacheInterface` 接口中声明这两个方法,确保一致性。`getStore` 是 `getDriver` 的别名,提供更直观的访问方式。 --- src/cache/src/Contract/CacheInterface.php | 8 ++++++++ src/cache/src/Repository.php | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/cache/src/Contract/CacheInterface.php b/src/cache/src/Contract/CacheInterface.php index bc322002a..c0ca24c2a 100644 --- a/src/cache/src/Contract/CacheInterface.php +++ b/src/cache/src/Contract/CacheInterface.php @@ -14,6 +14,7 @@ use Closure; use DateInterval; use DateTimeInterface; +use Hyperf\Cache\Driver\DriverInterface; interface CacheInterface extends \Psr\SimpleCache\CacheInterface { @@ -140,4 +141,11 @@ public function rememberForever($key, Closure $callback); * @return TCacheValue */ public function sear($key, Closure $callback); + + public function getDriver(): DriverInterface; + + /** + * Alias for the `getDriver` method. + */ + public function getStore(): DriverInterface; } diff --git a/src/cache/src/Repository.php b/src/cache/src/Repository.php index 68f5886df..5a39d0b28 100644 --- a/src/cache/src/Repository.php +++ b/src/cache/src/Repository.php @@ -363,6 +363,16 @@ public function deleteMultiple($keys): bool return $result; } + public function getDriver(): DriverInterface + { + return $this->driver; + } + + public function getStore(): DriverInterface + { + return $this->getDriver(); + } + protected function getName(): string { return $this->name; From e0d46ef7e11982ffa2cf91f00e01d088545cfe5d Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 26 Mar 2025 21:22:02 +0800 Subject: [PATCH 3/7] =?UTF-8?q?refactor(cache):=20=E5=B0=86=20CacheInterfa?= =?UTF-8?q?ce=20=E6=9B=BF=E6=8D=A2=E4=B8=BA=20Repository=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构缓存模块,将原有的 CacheInterface 替换为 Repository 接口,以统一命名并提高代码的可维护性。此次更改涉及多个文件,包括 Repository.php、CacheInterface.php 等,确保所有相关依赖项都更新为使用新的 Repository 接口。 --- src/cache/src/CacheInterface.php | 6 +- src/cache/src/CacheManager.php | 10 +- src/cache/src/ConfigProvider.php | 5 +- src/cache/src/Contract/CacheInterface.php | 140 +------------------- src/cache/src/Contract/Repository.php | 151 ++++++++++++++++++++++ src/cache/src/Facade/Cache.php | 8 +- src/cache/src/Repository.php | 2 +- 7 files changed, 171 insertions(+), 151 deletions(-) create mode 100644 src/cache/src/Contract/Repository.php diff --git a/src/cache/src/CacheInterface.php b/src/cache/src/CacheInterface.php index d4e2b6473..c527e9794 100644 --- a/src/cache/src/CacheInterface.php +++ b/src/cache/src/CacheInterface.php @@ -11,13 +11,13 @@ namespace FriendsOfHyperf\Cache; -class_alias(Contract\CacheInterface::class, CacheInterface::class); +class_alias(Contract\Repository::class, CacheInterface::class); if (false) { // @phpstan-ignore-line /** - * @deprecated since v3.1, use `\FriendsOfHyperf\Cache\Contract\Repository` instead, will removed in v3.2 + * @deprecated since v3.1, use `\FriendsOfHyperf\Cache\Contract\RepositoryInterface` instead, will removed in v3.2 */ - interface CacheInterface extends Contract\CacheInterface + interface CacheInterface extends Contract\Repository { } } diff --git a/src/cache/src/CacheManager.php b/src/cache/src/CacheManager.php index 44ad8e672..266505f9e 100644 --- a/src/cache/src/CacheManager.php +++ b/src/cache/src/CacheManager.php @@ -11,7 +11,7 @@ namespace FriendsOfHyperf\Cache; -use FriendsOfHyperf\Cache\Contract\CacheInterface; +use FriendsOfHyperf\Cache\Contract\Repository; use Hyperf\Cache\CacheManager as HyperfCacheManager; use function Hyperf\Support\make; @@ -19,7 +19,7 @@ class CacheManager { /** - * @var CacheInterface[] + * @var Repository[] */ protected array $drivers = []; @@ -30,7 +30,7 @@ public function __construct(protected HyperfCacheManager $cacheManager) /** * Get a cache driver instance. */ - public function store(string $name = 'default'): CacheInterface + public function store(string $name = 'default'): Repository { return $this->drivers[$name] ?? $this->drivers[$name] = $this->resolve($name); } @@ -38,7 +38,7 @@ public function store(string $name = 'default'): CacheInterface /** * Alias for the "store" method. */ - public function driver(string $name = 'default'): CacheInterface + public function driver(string $name = 'default'): Repository { return $this->store($name); } @@ -46,7 +46,7 @@ public function driver(string $name = 'default'): CacheInterface /** * Resolve a cache repository instance. */ - public function resolve(string $name): CacheInterface + public function resolve(string $name): Repository { return make(Repository::class, [ 'name' => $name, diff --git a/src/cache/src/ConfigProvider.php b/src/cache/src/ConfigProvider.php index 7fc314cbe..5e60d413f 100644 --- a/src/cache/src/ConfigProvider.php +++ b/src/cache/src/ConfigProvider.php @@ -17,8 +17,9 @@ public function __invoke(): array { return [ 'dependencies' => [ - Contract\CacheInterface::class => RepositoryFactory::class, - CacheInterface::class => fn ($container) => $container->get(Contract\CacheInterface::class), // Will removed in v3.2 + Contract\Repository::class => RepositoryFactory::class, + Contract\CacheInterface::class => fn ($container) => $container->get(Contract\Repository::class), + CacheInterface::class => fn ($container) => $container->get(Contract\Repository::class), // Will removed in v3.2 ], ]; } diff --git a/src/cache/src/Contract/CacheInterface.php b/src/cache/src/Contract/CacheInterface.php index c0ca24c2a..aa338ab99 100644 --- a/src/cache/src/Contract/CacheInterface.php +++ b/src/cache/src/Contract/CacheInterface.php @@ -11,141 +11,9 @@ namespace FriendsOfHyperf\Cache\Contract; -use Closure; -use DateInterval; -use DateTimeInterface; -use Hyperf\Cache\Driver\DriverInterface; - -interface CacheInterface extends \Psr\SimpleCache\CacheInterface +/** + * @deprecated since v3.1, use `\FriendsOfHyperf\Cache\Contract\RepositoryInterface` instead, will removed in v3.2 + */ +interface CacheInterface extends Repository { - /** - * @param string $key - * @param mixed $value - * @param DateInterval|DateTimeInterface|int|null $ttl - */ - public function add($key, $value, $ttl = null): bool; - - /** - * Retrieve an item from the cache by key, refreshing it in the background if it is stale. - * - * @template TCacheValue - * - * @param string $key - * @param array{ 0: DateTimeInterface|DateInterval|int, 1: DateTimeInterface|DateInterval|int } $ttl - * @param (callable(): TCacheValue) $callback - * @param array{ seconds?: int, owner?: string }|null $lock - * @return TCacheValue - */ - public function flexible($key, $ttl, $callback, $lock = null); - - /** - * Alias for the "clear" method. - * @deprecated since v3.1, use "clear" instead, will removed at v3.2 - */ - public function flush(): bool; - - /** - * @param string $key - * @param mixed $value - */ - public function forever($key, $value): bool; - - /** - * @param string $key - */ - public function forget($key): bool; - - /** - * Determine if an item exists in the cache. - * - * @param string $key - */ - public function has($key): bool; - - /** - * Determine if an item doesn't exist in the cache. - * - * @param string $key - */ - public function missing($key): bool; - - /** - * @param array|string $key - * @param mixed $value - * @param DateInterval|DateTimeInterface|int|null $ttl - */ - public function put($key, $value, $ttl = null): bool; - - public function putMany(array $values, $ttl = null): bool; - - /** - * @param string $key - * @param int $value - * @return bool|int - */ - public function decrement($key, $value = 1); - - /** - * @param string $key - * @param int $value - * @return bool|int - */ - public function increment($key, $value = 1); - - /** - * @return iterable - */ - public function many(array $keys); - - /** - * Retrieve an item from the cache and delete it. - * - * @template TCacheValue - * - * @param string[]|string $key - * @param (Closure(): TCacheValue)|TCacheValue $default - * @return (TCacheValue is null ? mixed : TCacheValue) - */ - public function pull($key, $default = null); - - /** - * Get an item from the cache, or execute the given Closure and store the result. - * - * @template TCacheValue - * - * @param string $key - * @param DateInterval|DateTimeInterface|int|null $ttl - * @param Closure(): TCacheValue $callback - * @return TCacheValue - */ - public function remember($key, $ttl, Closure $callback); - - /** - * Get an item from the cache, or execute the given Closure and store the result forever. - * - * @template TCacheValue - * - * @param string $key - * @param Closure(): TCacheValue $callback - * @return TCacheValue - */ - public function rememberForever($key, Closure $callback); - - /** - * Get an item from the cache, or execute the given Closure and store the result forever. - * - * @template TCacheValue - * - * @param string $key - * @param Closure(): TCacheValue $callback - * @return TCacheValue - */ - public function sear($key, Closure $callback); - - public function getDriver(): DriverInterface; - - /** - * Alias for the `getDriver` method. - */ - public function getStore(): DriverInterface; } diff --git a/src/cache/src/Contract/Repository.php b/src/cache/src/Contract/Repository.php new file mode 100644 index 000000000..242bbe36f --- /dev/null +++ b/src/cache/src/Contract/Repository.php @@ -0,0 +1,151 @@ +{$name}(...$arguments); } - public static function store(string $name = 'default'): CacheInterface + public static function store(string $name = 'default'): Repository { return self::getFacadeAccessor()->store($name); } - public static function driver(string $name = 'default'): CacheInterface + public static function driver(string $name = 'default'): Repository { return self::getFacadeAccessor()->driver($name); } - public static function resolve(string $name): CacheInterface + public static function resolve(string $name): Repository { return self::getFacadeAccessor()->resolve($name); } diff --git a/src/cache/src/Repository.php b/src/cache/src/Repository.php index 5a39d0b28..9a9c291b8 100644 --- a/src/cache/src/Repository.php +++ b/src/cache/src/Repository.php @@ -39,7 +39,7 @@ use function Hyperf\Support\with; use function Hyperf\Tappable\tap; -class Repository implements Contract\CacheInterface +class Repository implements Contract\Repository { use InteractsWithTime; use Macroable; From 5c89a3e24a98a97aa166cb4598c80fb098a02d51 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 26 Mar 2025 21:23:01 +0800 Subject: [PATCH 4/7] =?UTF-8?q?docs(cache):=20=E6=9B=B4=E6=96=B0=E5=BC=83?= =?UTF-8?q?=E7=94=A8=E4=BF=A1=E6=81=AF=E4=B8=AD=E7=9A=84=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=BC=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将弃用信息中的 `RepositoryInterface` 替换为 `Repository`,以保持一致性并避免混淆。 --- src/cache/src/CacheInterface.php | 2 +- src/cache/src/Contract/CacheInterface.php | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/cache/src/CacheInterface.php b/src/cache/src/CacheInterface.php index c527e9794..d3dab20ca 100644 --- a/src/cache/src/CacheInterface.php +++ b/src/cache/src/CacheInterface.php @@ -15,7 +15,7 @@ class_alias(Contract\Repository::class, CacheInterface::class); if (false) { // @phpstan-ignore-line /** - * @deprecated since v3.1, use `\FriendsOfHyperf\Cache\Contract\RepositoryInterface` instead, will removed in v3.2 + * @deprecated since v3.1, use `\FriendsOfHyperf\Cache\Contract\Repository` instead, will removed in v3.2 */ interface CacheInterface extends Contract\Repository { diff --git a/src/cache/src/Contract/CacheInterface.php b/src/cache/src/Contract/CacheInterface.php index aa338ab99..5af34e72a 100644 --- a/src/cache/src/Contract/CacheInterface.php +++ b/src/cache/src/Contract/CacheInterface.php @@ -11,9 +11,11 @@ namespace FriendsOfHyperf\Cache\Contract; -/** - * @deprecated since v3.1, use `\FriendsOfHyperf\Cache\Contract\RepositoryInterface` instead, will removed in v3.2 - */ -interface CacheInterface extends Repository -{ +if (false) { // @phpstan-ignore-line + /** + * @deprecated since v3.1, use `\FriendsOfHyperf\Cache\Contract\Repository` instead, will removed in v3.2 + */ + interface CacheInterface extends Repository + { + } } From 170d2b3a2b20b6c19738ddc9197ddbc49b4dd79e Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 26 Mar 2025 21:23:58 +0800 Subject: [PATCH 5/7] =?UTF-8?q?refactor(cache):=20=E6=A0=87=E8=AE=B0=20v3.?= =?UTF-8?q?2=20=E4=B8=AD=E5=B0=86=E8=A2=AB=E7=A7=BB=E9=99=A4=E7=9A=84?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 ConfigProvider 中标记了 v3.2 版本中将被移除的依赖项,以便为未来的代码清理做准备。 --- src/cache/src/ConfigProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cache/src/ConfigProvider.php b/src/cache/src/ConfigProvider.php index 5e60d413f..f61abffda 100644 --- a/src/cache/src/ConfigProvider.php +++ b/src/cache/src/ConfigProvider.php @@ -18,7 +18,7 @@ public function __invoke(): array return [ 'dependencies' => [ Contract\Repository::class => RepositoryFactory::class, - Contract\CacheInterface::class => fn ($container) => $container->get(Contract\Repository::class), + Contract\CacheInterface::class => fn ($container) => $container->get(Contract\Repository::class), // Will removed in v3.2 CacheInterface::class => fn ($container) => $container->get(Contract\Repository::class), // Will removed in v3.2 ], ]; From 3846825f8976edd4364255b6e020e93525346375 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 26 Mar 2025 21:34:20 +0800 Subject: [PATCH 6/7] =?UTF-8?q?refactor(cache):=20=E4=BD=BF=E7=94=A8=20cla?= =?UTF-8?q?ss=5Falias=20=E6=9B=BF=E6=8D=A2=20CacheInterface?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为了简化代码并提高可维护性,使用 `class_alias` 将 `CacheInterface` 替换为 `Repository`。此更改不影响现有功能,但为未来的版本升级做好准备。 --- src/cache/src/Contract/CacheInterface.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cache/src/Contract/CacheInterface.php b/src/cache/src/Contract/CacheInterface.php index 5af34e72a..975f53659 100644 --- a/src/cache/src/Contract/CacheInterface.php +++ b/src/cache/src/Contract/CacheInterface.php @@ -11,6 +11,8 @@ namespace FriendsOfHyperf\Cache\Contract; +class_alias(Repository::class, CacheInterface::class); + if (false) { // @phpstan-ignore-line /** * @deprecated since v3.1, use `\FriendsOfHyperf\Cache\Contract\Repository` instead, will removed in v3.2 From 9f19483e9d3cda044e5633f76f4cf3eb540b08fb Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 26 Mar 2025 21:41:54 +0800 Subject: [PATCH 7/7] =?UTF-8?q?refactor(cache):=20=E9=87=8D=E6=9E=84=20Rep?= =?UTF-8?q?ository=20=E6=9E=84=E9=80=A0=E5=87=BD=E6=95=B0=E4=BB=A5?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=AE=B9=E5=99=A8=E6=B3=A8=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构 Repository 构造函数,将 ContainerInterface 作为依赖注入,以便动态获取 EventDispatcherInterface 实例。这提高了代码的灵活性和可测试性。 --- src/cache/src/Repository.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cache/src/Repository.php b/src/cache/src/Repository.php index 9a9c291b8..1b5255419 100644 --- a/src/cache/src/Repository.php +++ b/src/cache/src/Repository.php @@ -30,6 +30,7 @@ use Hyperf\Cache\Driver\DriverInterface; use Hyperf\Macroable\Macroable; use Hyperf\Support\Traits\InteractsWithTime; +use Psr\Container\ContainerInterface; use Psr\EventDispatcher\EventDispatcherInterface; use function FriendsOfHyperf\Lock\lock; @@ -44,11 +45,16 @@ class Repository implements Contract\Repository use InteractsWithTime; use Macroable; + protected ?EventDispatcherInterface $eventDispatcher = null; + public function __construct( + protected ContainerInterface $container, protected DriverInterface $driver, - protected ?EventDispatcherInterface $eventDispatcher = null, protected string $name = 'default' ) { + if ($container->has(EventDispatcherInterface::class)) { + $this->eventDispatcher = $container->get(EventDispatcherInterface::class); + } } /**