Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cache/src/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
}
10 changes: 5 additions & 5 deletions src/cache/src/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

namespace FriendsOfHyperf\Cache;

use FriendsOfHyperf\Cache\Contract\CacheInterface;
use FriendsOfHyperf\Cache\Contract\Repository;
use Hyperf\Cache\CacheManager as HyperfCacheManager;

use function Hyperf\Support\make;

class CacheManager
{
/**
* @var CacheInterface[]
* @var Repository[]
*/
protected array $drivers = [];

Expand All @@ -30,23 +30,23 @@ 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);
}

/**
* Alias for the "store" method.
*/
public function driver(string $name = 'default'): CacheInterface
public function driver(string $name = 'default'): Repository
{
return $this->store($name);
}

/**
* Resolve a cache repository instance.
*/
public function resolve(string $name): CacheInterface
public function resolve(string $name): Repository
{
return make(Repository::class, [
'name' => $name,
Expand Down
5 changes: 3 additions & 2 deletions src/cache/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
],
];
}
Expand Down
128 changes: 6 additions & 122 deletions src/cache/src/Contract/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
}
151 changes: 151 additions & 0 deletions src/cache/src/Contract/Repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact huangdijia@gmail.com
*/

namespace FriendsOfHyperf\Cache\Contract;

use Closure;
use DateInterval;
use DateTimeInterface;
use Hyperf\Cache\Driver\DriverInterface;

interface Repository extends \Psr\SimpleCache\CacheInterface
{
/**
* @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;
}
19 changes: 19 additions & 0 deletions src/cache/src/Event/CacheFlushed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact huangdijia@gmail.com
*/

namespace FriendsOfHyperf\Cache\Event;

class CacheFlushed
{
public function __construct(public ?string $storeName)
{
}
}
Loading