diff --git a/src/CachedComponent.php b/src/CachedComponent.php index 7fcac76..5293df1 100644 --- a/src/CachedComponent.php +++ b/src/CachedComponent.php @@ -5,9 +5,6 @@ use Illuminate\Support\HtmlString; use Illuminate\View\Component; -/** - * @method mixed get(array $parameters = [], bool $update = false) - */ abstract class CachedComponent extends Component { use CachesValue; diff --git a/src/CachesValue.php b/src/CachesValue.php index 453050e..055e25b 100644 --- a/src/CachesValue.php +++ b/src/CachesValue.php @@ -67,17 +67,19 @@ trait CachesValue */ final public function handle($event = null): void { - $this->updateNow($event); + $store = $this->store($this->getParameters()); + + $this->updateNow($store, $event); } /** * Update the cached value, and immediately return it. */ - final public function updateNow($event = null): mixed + final public function updateNow(array $store, $event = null): mixed { $this->isUpdating = true; - [$driver, $cacheKey] = $this->store($this->getParameters()); + [$driver, $cacheKey] = $store; PermanentCacheUpdating::dispatch($this); @@ -122,7 +124,11 @@ private static function store($parameters): array } /** - * Manually force a static cache to update. + * Manually force a potentially queueable static cache to update. + * + * If you want to update the cache synchronously, consider using + * the `CachesValue::updateAndGet` method instead, as it also + * returns to you the updated value without hitting the cache. */ final public static function update($parameters = []): ?PendingDispatch { @@ -156,26 +162,24 @@ final public static function get($default = null, bool $update = false): mixed $cache = Cache::driver($driver); if ($update && ! $cache->has($cacheKey)) { - $instance = app()->make(static::class, $parameters ?? []); - - return $instance->updateNow(); + return static::updateAndGet($parameters ?? []); } return $cache->get($cacheKey, $default); } /** - * Force an update of the cache and return the updated value. + * Force a synchronous update of the cache and return the updated value. * * @return V|mixed */ final public static function updateAndGet($parameters = []): mixed { - [$driver, $cacheKey] = self::store($parameters); + $store = self::store($parameters); - static::update($parameters)?->onConnection('sync'); + $instance = app()->make(static::class, $parameters ?? []); - return Cache::driver($driver)->get($cacheKey); + return $instance->updateNow($store); } /** diff --git a/tests/Unit/ReactiveCacheTest.php b/tests/Unit/ReactiveCacheTest.php index e7e8b47..35df051 100644 --- a/tests/Unit/ReactiveCacheTest.php +++ b/tests/Unit/ReactiveCacheTest.php @@ -14,6 +14,7 @@ class TestEvent { + // } class TestCache extends Cached @@ -65,5 +66,5 @@ public function run(TestEvent $_) Permanentcache::caches(TestCache::class); event(new TestEvent); Event::assertDispatchedTimes(PermanentCacheUpdating::class, times: 1); - Event::assertDispatchedTimes(PermanentCacheUpdated::class, times : 1); + Event::assertDispatchedTimes(PermanentCacheUpdated::class, times: 1); });