Skip to content

Commit 15c63f6

Browse files
committed
Merge branch 'release/0.2.36'
2 parents 9098a2b + 9f1dacb commit 15c63f6

File tree

6 files changed

+69
-29
lines changed

6 files changed

+69
-29
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ 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.36] - 23 Feb 2018
8+
### Added
9+
- config setting to allow disabling of model-caching.
10+
711
## [0.2.35] - 21 Feb 2018
812
### Fixed
913
- cache key generation for `find()` and `findOrFail()`.

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,17 @@ Not only that, but it probably isn't a good idea to cache the user model anyway,
106106
since you always want to pull the most up-to-date info on it.
107107

108108
### Optional Disabling Caching of Queries
109-
**Recommendation: add this to all your seeder queries to avoid pulling in
109+
There are two methods by which model-caching can be disabled:
110+
1. Use `->disableCache()` in a query-by-query instance.
111+
2. Set `MODEL_CACHE_DISABLED=TRUE` in your `.env` file.
112+
113+
**Recommendation: use option #1 in all your seeder queries to avoid pulling in
110114
cached information when reseeding multiple times.**
111115
You can disable a given query by using `disableCache()` in the query chain, and
112116
it needs to be placed (anywhere) prior to the query command (`get()`, `all()`,
113117
`find()`, etc). For example:
114118
```php
115-
$results = $myModel->disableCache()->all();
119+
$results = $myModel->disableCache()->get();
116120
```
117121

118122
### Manual Flushing of Specific Model

config/laravel-model-caching.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

33
return [
4-
'store' => env('MODEL_CACHE_STORE'),
5-
64
'cache-prefix' => '',
5+
6+
'disabled' => env('MODEL_CACHE_STORE', false),
7+
8+
'store' => env('MODEL_CACHE_STORE'),
79
];

src/CachedBuilder.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class CachedBuilder extends EloquentBuilder
1212

