Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
markvaneijk committed Apr 28, 2024
1 parent 434cb09 commit c4c891d
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 43 deletions.
61 changes: 24 additions & 37 deletions src/CachesValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Bus\Queueable;
use Illuminate\Console\Scheduling\CallbackEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\PendingDispatch;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Cache;
Expand Down Expand Up @@ -45,27 +44,18 @@ trait CachesValue
*/
protected $expression = null;

/**
* The parameters this cache should be stored with.
*
* @var array<string, mixed>
*/
/** @var array<string, mixed> */
protected $parameters = [];

/**
* Indicates whether this cache is currently updating or not.
*
* @var bool
*/
private $isUpdating = false;
private bool $isUpdating = false;

/**
* Update the cached value, this method expects an event if
* the cacher is not static.
*
* @internal You shouldn't call this yourself, use the `CachesValue::update` method instead.
* @internal You shouldn't call this yourself.
*/
final public function handle($event = null): void
final public function handle($event = null): mixed
{
$this->isUpdating = true;

Expand All @@ -78,14 +68,16 @@ final public function handle($event = null): void
: $this->run($event);

if (is_null($value)) {
return;
return null;
}

Cache::driver($driver)->forever($cacheKey, $value);

PermanentCacheUpdated::dispatch($this);

$this->isUpdating = false;

return $value;
}

public function getParameters()
Expand Down Expand Up @@ -115,23 +107,29 @@ private static function store($parameters): array

public function shouldBeUpdating(): bool
{
return app()->runningInConsole();
return (
!app()->environment('testing') &&
app()->runningInConsole()
);
}

/**
* Manually force a static cache to update.
*/
final public static function update($parameters = []): ?PendingDispatch
final public static function update($parameters = []): mixed
{
$instance = app()->make(static::class, $parameters);

if (! is_subclass_of(static::class, ShouldQueue::class)) {
$instance->handle();
if (
app()->runningInConsole() &&
is_subclass_of(static::class, ShouldQueue::class)
) {
dispatch($instance);

return null;
}

return dispatch($instance);
return $instance->handle();
}

/**
Expand All @@ -152,27 +150,16 @@ final public static function get($default = null, bool $update = false): mixed

$cache = Cache::driver($driver);

if ($update && ! $cache->has($cacheKey)) {
static::update($parameters ?? [])->onConnection('sync');
if (
$update ||
! $cache->has($cacheKey)
) {
return static::update($parameters ?? []);
}

return $cache->get($cacheKey, $default);
}

/**
* Force an update of the cache and return the updated value.
*
* @return V|mixed
*/
final public static function updateAndGet($parameters = []): mixed
{
[$driver, $cacheKey] = self::store($parameters);

static::update($parameters)->onConnection('sync');

return Cache::driver($driver)->get($cacheKey);
}

/**
* Get the cached value this cacher provides.
*
Expand Down Expand Up @@ -253,7 +240,7 @@ private static function parseCacheString($class, ?string $store, ?array $paramet
}

$cacheDriver ??= config('cache.default');
$cacheKey ??= preg_replace('/[^A-Za-z0-9]+/', '_', strtolower(\Str::snake($class)));
$cacheKey ??= preg_replace('/[^A-Za-z0-9]+/', '_', strtolower(snake_case($class)));

if ($parameters) {
$cacheKey .= ':'.http_build_query($parameters);
Expand Down
5 changes: 0 additions & 5 deletions tests/ExampleTest.php

This file was deleted.

19 changes: 19 additions & 0 deletions tests/Unit/CachedComponent/CachedComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Vormkracht10\PermanentCache\Scheduled;

class CachedComponent extends \Vormkracht10\PermanentCache\CachedComponent
{
protected $store = 'file:unique-cache-key';

public function render(): string
{
sleep(3);

return <<<HTML
<div class="alert alert-danger">
This is a cached component!
</div>
HTML;
}
}
25 changes: 25 additions & 0 deletions tests/Unit/CachedComponent/CachedComponentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Illuminate\Support\Facades\Blade;

require_once 'tests/Unit/CachedComponent/CachedComponent.php';
require_once 'tests/Unit/CachedComponent/ScheduledCachedComponent.php';

beforeEach(function () {
Cache::driver('file')->clear();
(fn () => $this->cachers = new \SplObjectStorage)->call(app(\Vormkracht10\PermanentCache\PermanentCache::class));
});

test('test cached component is cached second time', function () {
$time = microtime(true);

Blade::renderComponent(new CachedComponent);

$this->assertGreaterThanOrEqual(3, microtime(true) - $time);

$time = microtime(true);

Blade::renderComponent(new CachedComponent);

$this->assertLessThan(3, microtime(true) - $time);
});
24 changes: 24 additions & 0 deletions tests/Unit/CachedComponent/ScheduledCachedComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Vormkracht10\PermanentCache\Scheduled;

class ScheduledCachedComponent extends \Vormkracht10\PermanentCache\CachedComponent implements Scheduled
{
protected $store = 'file:unique-cache-key';

public function render(): string
{
sleep(10);

return <<<HTML
<div class="alert alert-danger">
This is a cached component!
</div>
HTML;
}

public static function schedule($callback)
{
return $callback->everyMinute();
}
}
24 changes: 24 additions & 0 deletions tests/Unit/CachedComponent/ScheduledCachedComponentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Support\Facades\Blade;
use Vormkracht10\PermanentCache\Facades\PermanentCache;

require_once 'tests/Unit/CachedComponent/CachedComponent.php';
require_once 'tests/Unit/CachedComponent/ScheduledCachedComponent.php';

beforeEach(function () {
Cache::driver('file')->clear();
(fn () => $this->cachers = new \SplObjectStorage)->call(app(\Vormkracht10\PermanentCache\PermanentCache::class));
});

test('test scheduled cached component gets scheduled', function () {
PermanentCache::caches([
ScheduledCachedComponent::class
]);

$events = collect(app(\Illuminate\Console\Scheduling\Schedule::class)->events())
->filter(fn ($schedule) => $schedule->description === 'ScheduledCachedComponent');

expect($events)->toHaveCount(1);
expect($events->first()->expression)->toBe('* * * * *');
});
2 changes: 1 addition & 1 deletion tests/Unit/ReactiveCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Vormkracht10\PermanentCache\Facades\PermanentCache;

beforeEach(function () {
Cache::driver('array')->clear();
Cache::driver('file')->clear();
(fn () => $this->cachers = new \SplObjectStorage)->call(app(\Vormkracht10\PermanentCache\PermanentCache::class));
});

Expand Down

0 comments on commit c4c891d

Please sign in to comment.