@@ -129,28 +129,28 @@ protected function checkCooldownAndRemoveIfExpired(Model $instance)
129129 {
130130 [$ cacheCooldown , $ invalidatedAt ] = $ this ->getModelCacheCooldown ($ instance );
131131
132- if (! $ cacheCooldown ) {
132+ if (! $ cacheCooldown
133+ || now ()->diffInSeconds ($ invalidatedAt ) < $ cacheCooldown
134+ ) {
133135 return ;
134136 }
135137
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 ();
154154 }
155155
156156 protected function checkCooldownAndFlushAfterPersiting (Model $ instance )
@@ -163,19 +163,7 @@ protected function checkCooldownAndFlushAfterPersiting(Model $instance)
163163 return ;
164164 }
165165
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 );
179167
180168 if ($ savedAt > $ invalidatedAt
181169 && now ()->diffInSeconds ($ invalidatedAt ) >= $ cacheCooldown
@@ -184,6 +172,21 @@ protected function checkCooldownAndFlushAfterPersiting(Model $instance)
184172 }
185173 }
186174
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+
187190 public static function bootCachable ()
188191 {
189192 // TODO: add for deleted,updated,etc?
0 commit comments