Skip to content

Commit acd1c39

Browse files
committed
Fixed caching of get() with string.
Fixes #281
1 parent 536316a commit acd1c39

File tree

9 files changed

+177
-7
lines changed

9 files changed

+177
-7
lines changed

phpunit.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
<env name="SESSION_DRIVER" value="array"/>
3535
<env name="QUEUE_DRIVER" value="sync"/>
3636
<env name="DB_CONNECTION" value="sqlite"/>
37-
<env name="DB_HOST" value="192.168.10.10"/>
37+
<env name="DB_HOST" value="127.0.0.1"/>
3838
<env name="DB_DATABASE" value="testing"/>
39-
<env name="DB_USERNAME" value="homestead"/>
40-
<env name="DB_PASSWORD" value="secret"/>
39+
<env name="DB_USERNAME" value="mike"/>
40+
<env name="DB_PASSWORD" value=""/>
4141
</php>
4242
</phpunit>

src/Traits/Buildable.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function find($id, $columns = ["*"])
5959
$preStr = is_array($id)
6060
? 'find_list'
6161
: 'find';
62+
$columns = collect($columns)->toArray();
6263
$cacheKey = $this->makeCacheKey($columns, null, "-{$preStr}_{$idKey}");
6364

6465
return $this->cachedValue(func_get_args(), $cacheKey);
@@ -70,6 +71,7 @@ public function first($columns = ["*"])
7071
return parent::first($columns);
7172
}
7273

74+
$columns = collect($columns)->toArray();
7375
$cacheKey = $this->makeCacheKey($columns, null, "-first");
7476

7577
return $this->cachedValue(func_get_args(), $cacheKey);
@@ -89,6 +91,7 @@ public function get($columns = ["*"])
8991
return parent::get($columns);
9092
}
9193

94+
$columns = collect($columns)->toArray();
9295
$cacheKey = $this->makeCacheKey($columns);
9396

