Skip to content

Commit

Permalink
update Cached::$cache to Cached::$store
Browse files Browse the repository at this point in the history
  • Loading branch information
david-d-h committed Dec 12, 2023
1 parent 26ec90c commit 6fe6c30
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ to easily cache data based on a schedule or an event.
## "Reactive" caches

To get started with this Cached class, make a `HelloCache` class like so.
This cache will respond to a`TestEvent` by caching whatever will
This cache will respond to a `TestEvent` by caching whatever will
be returned from the `run` method.

```php
Expand All @@ -38,7 +38,7 @@ use Vormkracht10\PermanentCache\Cached;

class HelloCache extends Cached
{
protected $cache = 'redis:hello';
protected $store = 'redis:hello';

public function run(TestEvent $event): string
{
Expand All @@ -47,9 +47,9 @@ class HelloCache extends Cached
}
```

To know *where* to cache the returned value, we have the `$cache` property.
To know *where* to cache the returned value, we have the `$store` property.
This is formatted like `driver:identifier`, but you can also omit the `driver:`
like so `protected $cache = 'hello';` and we will use the config's `cache.default` value instead.
like so `protected $store = 'hello';` and we will use the config's `cache.default` value instead.

##### if you don't want to type hint the `TestEvent` class in the `run` method, you can also explicitly specify the type like so `protected $event = TestEvent::class;`

Expand All @@ -68,7 +68,7 @@ use Vormkracht10\PermanentCache\Scheduled;

class MinutesCache extends Cached implements Scheduled
{
protected $cache = 'redis:minutes';
protected $store = 'redis:minutes';

protected $expression = '* * * * *';

Expand All @@ -88,7 +88,7 @@ Let's take our previous snippet, and edit it a little to use Laravel's `Schedule
```php
class MinutesCache extends Cached implements Scheduled
{
protected $cache = 'redis:minutes';
protected $store = 'redis:minutes';

public function run(): mixed
{
Expand All @@ -112,7 +112,7 @@ class HelloCache extends Cached implements ShouldQueue
{
protected $connection = 'redis';

protected $cache = 'redis:hello';
protected $store = 'redis:hello';

public function run(TestEvent $event): string
{
Expand All @@ -121,7 +121,7 @@ class HelloCache extends Cached implements ShouldQueue
}
```

You can specify a whole bunch of things, like the queue connection using the `$connection` property.
You can specify a bunch of things, like the queue connection using the `$connection` property.
You can basically configure you cache as a Laravel job. This works because the `Cached` class from which
we are inheriting is structured like a Laravel job!

Expand Down
18 changes: 9 additions & 9 deletions src/Cached.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class Cached
*
* @var string|null
*/
protected $cache = null;
protected $store = null;

/**
* The event that this cacher will listen for, this is optional
Expand All @@ -49,8 +49,8 @@ abstract class Cached
*/
final public function handle($event = null): void
{
[$driver, $ident] = self::parseCacheString($this->cache
?? throw new \Exception('The $cache property in ['.static::class.'] must be overridden'),
[$driver, $ident] = self::parseCacheString($this->store
?? throw new \Exception('The $store property in ['.static::class.'] must be overridden'),
);
Cache::driver($driver)->forever($ident,
/** @phpstan-ignore-next-line */
Expand Down Expand Up @@ -79,12 +79,12 @@ final public static function update(): void
*/
final public static function get(): mixed
{
$cache = (new ReflectionClass(static::class))
->getProperty('cache')
$store = (new ReflectionClass(static::class))
->getProperty('store')
->getDefaultValue();

[$driver, $ident] = self::parseCacheString($cache
?? throw new \Exception('The $cache property in ['.static::class.'] must be overridden'),
[$driver, $ident] = self::parseCacheString($store
?? throw new \Exception('The $store property in ['.static::class.'] must be overridden'),
);

return Cache::driver($driver)->get($ident);
Expand Down Expand Up @@ -130,9 +130,9 @@ final public static function getListenerEvent(): ?string
/**
* @return array{string, string}
*/
private static function parseCacheString(string $cache): array
private static function parseCacheString(string $store): array
{
[$driver, $ident] = explode(':', $cache) + [1 => null];
[$driver, $ident] = explode(':', $store) + [1 => null];

if (is_null($ident)) {
[$driver, $ident] = [config('cache.default'), $driver];
Expand Down

0 comments on commit 6fe6c30

Please sign in to comment.