File tree Expand file tree Collapse file tree 5 files changed +67
-0
lines changed Expand file tree Collapse file tree 5 files changed +67
-0
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44The format is based on [ Keep a Changelog] ( http://keepachangelog.com/en/1.0.0/ )
55and 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
Original file line number Diff line number Diff line change 154154There are two methods by which model-caching can be disabled:
1551551 . Use ` ->disableCache() ` in a query-by-query instance.
1561562 . 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
159166cached information when reseeding multiple times.**
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11<?php namespace GeneaLabs \LaravelModelCaching \Providers ;
22
33use GeneaLabs \LaravelModelCaching \Console \Commands \Clear ;
4+ use GeneaLabs \LaravelModelCaching \Helper ;
45use Illuminate \Support \ServiceProvider ;
56
67class 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments