Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebronner committed Feb 24, 2018
2 parents d1bde66 + 15c4e5b commit 8c3955e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
filter:
excluded_paths:
- "tests/"
build:
environment:
variables:
REDIS_HOST: '127.0.0.1'
nodes:
tests: false
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ There are two methods by which model-caching can be disabled:
1. Use `->disableCache()` in a query-by-query instance.
2. Set `MODEL_CACHE_DISABLED=TRUE` in your `.env` file.

**EXCEPTION:** currently the `::all()` method cannot be disabled by doing something
like this: `$model->disableCache()->all()`, because it is a static method. To
work around this, use the `->get()` method if you really need to disable the
cache for that single query. Disabling cache via the config flag still works.

**Recommendation: use option #1 in all your seeder queries to avoid pulling in
cached information when reseeding multiple times.**
You can disable a given query by using `disableCache()` in the query chain, and
Expand Down
4 changes: 4 additions & 0 deletions src/Traits/Cachable.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public static function bootCachable()

public static function all($columns = ['*'])
{
if (config('laravel-model-caching.disabled')) {
return parent::all($columns);
}

$class = get_called_class();
$instance = new $class;
$tags = [str_slug(get_called_class())];
Expand Down
24 changes: 23 additions & 1 deletion tests/Unit/CachedModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public function testAllModelResultsCreatesCache()
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
];

$cachedResults = $this->cache()
$cachedResults = $this
->cache()
->tags($tags)
->get($key)['value'];
$liveResults = (new UncachedAuthor)
Expand All @@ -51,4 +52,25 @@ public function testScopeDisablesCaching()
$this->assertNull($cachedResults);
$this->assertNotEquals($authors, $cachedResults);
}

public function testAllMethodCachingCanBeDisabledViaConfig()
{
config(['laravel-model-caching.disabled' => true]);
$authors = (new Author)
->all();
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = [
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
];
config(['laravel-model-caching.disabled' => false]);

$cachedResults = $this
->cache()
->tags($tags)
->get($key)['value'];

$this->assertEmpty($cachedResults);
$this->assertNotEmpty($authors);
$this->assertCount(10, $authors);
}
}
36 changes: 18 additions & 18 deletions tests/Unit/Traits/CachableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,24 @@ public function testSetCachePrefixAttribute()
$this->assertNotNull($results);
}

// public function testAllReturnsCollection()
// {
// (new Author)->truncate();
// factory(Author::class, 1)->create();
// $authors = (new Author)->all();
//
// $cachedResults = $this
// ->cache()
// ->tags([
// 'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
// ])
// ->get(sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'))['value'];
// $liveResults = (new UncachedAuthor)->all();
//
// $this->assertInstanceOf(Collection::class, $authors);
// $this->assertInstanceOf(Collection::class, $cachedResults);
// $this->assertInstanceOf(Collection::class, $liveResults);
// }
public function testAllReturnsCollection()
{
(new Author)->truncate();
factory(Author::class, 1)->create();
$authors = (new Author)->all();

$cachedResults = $this
->cache()
->tags([
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
])
->get(sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'))['value'];
$liveResults = (new UncachedAuthor)->all();

$this->assertInstanceOf(Collection::class, $authors);
$this->assertInstanceOf(Collection::class, $cachedResults);
$this->assertInstanceOf(Collection::class, $liveResults);
}

public function testsCacheFlagDisablesCaching()
{
Expand Down

0 comments on commit 8c3955e

Please sign in to comment.