Skip to content

Commit a156291

Browse files
committed
Added softdeletes parsing
1 parent e200e03 commit a156291

File tree

3 files changed

+81
-31
lines changed

3 files changed

+81
-31
lines changed

src/CacheKey.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ class CacheKey
99
{
1010
use CachePrefixing;
1111

12+
protected $currentBinding = 0;
1213
protected $eagerLoad;
14+
protected $macroKey;
1315
protected $model;
1416
protected $query;
15-
protected $currentBinding = 0;
1617

1718
public function __construct(
1819
array $eagerLoad,
1920
$model,
20-
$query
21+
$query,
22+
$macroKey
2123
) {
2224
$this->eagerLoad = $eagerLoad;
25+
$this->macroKey = $macroKey;
2326
$this->model = $model;
2427
$this->query = $query;
2528
}
@@ -40,6 +43,7 @@ public function make(
4043
$key .= $this->getOffsetClause();
4144
$key .= $this->getLimitClause();
4245
$key .= $keyDifferentiator;
46+
$key .= $this->macroKey;
4347

4448
return $key;
4549
}

src/Traits/Caching.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ trait Caching
1414
{
1515
protected $isCachable = true;
1616
protected $scopesAreApplied = false;
17+
protected $macroKey = "";
18+
19+
public function __call($method, $parameters)
20+
{
21+
$result = parent::__call($method, $parameters);
22+
23+
if (isset($this->localMacros[$method])) {
24+
$this->macroKey .= "-{$method}";
25+
26+
if ($parameters) {
27+
$this->macroKey .= implode("_", $parameters);
28+
}
29+
}
30+
31+
return $result;
32+
}
1733

1834
protected function applyScopesToInstance()
1935
{
@@ -28,8 +44,6 @@ protected function applyScopesToInstance()
2844
continue;
2945
}
3046

31-
$this->scopesAreApplied = true;
32-
3347
$this->callScope(function () use ($scope) {
3448
if ($scope instanceof Closure) {
3549
$scope($this);
@@ -42,6 +56,8 @@ protected function applyScopesToInstance()
4256
}
4357
});
4458
}
59+
60+
$this->scopesAreApplied = true;
4561
}
4662

4763
public function cache(array $tags = [])
@@ -132,7 +148,7 @@ protected function makeCacheKey(
132148
$query = $this->query->getQuery();
133149
}
134150

135-
return (new CacheKey($eagerLoad, $model, $query))
151+
return (new CacheKey($eagerLoad, $model, $query, $this->macroKey))
136152
->make($columns, $idColumn, $keyDifferentiator);
137153
}
138154

tests/Integration/CachedBuilder/SoftDeletesTest.php

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ class SoftDeletesTest extends IntegrationTestCase
88
{
99
public function testWithTrashedIsCached()
1010
{
11-
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.id_=_1-first");
12-
$tags = [
13-
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
14-
];
1511
$author = (new UncachedAuthor)
1612
->first();
1713
$author->delete();
14+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-find_1-withTrashed");
15+
$tags = [
16+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
17+
];
1818

1919
$deletedAuthor = (new Author)
2020
->withTrashed()
@@ -31,26 +31,56 @@ public function testWithTrashedIsCached()
3131
$this->assertEquals($cachedResults->toArray(), $deletedUncachedAuthor->toArray());
3232
}
3333

34-
// public function testWithoutTrashedIsCached()
35-
// {
36-
// $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_1-first");
37-
// $tags = [
38-
// "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
39-
// ];
40-
// $author = (new UncachedAuthor)
41-
// ->first();
42-
// $author->delete();
43-
44-
// $deletedAuthor = (new Author)
45-
// ->first($author->id);
46-
// $cachedResults = $this
47-
// ->cache()
48-
// ->tags($tags)
49-
// ->get($key)['value'];
50-
// $deletedUncachedAuthor = (new UncachedAuthor)
51-
// ->first($author->id);
52-
53-
// $this->assertEquals($cachedResults->toArray(), $deletedAuthor->toArray());
54-
// $this->assertEquals($cachedResults->toArray(), $deletedUncachedAuthor->toArray());
55-
// }
34+
public function testWithoutTrashedIsCached()
35+
{
36+
$author = (new UncachedAuthor)
37+
->first();
38+
$author->delete();
39+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-find_{$author->id}-withoutTrashed");
40+
$tags = [
41+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
42+
];
43+
44+
$result = (new Author)
45+
->withoutTrashed()
46+
->find($author->id);
47+
$cachedResult = $this
48+
->cache()
49+
->tags($tags)
50+
->get($key)['value'];
51+
$uncachedResult = (new UncachedAuthor)
52+
->withoutTrashed()
53+
->find($author->id);
54+
55+
$this->assertEquals($uncachedResult, $result);
56+
$this->assertEquals($uncachedResult, $cachedResult);
57+
$this->assertNull($result);
58+
$this->assertNull($cachedResult);
59+
$this->assertNull($uncachedResult);
60+
}
61+
62+
public function testonlyTrashedIsCached()
63+
{
64+
$author = (new UncachedAuthor)
65+
->first();
66+
$author->delete();
67+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_notnull-find_{$author->id}-onlyTrashed");
68+
$tags = [
69+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
70+
];
71+
72+
$deletedAuthor = (new Author)
73+
->onlyTrashed()
74+
->find($author->id);
75+
$cachedResults = $this
76+
->cache()
77+
->tags($tags)
78+
->get($key)['value'];
79+
$deletedUncachedAuthor = (new UncachedAuthor)
80+
->onlyTrashed()
81+
->find($author->id);
82+
83+
$this->assertEquals($cachedResults->toArray(), $deletedAuthor->toArray());
84+
$this->assertEquals($cachedResults->toArray(), $deletedUncachedAuthor->toArray());
85+
}
5686
}

0 commit comments

Comments
 (0)