9497
return $this->cachedValue(func_get_args(), $cacheKey);
@@ -157,6 +160,7 @@ public function paginate(
157160
if (is_array($page)) {
158161
$page = $this->recursiveImplodeWithKey($page);
159162
}
163+
$columns = collect($columns)->toArray();
160164
$cacheKey = $this->makeCacheKey($columns, null, "-paginate_by_{$perPage}_{$pageName}_{$page}");
161165

162166
return $this->cachedValue(func_get_args(), $cacheKey);

tests/EnvironmentSetup.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ protected function getEnvironmentSetUp($app)
1212
"foreign_key_constraints" => false,
1313
]);
1414
$app['config']->set('database.redis.cache', [
15-
'host' => env('REDIS_HOST', '192.168.10.10'),
15+
'host' => env('REDIS_HOST', '127.0.0.1'),
1616
]);
1717
$app['config']->set('database.redis.default', [
18-
'host' => env('REDIS_HOST', '192.168.10.10'),
18+
'host' => env('REDIS_HOST', '127.0.0.1'),
1919
]);
2020
$app['config']->set('database.redis.model-cache', [
21-
'host' => env('REDIS_HOST', '192.168.10.10'),
21+
'host' => env('REDIS_HOST', '127.0.0.1'),
2222
'password' => env('REDIS_PASSWORD', null),
2323
'port' => env('REDIS_PORT', 6379),
2424
'database' => 1,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4+
use Illuminate\Database\Eloquent\Model;
5+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
6+
7+
class StoreWithUncachedBooks extends Model
8+
{
9+
use Cachable;
10+
11+
protected $fillable = [
12+
'address',
13+
'name',
14+
];
15+
protected $table = "stores";
16+
17+
public function books() : BelongsToMany
18+
{
19+
return $this->belongsToMany(UncachedBook::class, "book_store", "store_id", "book_id");
20+
}
21+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use Illuminate\Database\Eloquent\Model;
4+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
5+
use Illuminate\Database\Eloquent\Relations\MorphMany;
6+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7+
use Illuminate\Database\Eloquent\Relations\MorphOne;
8+
9+
class UncachedBookWithStores extends Model
10+
{
11+
protected $casts = [
12+
'price' => 'float',
13+
];
14+
protected $dates = [
15+
'published_at',
16+
];
17+
protected $fillable = [
18+
'description',
19+
'published_at',
20+
'title',
21+
];
22+
protected $table = 'books';
23+
24+
public function author() : BelongsTo
25+
{
26+
return $this->belongsTo(UncachedAuthor::class);
27+
}
28+
29+
public function comments() : MorphMany
30+
{
31+
return $this->morphMany(Comment::class, "commentable");
32+
}
33+
34+
public function image() : MorphOne
35+
{
36+
return $this->morphOne(Image::class, "imagable");
37+
}
38+
39+
public function publisher() : BelongsTo
40+
{
41+
return $this->belongsTo(UncachedPublisher::class);
42+
}
43+
44+
public function stores() : BelongsToMany
45+
{
46+
return $this->belongsToMany(Store::class, "book_store", "book_id", "store_id");
47+
}
48+
}

tests/Integration/CachedBuilder/BelongsToManyTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
55
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook;
66
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
7+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBookWithCachedStores;
8+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\StoreWithUncachedBooks;
9+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\BookWithUncachedStores;
10+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBookWithStores;
711

812
class BelongsToManyTest extends IntegrationTestCase
913
{
@@ -159,4 +163,77 @@ public function testUncachedRelatedModelDoesntCache()
159163
$this->assertNotNull($result);
160164
$this->assertNotNull($uncachedResult);
161165
}
166+
167+
// /** @group test */
168+
// public function testUncachedDetachesFromCached()
169+
// {
170+
// // $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:book-store:genealabslaravelmodelcachingcachedbelongstomany-book_store.book_id_=_{$bookId}");
171+
// // $tags = [
172+
// // "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesstore",
173+
// // ];
174+
175+
// $store = (new StoreWithUncachedBooks)
176+
// ->with("books")
177+
// ->has("books")
178+
// ->first();
179+
// $store->books()
180+
// ->detach();
181+
// // $store->delete();
182+
// // dd($results);
183+
// // $cachedResult = $this
184+
// // ->cache()
185+
// // ->tags($tags)
186+
// // ->get($key)['value'];
187+
188+
// // $this->assertNotEmpty($result);
189+
// // $this->assertNull($cachedResult);
190+
// }
191+
192+
// /** @group test */
193+
// public function testCachedDetachesFromUncached()
194+
// {
195+
// // $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:book-store:genealabslaravelmodelcachingcachedbelongstomany-book_store.book_id_=_{$bookId}");
196+
// // $tags = [
197+
// // "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesstore",
198+
// // ];
199+
// $book = (new UncachedBookWithStores)
200+
// ->with("stores")
201+
// ->has("stores")
202+
// ->first();
203+
// $book->stores()
204+
// ->detach();
205+
// // $book->delete();
206+
// // dd($results);
207+
// // $cachedResult = $this
208+
// // ->cache()
209+
// // ->tags($tags)
210+
// // ->get($key)['value'];
211+
212+
// // $this->assertNotEmpty($result);
213+
// // $this->assertNull($cachedResult);
214+
// }
215+
216+
// public function testDetachingFiresEvent()
217+
// {
218+
// // $key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:book-store:genealabslaravelmodelcachingcachedbelongstomany-book_store.book_id_=_{$bookId}");
219+
// // $tags = [
220+
// // "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesstore",
221+
// // ];
222+
223+
// $store = (new Store)
224+
// ->with("books")
225+
// ->has("books")
226+
// ->first();
227+
// $store->books()
228+
// ->detach();
229+
// $store->delete();
230+
// // dd($results);
231+
// // $cachedResult = $this
232+
// // ->cache()
233+
// // ->tags($tags)
234+
// // ->get($key)['value'];
235+
236+
// // $this->assertNotEmpty($result);
237+
// // $this->assertNull($cachedResult);
238+
// }
162239
}

tests/Integration/CachedBuilder/GetTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,27 @@ public function testGetWithFieldArrayCachesResults()
5757
->tags($tags)
5858
->get($key)['value'];
5959
$liveResults = (new UncachedAuthor)
60-
->get();
60+
->get(["id", "name"]);
61+
62+
$this->assertEquals($liveResults->pluck("id"), $authors->pluck("id"));
63+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
64+
}
65+
66+
public function testGetWithFieldStringCachesResults()
67+
{
68+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_id-authors.deleted_at_null");
69+
$tags = [
70+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
71+
];
72+
73+
$authors = (new Author)
74+
->get("id");
75+
$cachedResults = $this
76+
->cache()
77+
->tags($tags)
78+
->get($key)['value'];
79+
$liveResults = (new UncachedAuthor)
80+
->get("id");
6181

6282
$this->assertEquals($liveResults->pluck("id"), $authors->pluck("id"));
6383
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));

tests/database/baseline.sqlite

24 KB
Binary file not shown.

tests/database/testing.sqlite

24 KB
Binary file not shown.

0 commit comments

Comments
 (0)