Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
david-d-h committed May 16, 2024
1 parent bd6d170 commit c3a6547
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
3 changes: 0 additions & 3 deletions src/CachedComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 15 additions & 11 deletions src/CachesValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/ReactiveCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

class TestEvent
{
//
}

class TestCache extends Cached
Expand Down Expand Up @@ -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);
});

0 comments on commit c3a6547

Please sign in to comment.