Skip to content

Commit 06f1a79

Browse files
committed
Added helper method runDisabled() to run blocks of code without model caching
1 parent 7d10ba5 commit 06f1a79

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
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.4.13] - 4 Apr 2019
8+
### Added
9+
- helper function to run closure with model-caching disabled. Thanks for the suggestion, @mycarrysun
10+
711
## [0.4.12] - 3 Apr 2019
812
### Updated
913
- string and array helpers to use the `Str` and `Arr` classes directly, in preparation for helper deprecations in Laravel 5.9. Thanks @mycarrysun

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ or:
154154
There are two methods by which model-caching can be disabled:
155155
1. Use `->disableCache()` in a query-by-query instance.
156156
2. Set `MODEL_CACHE_DISABLED=TRUE` in your `.env` file.
157+
3. If you only need to disable the cache for a block of code, or for non-
158+
eloquent queries, this is probably the better option:
159+
```php
160+
app("model-cache")->runDisabled(function () {
161+
// your code here, it may return, as well
162+
});
163+
```
157164

158165
**Recommendation: use option #1 in all your seeder queries to avoid pulling in
159166
cached information when reseeding multiple times.**

src/Helper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching;
2+
3+
class Helper
4+
{
5+
public function runDisabled(callable $closure)
6+
{
7+
$originalSetting = config('laravel-model-caching.disabled');
8+
config(['laravel-model-caching.disabled' => true]);
9+
10+
$result = $closure();
11+
12+
config(['laravel-model-caching.disabled' => $originalSetting]);
13+
14+
return $result;
15+
}
16+
}

src/Providers/Service.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Providers;
22

33
use GeneaLabs\LaravelModelCaching\Console\Commands\Clear;
4+
use GeneaLabs\LaravelModelCaching\Helper;
45
use Illuminate\Support\ServiceProvider;
56

67
class Service extends ServiceProvider
@@ -13,4 +14,9 @@ public function boot()
1314
$this->mergeConfigFrom($configPath, 'laravel-model-caching');
1415
$this->commands(Clear::class);
1516
}
17+
18+
public function register()
19+
{
20+
$this->app->bind("model-cache", Helper::class);
21+
}
1622
}

tests/Integration/HelperTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
5+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
6+
7+
class HelperTest extends IntegrationTestCase
8+
{
9+
public function testClosureRunsWithCacheDisabled()
10+
{
11+
$key = sha1('genealabs:laravel-model-caching:testing::memory::authors:genealabslaravelmodelcachingtestsfixturesauthor');
12+
$tags = ['genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor'];
13+
14+
$authors = app("model-cache")->runDisabled(function () {
15+
return (new Author)
16+
->get();
17+
});
18+
19+
$cachedResults1 = $this->cache()
20+
->tags($tags)
21+
->get($key)["value"];
22+
(new Author)
23+
->get();
24+
$cachedResults2 = $this->cache()
25+
->tags($tags)
26+
->get($key)["value"];
27+
$liveResults = (new UncachedAuthor)
28+
->get();
29+
30+
$this->assertEquals($liveResults->toArray(), $authors->toArray());
31+
$this->assertNull($cachedResults1);
32+
$this->assertEquals($authors->toArray(), $cachedResults2->toArray());
33+
}
34+
}

0 commit comments

Comments
 (0)