Skip to content

Commit 1f51b40

Browse files
committed
WIP
1 parent a81fec6 commit 1f51b40

File tree

5 files changed

+28
-33
lines changed

5 files changed

+28
-33
lines changed

src/CachedBuilder.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,6 @@ public function count($columns = ["*"])
3232
return $this->cachedValue(func_get_args(), $cacheKey);
3333
}
3434

35-
public function cursor()
36-
{
37-
if (! $this->isCachable()) {
38-
return collect(parent::cursor());
39-
}
40-
41-
$cacheKey = $this->makeCacheKey(["*"], null, "-cursor");
42-
43-
return $this->cachedValue(func_get_args(), $cacheKey);
44-
}
45-
4635
public function delete()
4736
{
4837
$this->cache($this->makeCacheTags())

src/Traits/Cachable.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ protected function addTagsWhenCalledFromCachedBuilder(array $tags) : array
3434
$usesCachableTrait = collect(class_uses($this))
3535
->contains("GeneaLabs\LaravelModelCaching\Traits\Cachable");
3636

37-
if (! $usesCachableTrait) {
38-
array_push($tags, str_slug(get_called_class()));
39-
}
40-
4137
return $tags;
4238
}
4339

@@ -59,10 +55,7 @@ public function flushCache(array $tags = [])
5955
[$cacheCooldown] = $this->getModelCacheCooldown($this);
6056

6157
if ($cacheCooldown) {
62-
$cachePrefix = "genealabs:laravel-model-caching:"
63-
. (config('laravel-model-caching.cache-prefix')
64-
? config('laravel-model-caching.cache-prefix', '') . ":"
65-
: "");
58+
$cachePrefix = $this->getCachePrefix();
6659
$modelClassName = get_class($this);
6760
$cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:saved-at";
6861

@@ -157,7 +150,7 @@ protected function checkCooldownAndRemoveIfExpired(Model $instance)
157150

158151
protected function checkCooldownAndFlushAfterPersiting(Model $instance)
159152
{
160-
[$cacheCooldown, $invalidatedAt, $savedAt] = $instance->getModelCacheCooldown($instance);
153+
[$cacheCooldown, $invalidatedAt] = $instance->getModelCacheCooldown($instance);
161154

162155
if (! $cacheCooldown) {
163156
$instance->flushCache();
@@ -167,19 +160,14 @@ protected function checkCooldownAndFlushAfterPersiting(Model $instance)
167160

168161
$this->setCacheCooldownSavedAtTimestamp($instance);
169162

170-
if ($savedAt > $invalidatedAt
171-
&& now()->diffInSeconds($invalidatedAt) >= $cacheCooldown
172-
) {
163+
if (now()->diffInSeconds($invalidatedAt) >= $cacheCooldown) {
173164
$instance->flushCache();
174165
}
175166
}
176167

177168
protected function setCacheCooldownSavedAtTimestamp(Model $instance)
178169
{
179-
$cachePrefix = "genealabs:laravel-model-caching:"
180-
. (config('laravel-model-caching.cache-prefix')
181-
? config('laravel-model-caching.cache-prefix', '') . ":"
182-
: "");
170+
$cachePrefix = $this->getCachePrefix();
183171
$modelClassName = get_class($instance);
184172
$cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:saved-at";
185173

@@ -235,10 +223,7 @@ public function scopeWithCacheCooldownSeconds(
235223
EloquentBuilder $query,
236224
int $seconds
237225
) : EloquentBuilder {
238-
$cachePrefix = "genealabs:laravel-model-caching:"
239-
. (config('laravel-model-caching.cache-prefix')
240-
? config('laravel-model-caching.cache-prefix', '') . ":"
241-
: "");
226+
$cachePrefix = $this->getCachePrefix();
242227
$modelClassName = get_class($this);
243228
$cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:seconds";
244229

tests/Integration/CachedBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ public function testWhereNotInResults()
786786
$this->assertTrue($cachedResults->diffKeys($books)->isEmpty());
787787
$this->assertTrue($liveResults->diffKeys($books)->isEmpty());
788788
}
789-
/** @group test */
789+
790790
public function testHashCollision()
791791
{
792792
$key1 = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook-id_notin_1_2');

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-
/** @group test */
100+
101101
public function testModelCacheDoesntInvalidateDuringCooldownPeriod()
102102
{
103103
$authors = (new Author)

tests/Integration/DisabledCachedBuilderTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,25 @@ public function testValueModelResultsIsNotCached()
308308
$this->assertEquals($author, $liveResult);
309309
$this->assertNull($cachedResult);
310310
}
311+
312+
public function testPaginationIsCached()
313+
{
314+
$authors = (new Author)
315+
->disableCache()
316+
->paginate(3);
317+
318+
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor-paginate_by_3_page_1');
319+
$tags = [
320+
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
321+
];
322+
323+
$cachedResults = $this->cache()
324+
->tags($tags)
325+
->get($key)['value'];
326+
$liveResults = (new UncachedAuthor)
327+
->paginate(3);
328+
329+
$this->assertNull($cachedResults);
330+
$this->assertEquals($liveResults->toArray(), $authors->toArray());
331+
}
311332
}

0 commit comments

Comments
 (0)