Skip to content

Commit 749c7ac

Browse files
committed
Fix ->insert() not invalidating cache
1 parent d182c12 commit 749c7ac

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

src/CachedBuilder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ public function get($columns = ["*"])
7676
return $this->cachedValue(func_get_args(), $cacheKey);
7777
}
7878

79+
public function insert(array $values)
80+
{
81+
$this->checkCooldownAndFlushAfterPersiting($this->model);
82+
83+
return parent::insert($values);
84+
}
85+
7986
public function max($column)
8087
{
8188
if (! $this->isCachable()) {

src/Traits/Cachable.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ protected function makeCacheTags() : array
8686
return $tags;
8787
}
8888

89-
protected function getModelCacheCooldown(Model $instance)
89+
public function getModelCacheCooldown(Model $instance)
9090
{
9191
$cachePrefix = $this->getCachePrefix();
9292
$modelClassName = get_class($instance);
9393
[$cacheCooldown, $invalidatedAt, $savedAt] = $this
9494
->getCacheCooldownDetails($instance, $cachePrefix, $modelClassName);
9595

96-
if (! $cacheCooldown) {
96+
if (! $cacheCooldown || $cacheCooldown === 0) {
9797
return [null, null, null];
9898
}
9999

@@ -178,7 +178,6 @@ protected function setCacheCooldownSavedAtTimestamp(Model $instance)
178178

179179
public static function bootCachable()
180180
{
181-
// TODO: add for deleted,updated,etc?
182181
static::saved(function ($instance) {
183182
$instance->checkCooldownAndFlushAfterPersiting($instance);
184183
});

tests/Integration/CachedBuilderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,4 +889,24 @@ public function testPaginationIsCached()
889889
$this->assertEquals($cachedResults->toArray(), $authors->toArray());
890890
$this->assertEquals($liveResults->toArray(), $authors->toArray());
891891
}
892+
893+
public function testInsertInvalidatesCache()
894+
{
895+
$authors = (new Author)
896+
->get();
897+
898+
(new Author)
899+
->insert([
900+
'name' => 'Test Insert',
901+
'email' => 'none@noemail.com',
902+
]);
903+
$authorsAfterInsert = (new Author)
904+
->get();
905+
$uncachedAuthors = (new UncachedAuthor)
906+
->get();
907+
908+
$this->assertCount(10, $authors);
909+
$this->assertCount(11, $authorsAfterInsert);
910+
$this->assertCount(11, $uncachedAuthors);
911+
}
892912
}

tests/Integration/CachedModelTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,20 @@ public function testModelCacheDoesntInvalidateDuringCooldownPeriod()
118118
$this->assertCount(11, $uncachedAuthors);
119119
$this->assertCount(11, $authorsAfterCooldown);
120120
}
121+
122+
public function testModelCacheDoesInvalidateWhenNoCooldownPeriod()
123+
{
124+
$authors = (new Author)
125+
->get();
126+
127+
factory(Author::class, 1)->create();
128+
$authorsAfterCreate = (new Author)
129+
->get();
130+
$uncachedAuthors = (new UncachedAuthor)
131+
->get();
132+
133+
$this->assertCount(10, $authors);
134+
$this->assertCount(11, $authorsAfterCreate);
135+
$this->assertCount(11, $uncachedAuthors);
136+
}
121137
}

0 commit comments

Comments
 (0)