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.
4
4
The format is based on [ Keep a Changelog] ( http://keepachangelog.com/en/1.0.0/ )
5
5
and this project adheres to [ Semantic Versioning] ( http://semver.org/spec/v2.0.0.html ) .
6
6
7
+ ## [ 0.2.51] - 51 Mar 2018
8
+ ### Added
9
+ - disabling of ` all() ` query.
10
+
7
11
## [ 0.2.50] - 10 Mar 2018
8
12
### Added
9
13
- 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:
131
131
1 . Use ` ->disableCache() ` in a query-by-query instance.
132
132
2 . Set ` MODEL_CACHE_DISABLED=TRUE ` in your ` .env ` file.
133
133
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
-
139
134
** Recommendation: use option #1 in all your seeder queries to avoid pulling in
140
135
cached information when reseeding multiple times.**
141
136
You 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 1
1
<?php namespace GeneaLabs \LaravelModelCaching ;
2
2
3
+ use GeneaLabs \LaravelModelCaching \Traits \BuilderCaching ;
3
4
use GeneaLabs \LaravelModelCaching \Traits \Caching ;
4
5
use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
5
6
8
9
*/
9
10
class CachedBuilder extends EloquentBuilder
10
11
{
12
+ use BuilderCaching;
11
13
use Caching;
12
14
13
15
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()
32
32
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor ' ,
33
33
];
34
34
35
- $ cachedResults = $ this ->cache ()
35
+ $ cachedResults = $ this
36
+ ->cache ()
36
37
->tags ($ tags )
37
38
->get ($ key )['value ' ];
38
39
$ 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