Skip to content

Commit cc87d74

Browse files
author
Henk Koop
committed
Merge remote-tracking branch 'upstream/master' into respect-isCachable
2 parents 56c2e75 + 7a7bed0 commit cc87d74

15 files changed

+270
-184
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.2.49] - 9 Mar 2018
8+
### Fixed
9+
- caching of `->first()` queries.
10+
11+
## [0.2.48] - 9 Mar 2018
12+
### Added
13+
- database connection name to cache prefix.
14+
15+
## [0.2.47] - 5 Mar 2018
16+
### Fixed
17+
- exception when calling disableCache() when caching is already disabled via config.
18+
19+
## [0.2.46] - 5 Mar 2018
20+
### Fixed
21+
- package dependency version to work with Laravel 5.5.
22+
723
## [0.2.45] - 3 Mar 2018
824
### Fixed
925
- pagination cache key generation; fixes #85.

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ abstract class BaseModel
6767
//
6868
}
6969
```
70+
### Multiple Database Connections
71+
__Thanks to @dtvmedia for suggestion this feature. This is actually a more robust
72+
solution than cache-prefixes.__
73+
74+
Keeping keys separate for multiple database connections is automatically handled.
75+
This is especially important for multi-tenant applications, and of course any
76+
application using multiple database connections.
7077

7178
### Optional Cache Key Prefix
7279
Thanks to @lucian-dragomir for suggesting this feature! You can use cache key
@@ -75,7 +82,8 @@ it is recommended to add the Cachable trait to a base model, then set the cache
7582
key prefix config value there.
7683

7784
**Note that the config setting is included before the parent method is called,
78-
so that the setting is available in the parent as well.**
85+
so that the setting is available in the parent as well. If you are developing a
86+
multi-tenant application, see the note above.**
7987

8088
Here's is an example:
8189
```php
@@ -92,7 +100,7 @@ class BaseModel extends Model
92100

93101
public function __construct($attributes = [])
94102
{
95-
config(['genealabs:laravel-model-caching' => 'test-prefix']);
103+
config(['laravel-model-caching.cache-prefix' => 'test-prefix']);
96104

97105
parent::__construct($attributes);
98106
}

src/CacheKey.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
<?php namespace GeneaLabs\LaravelModelCaching;
22

3+
use GeneaLabs\LaravelModelCaching\Traits\CachePrefixing;
34
use Illuminate\Database\Eloquent\Model;
45
use Illuminate\Database\Query\Builder;
56
use Illuminate\Support\Collection;
67