1313
public function avg($column)
1414
{
15-
if (! $this->isCachable) {
15+
if (! $this->isCachable()) {
1616
return parent::avg($column);
1717
}
1818

@@ -25,7 +25,7 @@ public function avg($column)
2525

2626
public function count($columns = ['*'])
2727
{
28-
if (! $this->isCachable) {
28+
if (! $this->isCachable()) {
2929
return parent::count($columns);
3030
}
3131

@@ -38,7 +38,7 @@ public function count($columns = ['*'])
3838

3939
public function cursor()
4040
{
41-
if (! $this->isCachable) {
41+
if (! $this->isCachable()) {
4242
return collect(parent::cursor());
4343
}
4444

@@ -62,7 +62,7 @@ public function delete()
6262
*/
6363
public function find($id, $columns = ['*'])
6464
{
65-
if (! $this->isCachable) {
65+
if (! $this->isCachable()) {
6666
return parent::find($id, $columns);
6767
}
6868

@@ -75,7 +75,7 @@ public function find($id, $columns = ['*'])
7575

7676
public function first($columns = ['*'])
7777
{
78-
if (! $this->isCachable) {
78+
if (! $this->isCachable()) {
7979
return parent::first($columns);
8080
}
8181

@@ -88,7 +88,7 @@ public function first($columns = ['*'])
8888

8989
public function get($columns = ['*'])
9090
{
91-
if (! $this->isCachable) {
91+
if (! $this->isCachable()) {
9292
return parent::get($columns);
9393
}
9494

@@ -101,7 +101,7 @@ public function get($columns = ['*'])
101101

102102
public function max($column)
103103
{
104-
if (! $this->isCachable) {
104+
if (! $this->isCachable()) {
105105
return parent::max($column);
106106
}
107107

@@ -114,7 +114,7 @@ public function max($column)
114114

115115
public function min($column)
116116
{
117-
if (! $this->isCachable) {
117+
if (! $this->isCachable()) {
118118
return parent::min($column);
119119
}
120120

@@ -131,7 +131,7 @@ public function paginate(
131131
$pageName = 'page',
132132
$page = null
133133
) {
134-
if (! $this->isCachable) {
134+
if (! $this->isCachable()) {
135135
return parent::paginate($perPage, $columns, $pageName, $page);
136136
}
137137

@@ -145,7 +145,7 @@ public function paginate(
145145

146146
public function pluck($column, $key = null)
147147
{
148-
if (! $this->isCachable) {
148+
if (! $this->isCachable()) {
149149
return parent::pluck($column, $key);
150150
}
151151

@@ -159,7 +159,7 @@ public function pluck($column, $key = null)
159159

160160
public function sum($column)
161161
{
162-
if (! $this->isCachable) {
162+
if (! $this->isCachable()) {
163163
return parent::sum($column);
164164
}
165165

@@ -172,7 +172,7 @@ public function sum($column)
172172

173173
public function value($column)
174174
{
175-
if (! $this->isCachable) {
175+
if (! $this->isCachable()) {
176176
return parent::value($column);
177177
}
178178

src/Traits/Cachable.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,18 @@ public static function all($columns = ['*'])
9999

100100
public function newEloquentBuilder($query)
101101
{
102-
if (! $this->isCachable) {
102+
if (! $this->isCachable()) {
103103
$this->isCachable = true;
104104

105105
return new EloquentBuilder($query);
106106
}
107107

108108
return new CachedBuilder($query);
109109
}
110+
111+
public function isCachable() : bool
112+
{
113+
return $this->isCachable
114+
&& ! config('laravel-model-caching.disabled');
115+
}
110116
}

tests/Unit/Traits/CachableTest.php

+36-12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function testSpecifyingAlternateCacheDriver()
2323
{
2424
$configCacheStores = config('cache.stores');
2525
$configCacheStores['customCache'] = ['driver' => 'array'];
26+
// TODO: make sure the alternate cache is actually loaded
2627
config(['cache.stores' => $configCacheStores]);
2728
config(['laravel-model-caching.store' => 'customCache']);
2829
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
@@ -49,28 +50,51 @@ public function testSetCachePrefixAttribute()
4950
{
5051
(new PrefixedAuthor)->get();
5152

52-
$results = $this->cache()
53-
->tags(['genealabs:laravel-model-caching:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor'])
53+
$results = $this->
54+
cache()
55+
->tags([
56+
'genealabs:laravel-model-caching:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor',
57+
])
5458
->get(sha1('genealabs:laravel-model-caching:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor'))['value'];
5559

5660
$this->assertNotNull($results);
5761
}
5862

59-
public function testAllReturnsCollection()
63+
// public function testAllReturnsCollection()
64+
// {
65+
// (new Author)->truncate();
66+
// factory(Author::class, 1)->create();
67+
// $authors = (new Author)->all();
68+
//
69+
// $cachedResults = $this
70+
// ->cache()
71+
// ->tags([
72+
// 'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
73+
// ])
74+
// ->get(sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'))['value'];
75+
// $liveResults = (new UncachedAuthor)->all();
76+
//
77+
// $this->assertInstanceOf(Collection::class, $authors);
78+
// $this->assertInstanceOf(Collection::class, $cachedResults);
79+
// $this->assertInstanceOf(Collection::class, $liveResults);
80+
// }
81+
82+
public function testsCacheFlagDisablesCaching()
6083
{
61-
(new Author)->truncate();
62-
factory(Author::class, 1)->create();
63-
$authors = (new Author)->all();
84+
config(['laravel-model-caching.disabled' => true]);
6485

65-
$cachedResults = $this->cache()
86+
$authors = (new Author)->get();
87+
$cachedAuthors = $this
88+
->cache()
6689
->tags([
6790
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
6891
])
69-
->get(sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'))['value'];
70-
$liveResults = (new UncachedAuthor)->all();
92+
->get(sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'));
93+
94+
config(['laravel-model-caching.disabled' => false]);
7195

72-
$this->assertInstanceOf(Collection::class, $authors);
73-
$this->assertInstanceOf(Collection::class, $cachedResults);
74-
$this->assertInstanceOf(Collection::class, $liveResults);
96+
$this->assertNull($cachedAuthors);
97+
$this->assertNotEmpty($authors);
98+
$this->assertCount(10, $authors);
7599
}
76100
}

0 commit comments

Comments
 (0)