diff --git a/src/cache/src/CacheInterface.php b/src/cache/src/CacheInterface.php index d4e2b6473..d3dab20ca 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 */ - 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..f61abffda 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), // Will removed in v3.2 + 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 d43d0d604..975f53659 100644 --- a/src/cache/src/Contract/CacheInterface.php +++ b/src/cache/src/Contract/CacheInterface.php @@ -11,129 +11,13 @@ namespace FriendsOfHyperf\Cache\Contract; -use Closure; -use DateInterval; -use DateTimeInterface; +class_alias(Repository::class, CacheInterface::class); -interface CacheInterface extends \Psr\SimpleCache\CacheInterface -{ +if (false) { // @phpstan-ignore-line /** - * @param string $key - * @param mixed $value - * @param DateInterval|DateTimeInterface|int|null $ttl + * @deprecated since v3.1, use `\FriendsOfHyperf\Cache\Contract\Repository` instead, will removed in v3.2 */ - 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); - - 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); + interface CacheInterface extends Repository + { + } } 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 2f608c45d..1b5255419 100644 --- a/src/cache/src/Repository.php +++ b/src/cache/src/Repository.php @@ -15,6 +15,8 @@ use Closure; use DateInterval; use DateTimeInterface; +use FriendsOfHyperf\Cache\Event\CacheFlushed; +use FriendsOfHyperf\Cache\Event\CacheFlushing; use FriendsOfHyperf\Cache\Event\CacheHit; use FriendsOfHyperf\Cache\Event\CacheMissed; use FriendsOfHyperf\Cache\Event\ForgettingKey; @@ -28,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; @@ -37,16 +40,21 @@ use function Hyperf\Support\with; use function Hyperf\Tappable\tap; -class Repository implements Contract\CacheInterface +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); + } } /** @@ -69,7 +77,15 @@ public function delete($key): bool public function clear(): bool { - return $this->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 +141,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 @@ -349,6 +369,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;