File tree Expand file tree Collapse file tree 4 files changed +45
-3
lines changed Expand file tree Collapse file tree 4 files changed +45
-3
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,13 @@ public function get($columns = ["*"])
76
76
return $ this ->cachedValue (func_get_args (), $ cacheKey );
77
77
}
78
78
79
+ public function insert (array $ values )
80
+ {
81
+ $ this ->checkCooldownAndFlushAfterPersiting ($ this ->model );
82
+
83
+ return parent ::insert ($ values );
84
+ }
85
+
79
86
public function max ($ column )
80
87
{
81
88
if (! $ this ->isCachable ()) {
Original file line number Diff line number Diff line change @@ -86,14 +86,14 @@ protected function makeCacheTags() : array
86
86
return $ tags ;
87
87
}
88
88
89
- protected function getModelCacheCooldown (Model $ instance )
89
+ public function getModelCacheCooldown (Model $ instance )
90
90
{
91
91
$ cachePrefix = $ this ->getCachePrefix ();
92
92
$ modelClassName = get_class ($ instance );
93
93
[$ cacheCooldown , $ invalidatedAt , $ savedAt ] = $ this
94
94
->getCacheCooldownDetails ($ instance , $ cachePrefix , $ modelClassName );
95
95
96
- if (! $ cacheCooldown ) {
96
+ if (! $ cacheCooldown || $ cacheCooldown === 0 ) {
97
97
return [null , null , null ];
98
98
}
99
99
@@ -178,7 +178,6 @@ protected function setCacheCooldownSavedAtTimestamp(Model $instance)
178
178
179
179
public static function bootCachable ()
180
180
{
181
- // TODO: add for deleted,updated,etc?
182
181
static ::saved (function ($ instance ) {
183
182
$ instance ->checkCooldownAndFlushAfterPersiting ($ instance );
184
183
});
Original file line number Diff line number Diff line change @@ -889,4 +889,24 @@ public function testPaginationIsCached()
889
889
$ this ->assertEquals ($ cachedResults ->toArray (), $ authors ->toArray ());
890
890
$ this ->assertEquals ($ liveResults ->toArray (), $ authors ->toArray ());
891
891
}
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
+ }
892
912
}
Original file line number Diff line number Diff line change @@ -118,4 +118,20 @@ public function testModelCacheDoesntInvalidateDuringCooldownPeriod()
118
118
$ this ->assertCount (11 , $ uncachedAuthors );
119
119
$ this ->assertCount (11 , $ authorsAfterCooldown );
120
120
}
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
+ }
121
137
}
You can’t perform that action at this time.
0 commit comments