Skip to content

Commit 5ed9daf

Browse files
authored
Merge pull request #40 from GeneaLabs/laravel-5.5
Implement optional disabling of caching
2 parents db70b30 + bbdf79c commit 5ed9daf

9 files changed

+523
-8
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ 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.12] - 14 Dec 2017
8+
### Added
9+
- chainable method to disable caching of queries.
10+
11+
## [0.2.11] - 13 Dec 2017
12+
### Added
13+
- functionality to clear corresponding cache tags when model is deleted.
14+
15+
## [0.2.10] - 5 Dec 2017
16+
### Fixed
17+
- caching when using `orderByRaw()`.
18+
19+
## [0.2.9] - 19 Nov 2017
20+
### Added
21+
- test for query scopes.
22+
- test for relationship query.
23+
24+
### Updated
25+
- readme file.
26+
- travis configuration.
27+
728
## [0.2.8] - 2017-10-17
829
### Updated
930
- code with optimizations and refactoring.

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ abstract class BaseModel extends CachedModel
5050
}
5151
```
5252

53+
### Disabling Caching of Queries
54+
**Recommendation: add this to all your seeder queries to avoid pulling in
55+
cacched information when reseeding multiple times.**
56+
You can disable a given query by using `disableCache()` in the query chain, and
57+
it needs to be placed (anywhere) prior to the query command (`get()`, `all()`,
58+
`find()`, etc). For example:
59+
```php
60+
$results = $myModel->disableCache()->all();
61+
```
62+
5363
**That's all you need to do. All model queries and relationships are now
5464
cached!**
5565

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
convertNoticesToExceptions="true"
77
convertWarningsToExceptions="true"
88
processIsolation="false"
9-
stopOnFailure="true"
9+
stopOnFailure="false"
1010
syntaxCheck="false"
1111
bootstrap="vendor/autoload.php"
1212
>

src/CachedBuilder.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
44
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
55

6+
/**
7+
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
8+
*/
69
class CachedBuilder extends EloquentBuilder
710
{
811
use Cachable;
912

13+
protected $isCachable = true;
14+
1015
public function avg($column)
1116
{
17+
if (! $this->isCachable) {
18+
return parent::avg($column);
19+
}
20+
1221
return $this->cache($this->makeCacheTags())
1322
->rememberForever($this->makeCacheKey() . "-avg_{$column}", function () use ($column) {
1423
return parent::avg($column);
@@ -17,6 +26,10 @@ public function avg($column)
1726

1827
public function count($columns = ['*'])
1928
{
29+
if (! $this->isCachable) {
30+
return parent::count($columns);
31+
}
32+
2033
return $this->cache($this->makeCacheTags())
2134
->rememberForever($this->makeCacheKey() . "-count", function () use ($columns) {
2235
return parent::count($columns);
@@ -25,6 +38,10 @@ public function count($columns = ['*'])
2538

2639
public function cursor()
2740
{
41+
if (! $this->isCachable) {
42+
return collect(parent::cursor());
43+
}
44+
2845
return $this->cache($this->makeCacheTags())
2946
->rememberForever($this->makeCacheKey() . "-cursor", function () {
3047
return collect(parent::cursor());
@@ -39,11 +56,22 @@ public function delete()
3956
return parent::delete();
4057
}
4158

59+
public function disableCache()
60+
{
61+
$this->isCachable = false;
62+
63+
return $this;
64+
}
65+
4266
/**
4367
* @SuppressWarnings(PHPMD.ShortVariable)
4468
*/
4569
public function find($id, $columns = ['*'])
4670
{
71+
if (! $this->isCachable) {
72+
return parent::find($id, $columns);
73+
}
74+
4775
return $this->cache($this->makeCacheTags())
4876
->rememberForever($this->makeCacheKey($columns, $id), function () use ($id, $columns) {
4977
return parent::find($id, $columns);
@@ -52,6 +80,10 @@ public function find($id, $columns = ['*'])
5280

5381
public function first($columns = ['*'])
5482
{
83+
if (! $this->isCachable) {
84+
return parent::first($columns);
85+
}
86+
5587
return $this->cache($this->makeCacheTags())
5688
->rememberForever($this->makeCacheKey($columns) . '-first', function () use ($columns) {
5789
return parent::first($columns);
@@ -60,6 +92,10 @@ public function first($columns = ['*'])
6092

6193
public function get($columns = ['*'])
6294
{
95+
if (! $this->isCachable) {
96+
return parent::get($columns);
97+
}
98+
6399
return $this->cache($this->makeCacheTags())
64100
->rememberForever($this->makeCacheKey($columns), function () use ($columns) {
65101
return parent::get($columns);
@@ -68,6 +104,10 @@ public function get($columns = ['*'])
68104

69105
public function max($column)
70106
{
107+
if (! $this->isCachable) {
108+
return parent::max($column);
109+
}
110+
71111
return $this->cache($this->makeCacheTags())
72112
->rememberForever($this->makeCacheKey() . "-max_{$column}", function () use ($column) {
73113
return parent::max($column);
@@ -76,6 +116,10 @@ public function max($column)
76116

77117
public function min($column)
78118
{
119+
if (! $this->isCachable) {
120+
return parent::min($column);
121+
}
122+
79123
return $this->cache($this->makeCacheTags())
80124
->rememberForever($this->makeCacheKey() . "-min_{$column}", function () use ($column) {
81125
return parent::min($column);
@@ -84,6 +128,10 @@ public function min($column)
84128

85129
public function pluck($column, $key = null)
86130
{
131+
if (! $this->isCachable) {
132+
return parent::pluck($column, $key);
133+
}
134+
87135
$cacheKey = $this->makeCacheKey([$column]) . "-pluck_{$column}";
88136

89137
if ($key) {
@@ -98,6 +146,10 @@ public function pluck($column, $key = null)
98146

99147
public function sum($column)
100148
{
149+
if (! $this->isCachable) {
150+
return parent::sum($column);
151+
}
152+
101153
return $this->cache($this->makeCacheTags())
102154
->rememberForever($this->makeCacheKey() . "-sum_{$column}", function () use ($column) {
103155
return parent::sum($column);

src/CachedModel.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@
33
use GeneaLabs\LaravelModelCaching\CachedBuilder as Builder;
44
use Illuminate\Cache\CacheManager;
55
use Illuminate\Cache\TaggableStore;
6+
use Illuminate\Cache\TaggedCache;
7+
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
68
use Illuminate\Database\Eloquent\Model;
79
use Illuminate\Database\Eloquent\Relations\Relation;
8-
use LogicException;
9-
10-
use Illuminate\Cache\TaggedCache;
1110
use Illuminate\Support\Collection;
11+
use LogicException;
1212

1313
abstract class CachedModel extends Model
1414
{
1515
public function newEloquentBuilder($query)
1616
{
17+
if (session('genealabs-laravel-model-caching-is-disabled')) {
18+
session()->forget('genealabs-laravel-model-caching-is-disabled');
19+
20+
return new EloquentBuilder($query);
21+
}
22+
1723
return new Builder($query);
1824
}
1925

@@ -52,6 +58,13 @@ public function cache(array $tags = [])
5258
return $cache;
5359
}
5460

61+
public function disableCache() : self
62+
{
63+
session(['genealabs-laravel-model-caching-is-disabled' => true]);
64+
65+
return $this;
66+
}
67+
5568
public function flushCache(array $tags = [])
5669
{
5770
$this->cache($tags)->flush();

tests/Unit/CachedBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function setUp()
2929
cache()->flush();
3030
$publishers = factory(Publisher::class, 10)->create();
3131
factory(Author::class, 10)->create()
32-
->each(function($author) use ($publishers) {
32+
->each(function ($author) use ($publishers) {
3333
factory(Book::class, random_int(2, 10))->make()
3434
->each(function ($book) use ($author, $publishers) {
3535
$book->author()->associate($author);

tests/Unit/CachedModelTest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function setUp()
2424
cache()->flush();
2525
$publishers = factory(Publisher::class, 10)->create();
2626
factory(Author::class, 10)->create()
27-
->each(function($author) use ($publishers) {
27+
->each(function ($author) use ($publishers) {
2828
factory(Book::class, random_int(2, 10))->make()
2929
->each(function ($book) use ($author, $publishers) {
3030
$book->author()->associate($author);
@@ -52,11 +52,33 @@ public function testAllModelResultsCreatesCache()
5252
'genealabslaravelmodelcachingtestsfixturesauthor',
5353
];
5454

55-
$cachedResults = cache()->tags($tags)
55+
$cachedResults = cache()
56+
->tags($tags)
5657
->get($key);
57-
$liveResults = (new UncachedAuthor)->all();
58+
$liveResults = (new UncachedAuthor)
59+
->all();
5860

5961
$this->assertEquals($authors, $cachedResults);
6062
$this->assertEmpty($liveResults->diffAssoc($cachedResults));
6163
}
64+
65+
/**
66+
* @group test
67+
**/
68+
public function testScopeDisablesCaching()
69+
{
70+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';
71+
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
72+
$authors = (new Author)
73+
->where("name", "Bruno")
74+
->disableCache()
75+
->get();
76+
77+
$cachedResults = cache()
78+
->tags($tags)
79+
->get($key);
80+
81+
$this->assertNull($cachedResults);
82+
$this->assertNotEquals($authors, $cachedResults);
83+
}
6284
}

0 commit comments

Comments
 (0)