Skip to content

Commit 434cb09

Browse files
authored
Merge pull request #12 from vormkracht10/tests
Optimize code
2 parents d6e2b94 + 87bb683 commit 434cb09

File tree

4 files changed

+117
-29
lines changed

4 files changed

+117
-29
lines changed

src/CachedComponent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
use Illuminate\View\Component;
77

88
/**
9-
* @method string render()
9+
* @method mixed get(array $parameters = [], bool $update = false)
1010
*/
1111
abstract class CachedComponent extends Component
1212
{
1313
use CachesValue;
1414

1515
/** {@inheritdoc} */
16-
public function resolveView(): \Illuminate\Contracts\View\View|HtmlString|\Illuminate\Contracts\Support\Htmlable|\Closure|string
16+
public function resolveView()
1717
{
1818
if (
1919
$this->isUpdating ||

src/CachesValue.php

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Bus\Queueable;
66
use Illuminate\Console\Scheduling\CallbackEvent;
77
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Foundation\Bus\PendingDispatch;
89
use Illuminate\Support\Arr;
910
use Illuminate\Support\Facades\Blade;
1011
use Illuminate\Support\Facades\Cache;
@@ -44,18 +45,27 @@ trait CachesValue
4445
*/
4546
protected $expression = null;
4647

47-
/** @var array<string, mixed> */
48+
/**
49+
* The parameters this cache should be stored with.
50+
*
51+
* @var array<string, mixed>
52+
*/
4853
protected $parameters = [];
4954

50-
private bool $isUpdating = false;
55+
/**
56+
* Indicates whether this cache is currently updating or not.
57+
*
58+
* @var bool
59+
*/
60+
private $isUpdating = false;
5161

5262
/**
5363
* Update the cached value, this method expects an event if
5464
* the cacher is not static.
5565
*
56-
* @internal You shouldn't call this yourself.
66+
* @internal You shouldn't call this yourself, use the `CachesValue::update` method instead.
5767
*/
58-
final public function handle($event = null): mixed
68+
final public function handle($event = null): void
5969
{
6070
$this->isUpdating = true;
6171

@@ -68,16 +78,14 @@ final public function handle($event = null): mixed
6878
: $this->run($event);
6979

7080
if (is_null($value)) {
71-
return null;
81+
return;
7282
}
7383

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

7686
PermanentCacheUpdated::dispatch($this);
7787

7888
$this->isUpdating = false;
79-
80-
return $value;
8189
}
8290

8391
public function getParameters()
@@ -113,20 +121,17 @@ public function shouldBeUpdating(): bool
113121
/**
114122
* Manually force a static cache to update.
115123
*/
116-
final public static function update($parameters = []): mixed
124+
final public static function update($parameters = []): ?PendingDispatch
117125
{
118126
$instance = app()->make(static::class, $parameters);
119127

120-
if (
121-
app()->runningInConsole() &&
122-
is_subclass_of(static::class, ShouldQueue::class)
123-
) {
124-
dispatch($instance);
128+
if (! is_subclass_of(static::class, ShouldQueue::class)) {
129+
$instance->handle();
125130

126131
return null;
127132
}
128133

129-
return $instance->handle();
134+
return dispatch($instance);
130135
}
131136

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

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

150-
if (
151-
$update ||
152-
! $cache->has($cacheKey)
153-
) {
154-
return static::update($parameters ?? []);
155+
if ($update && ! $cache->has($cacheKey)) {
156+
static::update($parameters ?? [])->onConnection('sync');
155157
}
156158

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

162+
/**
163+
* Force an update of the cache and return the updated value.
164+
*
165+
* @return V|mixed
166+
*/
167+
final public static function updateAndGet($parameters = []): mixed
168+
{
169+
[$driver, $cacheKey] = self::store($parameters);
170+
171+
static::update($parameters)->onConnection('sync');
172+
173+
return Cache::driver($driver)->get($cacheKey);
174+
}
175+
160176
/**
161177
* Get the cached value this cacher provides.
162178
*
@@ -237,7 +253,7 @@ private static function parseCacheString($class, ?string $store, ?array $paramet
237253
}
238254

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

242258
if ($parameters) {
243259
$cacheKey .= ':'.http_build_query($parameters);

src/PermanentCache.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
class PermanentCache
1010
{
11-
public function __construct(protected SplObjectStorage $cachers, protected Application $app)
12-
{
11+
public function __construct(
12+
protected SplObjectStorage $cachers,
13+
protected Application $app,
14+
) {
15+
//
1316
}
1417

1518
/**
@@ -33,18 +36,18 @@ public function caches($registeredCaches): self
3336
$cacher = array_key_first($parameters);
3437
$parameters = array_shift($parameters);
3538
} else {
36-
$cacher = array_first($parameters);
39+
$cacher = \Arr::first($parameters);
3740
$parameters = [];
3841
}
3942
}
4043

41-
$cacher = $this->app->make($cacher, $parameters);
44+
$cacherInstance = $this->app->make($cacher, $parameters);
4245

43-
if ([] !== $events = $cacher::getListenerEvents()) {
44-
Event::listen($events, fn () => $cacher->update($parameters));
46+
if ([] !== $events = $cacherInstance->getListenerEvents()) {
47+
Event::listen($events, fn ($event) => $cacherInstance->handle($event));
4548
}
4649

47-
$this->cachers[$cacher] = $events;
50+
$this->cachers[$cacherInstance] = $events;
4851
}
4952
}
5053

tests/Unit/ReactiveCacheTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Cache;
4+
use Illuminate\Support\Facades\Event;
5+
use Vormkracht10\PermanentCache\Cached;
6+
use Vormkracht10\PermanentCache\Events\PermanentCacheUpdated;
7+
use Vormkracht10\PermanentCache\Events\PermanentCacheUpdating;
8+
use Vormkracht10\PermanentCache\Facades\PermanentCache;
9+
10+
beforeEach(function () {
11+
Cache::driver('array')->clear();
12+
(fn () => $this->cachers = new \SplObjectStorage)->call(app(\Vormkracht10\PermanentCache\PermanentCache::class));
13+
});
14+
15+
class TestEvent
16+
{
17+
}
18+
19+
class TestCache extends Cached
20+
{
21+
protected $store = 'array:test';
22+
23+
public function run(TestEvent $_): mixed
24+
{
25+
return 'it works!';
26+
}
27+
}
28+
29+
test('caches (listeners) get registered properly when using the PermanentCache facade', function () {
30+
$events = Event::fake([TestEvent::class]);
31+
32+
PermanentCache::caches([TestCache::class]);
33+
34+
$caches = PermanentCache::configuredCaches();
35+
36+
expect($caches)
37+
->count()->toBe(1)
38+
->current()->toBeInstanceOf(TestCache::class)
39+
->and($events)->hasListeners(TestEvent::class);
40+
});
41+
42+
test('a cache will get updated when an event it\'s listening to gets fired', function () {
43+
global $pass;
44+
$pass = false;
45+
46+
class T extends Cached
47+
{
48+
public function run(TestEvent $_)
49+
{
50+
global $pass;
51+
$pass = true;
52+
}
53+
}
54+
55+
Event::fakeExcept(TestEvent::class);
56+
PermanentCache::caches(T::class);
57+
event(new TestEvent);
58+
59+
expect($pass)->toBeTrue();
60+
unset($pass);
61+
});
62+
63+
test('a cache will dispatch the updating and updated events when it gets invoked', function () {
64+
Event::fakeExcept(TestEvent::class);
65+
Permanentcache::caches(TestCache::class);
66+
event(new TestEvent);
67+
Event::assertDispatchedTimes(PermanentCacheUpdating::class, times: 1);
68+
Event::assertDispatchedTimes(PermanentCacheUpdated::class, times : 1);
69+
});

0 commit comments

Comments
 (0)