78
class CacheKey
89
{
10+
use CachePrefixing;
11+
912
protected $eagerLoad;
1013
protected $model;
1114
protected $query;
1215

13-
protected function getCachePrefix() : string
14-
{
15-
return "genealabs:laravel-model-caching:"
16-
. (config("laravel-model-caching.cache-prefix")
17-
? config("laravel-model-caching.cache-prefix", "") . ":"
18-
: "");
19-
}
20-
2116
public function __construct(
2217
array $eagerLoad,
2318
Model $model,

src/CacheTags.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
<?php namespace GeneaLabs\LaravelModelCaching;
22

33
use GeneaLabs\LaravelModelCaching\CachedBuilder;
4+
use GeneaLabs\LaravelModelCaching\Traits\CachePrefixing;
45
use Illuminate\Database\Eloquent\Model;
56
use Illuminate\Database\Eloquent\Relations\Relation;
7+
use Illuminate\Database\Query\Builder;
68

79
class CacheTags
810
{
11+
use CachePrefixing;
12+
913
protected $eagerLoad;
1014
protected $model;
15+
protected $query;
1116

12-
public function __construct(array $eagerLoad, Model $model)
13-
{
17+
public function __construct(
18+
array $eagerLoad,
19+
Model $model,
20+
Builder $query
21+
) {
1422
$this->eagerLoad = $eagerLoad;
1523
$this->model = $model;
16-
}
17-
18-
protected function getCachePrefix() : string
19-
{
20-
return "genealabs:laravel-model-caching:"
21-
. (config('laravel-model-caching.cache-prefix')
22-
? config('laravel-model-caching.cache-prefix', '') . ":"
23-
: "");
24+
$this->query = $query;
2425
}
2526

2627
public function make() : array

src/CachedBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function first($columns = ["*"])
6060
return parent::first($columns);
6161
}
6262

63-
$cacheKey = $this->makeCacheKey($columns);
63+
$cacheKey = $this->makeCacheKey($columns, null, "-first");
6464

6565
return $this->cachedValue(func_get_args(), $cacheKey);
6666
}

src/Console/Commands/Flush.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ protected function flushModelCache(string $option) : int
4949
return 0;
5050
}
5151

52-
protected function getAllTraitsUsedByClass(string $classname, bool $autoload = true) : Collection
53-
{
52+
protected function getAllTraitsUsedByClass(
53+
string $classname,
54+
bool $autoload = true
55+
) : Collection {
5456
$traits = collect();
5557

5658
if (class_exists($classname, $autoload)) {
@@ -60,7 +62,8 @@ protected function getAllTraitsUsedByClass(string $classname, bool $autoload = t
6062
$parentClass = get_parent_class($classname);
6163

6264
if ($parentClass) {
63-
$traits = $traits->merge($this->getAllTraitsUsedByClass($parentClass, $autoload));
65+
$traits = $traits
66+
->merge($this->getAllTraitsUsedByClass($parentClass, $autoload));
6467
}
6568

6669
return $traits;

src/Traits/CachePrefixing.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Traits;
2+
3+
trait CachePrefixing
4+
{
5+
protected function getCachePrefix() : string
6+
{
7+
return "genealabs:laravel-model-caching:"
8+
. $this->getDatabaseConnectionName() . ":"
9+
. (config("laravel-model-caching.cache-prefix")
10+
? config("laravel-model-caching.cache-prefix", "") . ":"
11+
: "");
12+
}
13+
14+
protected function getDatabaseConnectionName() : string
15+
{
16+
return $this->query->connection->getName();
17+
}
18+
}

src/Traits/Caching.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ protected function makeCacheKey(
7777

7878
protected function makeCacheTags() : array
7979
{
80-
$tags = (new CacheTags($this->eagerLoad ?? [], $this->model ?? $this))
80+
$eagerLoad = $this->eagerLoad ?? [];
81+
$model = $this->model ?? $this;
82+
$query = $this->query ?? app(Builder::class);
83+
84+
$tags = (new CacheTags($eagerLoad, $model, $query))
8185
->make();
8286

8387
return $tags;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
6+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
7+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
8+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
9+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook;
10+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile;
11+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPublisher;
12+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedStore;
13+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Http\Resources\Author as AuthorResource;
14+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
15+
use Illuminate\Foundation\Testing\RefreshDatabase;
16+
use Illuminate\Support\Collection;
17+
18+
class CachedBuilderMultipleQueryTest extends IntegrationTestCase
19+
{
20+
use RefreshDatabase;
21+
22+
public function testCallingAllThenFirstQueriesReturnsDifferingResults()
23+
{
24+
$allAuthors = (new Author)->all();
25+
$firstAuthor = (new Author)->first();
26+
27+
$this->assertNotEquals($allAuthors, $firstAuthor);
28+
$this->assertInstanceOf(Author::class, $firstAuthor);
29+
$this->assertInstanceOf(Collection::class, $allAuthors);
30+
}
31+
32+
public function testCallingGetThenFirstQueriesReturnsDifferingResults()
33+
{
34+
$allAuthors = (new Author)->get();
35+
$firstAuthor = (new Author)->first();
36+
37+
$this->assertNotEquals($allAuthors, $firstAuthor);
38+
$this->assertInstanceOf(Author::class, $firstAuthor);
39+
$this->assertInstanceOf(Collection::class, $allAuthors);
40+
}
41+
}

tests/Integration/CachedBuilderPaginationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public function testPaginationIsCached()
2727
$authors = (new Author)
2828
->paginate(3);
2929

30-
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor-paginate_by_3_page_1');
30+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor-paginate_by_3_page_1');
3131
$tags = [
32-
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
32+
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
3333
];
3434

3535
$cachedResults = $this->cache()

0 commit comments

Comments
 (0)