Skip to content

Commit fd342aa

Browse files
committed
fix: ensure debounce logic respects entity callability during cache initialization and testing
1 parent 22a1fef commit fd342aa

File tree

3 files changed

+88
-9
lines changed

3 files changed

+88
-9
lines changed

src/Cache.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,17 @@ public function refresh(CacheEvent $event): void
6060

6161
foreach ($this->model::cacheEntities() as $entity) {
6262
if ($entity->debounce) {
63-
$this->initCache($entity, $entity->getTtl());
64-
65-
RefreshDebouncer::dispatch(
66-
model: $this->model,
67-
name: $entity->name,
68-
queueConnection: $entity->queueConnection,
69-
queueName: $entity->queueName,
70-
wait: $entity->debounceWaitTime
71-
);
63+
if ($this->entityIsCallable($entity, $event)) {
64+
$this->initCache($entity, $entity->getTtl());
65+
66+
RefreshDebouncer::dispatch(
67+
model: $this->model,
68+
name: $entity->name,
69+
queueConnection: $entity->queueConnection,
70+
queueName: $entity->queueName,
71+
wait: $entity->debounceWaitTime
72+
);
73+
}
7274
}
7375
else if ($entity->isQueueable) {
7476
if ($this->entityIsCallable($entity, $event)) {

tests/Feature/DebounceCacheTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Illuminate\Support\Facades\Artisan;
44
use Illuminate\Support\Facades\Bus;
55
use Illuminate\Support\Facades\Queue;
6+
use Mostafaznv\LaraCache\DTOs\CacheData;
67
use Mostafaznv\LaraCache\Enums\CacheStatus;
78
use Mostafaznv\LaraCache\Jobs\DebounceRefresh;
89
use Mostafaznv\LaraCache\Jobs\UpdateLaraCacheModelsList;
@@ -201,3 +202,69 @@
201202
->and($cache->value)
202203
->toBeInstanceOf(DebounceTestModel::class);
203204
});
205+
206+
it('will respect cache creation rules', function () {
207+
# prepare
208+
$worksOnCreation = 'debounce-test-model.latest.debounce';
209+
$doesNotWorkOnCreation = 'debounce-test-model.latest.no-creation';
210+
211+
createDebounceModel();
212+
213+
214+
# test 1 - after creation, before running the queue
215+
$cache = Cache::get($worksOnCreation);
216+
expect($cache)
217+
->toBeInstanceOf(CacheData::class)
218+
->and($cache->status)
219+
->toBe(CacheStatus::CREATING);
220+
221+
$cache = Cache::get($doesNotWorkOnCreation);
222+
expect($cache)->toBeNull();
223+
224+
225+
# waiting for debouncing
226+
testTime()->freeze(
227+
now()->addSeconds($this->waitTime - 1)
228+
);
229+
230+
Artisan::call('queue:work --once --sleep=0');
231+
232+
233+
234+
# test 2 - after running the queue; before debouncing time is over
235+
$cache = Cache::get($worksOnCreation);
236+
expect($cache)
237+
->toBeInstanceOf(CacheData::class)
238+
->and($cache->status)
239+
->toBe(CacheStatus::CREATING);
240+
241+
$cache = Cache::get($doesNotWorkOnCreation);
242+
expect($cache)->toBeNull();
243+
244+
245+
246+
# waiting for debouncing
247+
testTime()->freeze(
248+
now()->addSeconds($this->waitTime + 1)
249+
);
250+
251+
252+
# test 3 - after debouncing time is over
253+
Artisan::call('queue:work --once --sleep=0');
254+
255+
$cache = Cache::get($worksOnCreation);
256+
expect($cache)
257+
->toBeInstanceOf(CacheData::class)
258+
->and($cache->status)
259+
->toBe(CacheStatus::CREATED);
260+
261+
$cache = Cache::get($doesNotWorkOnCreation);
262+
expect($cache)->toBeNull();
263+
264+
$cache = DebounceTestModel::cache()->get('latest.no-creation', true);
265+
expect($cache)
266+
->toBeInstanceOf(CacheData::class)
267+
->and($cache->status)
268+
->toBe(CacheStatus::CREATED);
269+
270+
});

tests/TestSupport/TestModels/DebounceTestModel.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ public static function cacheEntities(): array
2929
CacheEntity::make('latest')
3030
->validForRestOfDay()
3131
->setDefault(-1)
32+
->cache(function() {
33+
return DebounceTestModel::query()->latest()->first();
34+
}),
35+
36+
CacheEntity::make('latest.no-creation')
37+
->validForRestOfDay()
38+
->debounce()
39+
->refreshAfterCreate(false)
40+
->refreshAfterUpdate(false)
41+
->setDefault(-1)
3242
->cache(function() {
3343
return DebounceTestModel::query()->latest()->first();
3444
})

0 commit comments

Comments
 (0)