Skip to content

Commit 1e1ca06

Browse files
committed
Added functionality to flush specific cache when model is deleted
1 parent 140e3c7 commit 1e1ca06

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/CachedBuilder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public function cursor()
3131
});
3232
}
3333

34+
public function delete()
35+
{
36+
$this->cache($this->makeCacheTags())
37+
->flush();
38+
39+
return parent::delete();
40+
}
41+
3442
/**
3543
* @SuppressWarnings(PHPMD.ShortVariable)
3644
*/

tests/Unit/CachedBuilderTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,4 +660,27 @@ public function testRawOrderByWithoutColumnReference()
660660
$this->assertTrue($cachedResults->diffAssoc($authors)->isEmpty());
661661
$this->assertTrue($liveResults->diffAssoc($authors)->isEmpty());
662662
}
663+
664+
public function testDelete()
665+
{
666+
$author = (new Author)
667+
->first();
668+
$liveResult = (new UncachedAuthor)
669+
->first();
670+
$authorId = $author->id;
671+
$liveResultId = $liveResult->id;
672+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';
673+
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
674+
675+
$author->delete();
676+
$liveResult->delete();
677+
$cachedResult = cache()
678+
->tags($tags)
679+
->get($key);
680+
$deletedAuthor = (new Author)->find($authorId);
681+
682+
$this->assertEquals($liveResultId, $authorId);
683+
$this->assertNull($cachedResult);
684+
$this->assertNull($deletedAuthor);
685+
}
663686
}

0 commit comments

Comments
 (0)