File tree Expand file tree Collapse file tree 6 files changed +62
-6
lines changed Expand file tree Collapse file tree 6 files changed +62
-6
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.2.51] - 51 Mar 2018
8+ ### Added
9+ - disabling of ` all() ` query.
10+
711## [ 0.2.50] - 10 Mar 2018
812### Added
913- cache invalidation when ` destroy() ` ing models.
Original file line number Diff line number Diff line change @@ -131,11 +131,6 @@ There are two methods by which model-caching can be disabled:
1311311 . Use ` ->disableCache() ` in a query-by-query instance.
1321322 . Set ` MODEL_CACHE_DISABLED=TRUE ` in your ` .env ` file.
133133
134- ** EXCEPTION:** currently the ` ::all() ` method cannot be disabled by doing something
135- like this: ` $model->disableCache()->all() ` , because it is a static method. To
136- work around this, use the ` ->get() ` method if you really need to disable the
137- cache for that single query. Disabling cache via the config flag still works.
138-
139134** Recommendation: use option #1 in all your seeder queries to avoid pulling in
140135cached information when reseeding multiple times.**
141136You can disable a given query by using ` disableCache() ` anywhere in the query chain. For example:
Original file line number Diff line number Diff line change 11<?php namespace GeneaLabs \LaravelModelCaching ;
22
3+ use GeneaLabs \LaravelModelCaching \Traits \BuilderCaching ;
34use GeneaLabs \LaravelModelCaching \Traits \Caching ;
45use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
56
89 */
910class CachedBuilder extends EloquentBuilder
1011{
12+ use BuilderCaching;
1113 use Caching;
1214
1315 public function avg ($ column )
Original file line number Diff line number Diff line change 1+ <?php namespace GeneaLabs \LaravelModelCaching \Traits ;
2+
3+ use Illuminate \Database \Eloquent \Collection ;
4+
5+ trait BuilderCaching
6+ {
7+ public function all ($ columns = ['* ' ]) : Collection
8+ {
9+ if (! $ this ->isCachable ()) {
10+ $ this ->model ->disableModelCaching ();
11+ }
12+
13+ return $ this ->model ->get ($ columns );
14+ }
15+ }
Original file line number Diff line number Diff line change @@ -32,7 +32,8 @@ public function testPaginationIsCached()
3232 'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor ' ,
3333 ];
3434
35- $ cachedResults = $ this ->cache ()
35+ $ cachedResults = $ this
36+ ->cache ()
3637 ->tags ($ tags )
3738 ->get ($ key )['value ' ];
3839 $ liveResults = (new UncachedAuthor )
Original file line number Diff line number Diff line change 1+ <?php namespace GeneaLabs \LaravelModelCaching \Tests \Integration \Traits ;
2+
3+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \Author ;
4+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \Book ;
5+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \PrefixedAuthor ;
6+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \Profile ;
7+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \Publisher ;
8+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \Store ;
9+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \UncachedAuthor ;
10+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \UncachedBook ;
11+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \UncachedProfile ;
12+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \UncachedPublisher ;
13+ use GeneaLabs \LaravelModelCaching \Tests \Fixtures \UncachedStore ;
14+ use GeneaLabs \LaravelModelCaching \Tests \IntegrationTestCase ;
15+ use Illuminate \Foundation \Testing \RefreshDatabase ;
16+ use Illuminate \Database \Eloquent \Collection ;
17+
18+ class BuilderCachingTest extends IntegrationTestCase
19+ {
20+ use RefreshDatabase;
21+
22+ public function testDisablingAllQuery ()
23+ {
24+ $ allAuthors = (new Author )
25+ ->disableCache ()
26+ ->all ();
27+ $ key = sha1 ("genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor " );
28+ $ tags = [
29+ "genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor " ,
30+ ];
31+ $ cachedAuthors = $ this
32+ ->cache ()
33+ ->tags ($ tags )
34+ ->get ($ key )["value " ];
35+
36+ $ this ->assertInstanceOf (Collection::class, $ allAuthors );
37+ $ this ->assertNull ($ cachedAuthors );
38+ }
39+ }
You can’t perform that action at this time.
0 commit comments