Skip to content

Commit c7d0c7f

Browse files
committed
WIP
1 parent f210e5a commit c7d0c7f

File tree

3 files changed

+54
-39
lines changed

3 files changed

+54
-39
lines changed

src/Console/Commands/Flush.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,25 @@ public function handle()
1212
$option = $this->option('model');
1313

1414
if (! $option) {
15-
cache()
16-
->store(config('laravel-model-caching.store'))
17-
->flush();
15+
return $this->flushEntireCache();
16+
}
1817

19-
$this->info("✔︎ Entire model cache has been flushed.");
18+
return $this->flushModelCache($option);
19+
}
2020

21-
return 0;
22-
}
21+
protected function flushEntireCache() : int
22+
{
23+
cache()
24+
->store(config('laravel-model-caching.store'))
25+
->flush();
26+
27+
$this->info("✔︎ Entire model cache has been flushed.");
2328

29+
return 0;
30+
}
31+
32+
protected function flushModelCache(string $option) : int
33+
{
2434
$model = new $option;
2535
$usesCachableTrait = collect(class_uses($model))
2636
->contains("GeneaLabs\LaravelModelCaching\Traits\Cachable");
@@ -34,5 +44,7 @@ public function handle()
3444

3545
$model->flushCache();
3646
$this->info("✔︎ Cache for model '{$option}' has been flushed.");
47+
48+
return 0;
3749
}
3850
}

src/Traits/Cachable.php

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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?

tests/Integration/CachedModelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function testWhereHasIsBeingCached()
9797
$this->assertEquals(1, $books->first()->author->id);
9898
$this->assertEquals(1, $cachedResults->first()->author->id);
9999
}
100-
100+
/** @group test */
101101
public function testModelCacheDoesntInvalidateDuringCooldownPeriod()
102102
{
103103
$authors = (new Author)

0 commit comments

Comments
 (0)