@@ -129,28 +129,28 @@ protected function checkCooldownAndRemoveIfExpired(Model $instance)
129
129
{
130
130
[$ cacheCooldown , $ invalidatedAt ] = $ this ->getModelCacheCooldown ($ instance );
131
131
132
- if (! $ cacheCooldown ) {
132
+ if (! $ cacheCooldown
133
+ || now ()->diffInSeconds ($ invalidatedAt ) < $ cacheCooldown
134
+ ) {
133
135
return ;
134
136
}
135
137
136
- if (now ()->diffInSeconds ($ invalidatedAt ) >= $ cacheCooldown ) {
137
- $ cachePrefix = "genealabs:laravel-model-caching: "
138
- . (config ('laravel-model-caching.cache-prefix ' )
139
- ? config ('laravel-model-caching.cache-prefix ' , '' ) . ": "
140
- : "" );
141
- $ modelClassName = get_class ($ instance );
142
-
143
- $ instance
144
- ->cache ()
145
- ->forget ("{$ cachePrefix }: {$ modelClassName }-cooldown:invalidated-at " );
146
- $ instance
147
- ->cache ()
148
- ->forget ("{$ cachePrefix }: {$ modelClassName }-cooldown:invalidated-at " );
149
- $ instance
150
- ->cache ()
151
- ->forget ("{$ cachePrefix }: {$ modelClassName }-cooldown:saved-at " );
152
- $ instance ->flushCache ();
153
- }
138
+ $ cachePrefix = "genealabs:laravel-model-caching: "
139
+ . (config ('laravel-model-caching.cache-prefix ' )
140
+ ? config ('laravel-model-caching.cache-prefix ' , '' ) . ": "
141
+ : "" );
142
+ $ modelClassName = get_class ($ instance );
143
+
144
+ $ instance
145
+ ->cache ()
146
+ ->forget ("{$ cachePrefix }: {$ modelClassName }-cooldown:invalidated-at " );
147
+ $ instance
148
+ ->cache ()
149
+ ->forget ("{$ cachePrefix }: {$ modelClassName }-cooldown:invalidated-at " );
150
+ $ instance
151
+ ->cache ()
152
+ ->forget ("{$ cachePrefix }: {$ modelClassName }-cooldown:saved-at " );
153
+ $ instance ->flushCache ();
154
154
}
155
155
156
156
protected function checkCooldownAndFlushAfterPersiting (Model $ instance )
@@ -163,19 +163,7 @@ protected function checkCooldownAndFlushAfterPersiting(Model $instance)
163
163
return ;
164
164
}
165
165
166
- if ($ cacheCooldown ) {
167
- $ cachePrefix = "genealabs:laravel-model-caching: "
168
- . (config ('laravel-model-caching.cache-prefix ' )
169
- ? config ('laravel-model-caching.cache-prefix ' , '' ) . ": "
170
- : "" );
171
- $ modelClassName = get_class ($ instance );
172
- $ cacheKey = "{$ cachePrefix }: {$ modelClassName }-cooldown:saved-at " ;
173
-
174
- $ instance ->cache ()
175
- ->rememberForever ($ cacheKey , function () {
176
- return now ();
177
- });
178
- }
166
+ $ this ->setCacheCooldownSavedAtTimestamp ($ instance );
179
167
180
168
if ($ savedAt > $ invalidatedAt
181
169
&& now ()->diffInSeconds ($ invalidatedAt ) >= $ cacheCooldown
@@ -184,6 +172,21 @@ protected function checkCooldownAndFlushAfterPersiting(Model $instance)
184
172
}
185
173
}
186
174
175
+ protected function setCacheCooldownSavedAtTimestamp (Model $ instance )
176
+ {
177
+ $ cachePrefix = "genealabs:laravel-model-caching: "
178
+ . (config ('laravel-model-caching.cache-prefix ' )
179
+ ? config ('laravel-model-caching.cache-prefix ' , '' ) . ": "
180
+ : "" );
181
+ $ modelClassName = get_class ($ instance );
182
+ $ cacheKey = "{$ cachePrefix }: {$ modelClassName }-cooldown:saved-at " ;
183
+
184
+ $ instance ->cache ()
185
+ ->rememberForever ($ cacheKey , function () {
186
+ return now ();
187
+ });
188
+ }
189
+
187
190
public static function bootCachable ()
188
191
{
189
192
// TODO: add for deleted,updated,etc?
0 commit comments