7
7
use Illuminate \Support \Collection ;
8
8
use Illuminate \Database \Eloquent \Relations \Relation ;
9
9
10
- class Builder extends EloquentBuilder
10
+ class CachedBuilder extends EloquentBuilder
11
11
{
12
12
protected function cache (array $ tags = [])
13
13
{
@@ -22,32 +22,72 @@ protected function cache(array $tags = [])
22
22
23
23
protected function getCacheKey (array $ columns = ['* ' ], $ idColumn = null ) : string
24
24
{
25
- $ key = str_slug (get_class ($ this ->model ));
26
- $ key .= $ idColumn ? "_ {$ idColumn }" : '' ;
25
+ $ key = $ this ->getModelSlug ();
26
+ $ key .= $ this ->getIdColumn ($ idColumn ?: '' );
27
+ $ key .= $ this ->getQueryColumns ($ columns );
28
+ $ key .= $ this ->getWhereClauses ();
29
+ $ key .= $ this ->getWithModels ();
30
+ $ key .= $ this ->getOffsetClause ();
31
+ $ key .= $ this ->getLimitClause ();
27
32
28
- if ($ columns !== ['* ' ]) {
29
- $ key .= '_ ' . implode ('_ ' , $ columns );
33
+ return $ key ;
34
+ }
35
+
36
+ protected function getIdColumn (string $ idColumn ) : string
37
+ {
38
+ return $ idColumn ? "_ {$ idColumn }" : '' ;
39
+ }
40
+
41
+ protected function getLimitClause () : string
42
+ {
43
+ if (! $ this ->query ->limit ) {
44
+ return '' ;
30
45
}
31
46
32
- $ key .= collect ( $ this ->query ->wheres )-> reduce ( function ( $ carry , $ where ) {
33
- $ value = $ where [ ' value ' ] ?? implode ( ' _ ' , $ where [ ' values ' ]) ?? '' ;
47
+ return " -limit_ { $ this ->query ->limit }" ;
48
+ }
34
49
35
- return "{$ carry }- {$ where ['column ' ]}_ {$ value }" ;
36
- });
50
+ protected function getModelSlug () : string
51
+ {
52
+ return str_slug (get_class ($ this ->model ));
53
+ }
37
54
38
- if (collect ($ this ->eagerLoad )->isNotEmpty ()) {
39
- $ key .= '- ' . implode ('- ' , collect ($ this ->eagerLoad )->keys ()->toArray ());
55
+ protected function getOffsetClause () : string
56
+ {
57
+ if (! $ this ->query ->offset ) {
58
+ return '' ;
40
59
}
41
60
42
- if ($ this ->query ->offset ) {
43
- $ key .= "-offset_ {$ this ->query ->offset }" ;
61
+ return "-offset_ {$ this ->query ->offset }" ;
62
+ }
63
+
64
+ protected function getQueryColumns (array $ columns ) : string
65
+ {
66
+ if ($ columns === ['* ' ] || $ columns === []) {
67
+ return '' ;
44
68
}
45
69
46
- if ($ this ->query ->limit ) {
47
- $ key .= "-limit_ {$ this ->query ->limit }" ;
70
+ return '_ ' . implode ('_ ' , $ columns );
71
+ }
72
+
73
+ protected function getWhereClauses () : string
74
+ {
75
+ return collect ($ this ->query ->wheres )->reduce (function ($ carry , $ where ) {
76
+ $ value = $ where ['value ' ] ?? implode ('_ ' , $ where ['values ' ]) ?? '' ;
77
+
78
+ return "{$ carry }- {$ where ['column ' ]}_ {$ value }" ;
79
+ }) ?: '' ;
80
+ }
81
+
82
+ protected function getWithModels () : string
83
+ {
84
+ $ eagerLoads = collect ($ this ->eagerLoad );
85
+
86
+ if ($ eagerLoads ->isEmpty ()) {
87
+ return '' ;
48
88
}
49
89
50
- return $ key ;
90
+ return ' - ' . implode ( ' - ' , $ eagerLoads -> keys ()-> toArray ()) ;
51
91
}
52
92
53
93
protected function getCacheTags () : array
0 commit comments