9
9
10
10
class Builder extends EloquentBuilder
11
11
{
12
- protected function eagerLoadRelation (array $ models , $ name , Closure $ constraints )
12
+ protected function cache (array $ tags = [] )
13
13
{
14
- $ relation = $ this ->getRelation ($ name );
15
- $ relation ->addEagerConstraints ($ models );
16
- $ constraints ($ relation );
14
+ $ cache = cache ();
17
15
18
- return $ this ->cacheResults ($ relation , $ models , $ name );
16
+ if (is_subclass_of ($ cache ->getStore (), TaggableStore::class)) {
17
+ $ cache = $ cache ->tags ($ tags );
18
+ }
19
+
20
+ return $ cache ;
19
21
}
20
22
21
23
protected function cacheResults (Relation $ relation , array $ models , string $ name ) : array
22
24
{
23
25
$ parentIds = implode ('_ ' , collect ($ models )->pluck ('id ' )->toArray ());
24
26
$ parentName = str_slug (get_class ($ relation ->getParent ()));
25
27
$ childName = str_slug (get_class ($ relation ->getRelated ()));
26
- $ cache = cache ();
27
28
28
- if (is_subclass_of ($ cache ->getStore (), TaggableStore::class)) {
29
- $ cache = $ cache ->tags ([$ parentName , $ childName ]);
30
- }
31
-
32
- $ cachedResults = $ cache ->rememberForever (
29
+ $ cachedResults = $ this ->cache ([$ parentName , $ childName ])->rememberForever (
33
30
"{$ parentName }_ {$ parentIds }- {$ childName }s " ,
34
31
function () use ($ relation , $ models , $ name ) {
35
32
return $ relation ->match (
@@ -42,4 +39,66 @@ function () use ($relation, $models, $name) {
42
39
43
40
return $ cachedResults ;
44
41
}
42
+
43
+ protected function eagerLoadRelation (array $ models , $ name , Closure $ constraints )
44
+ {
45
+ $ relation = $ this ->getRelation ($ name );
46
+ $ relation ->addEagerConstraints ($ models );
47
+ $ constraints ($ relation );
48
+
49
+ return $ this ->cacheResults ($ relation , $ models , $ name );
50
+ }
51
+
52
+ /**
53
+ * @SuppressWarnings(PHPMD.ShortVariable)
54
+ */
55
+ public function find ($ id , $ columns = ['* ' ])
56
+ {
57
+ $ tag = str_slug (get_class ($ this ->model ));
58
+ $ key = "{$ tag }_ {$ id }_ " . implode ('_ ' , $ columns );
59
+
60
+ return $ this ->cache ([$ tag ])
61
+ ->rememberForever ($ key , function () use ($ id , $ columns ) {
62
+ return parent ::find ($ id , $ columns );
63
+ });
64
+ }
65
+
66
+ public function count ($ columns = '* ' )
67
+ {
68
+ $ tag = str_slug (get_class ($ this ->model ));
69
+ $ key = "{$ tag }_ " . implode ('_ ' , $ columns );
70
+
71
+ return $ this ->cache ([$ tag ])
72
+ ->rememberForever ($ key , function () use ($ id , $ columns ) {
73
+ return parent ::count ($ columns );
74
+ });
75
+ }
76
+
77
+ public function first ($ columns = ['* ' ])
78
+ {
79
+ $ tag = str_slug (get_class ($ this ->model ));
80
+ $ key = "{$ tag }_ " . implode ('_ ' , $ columns );
81
+
82
+ return $ this ->cache ([$ tag ])
83
+ ->rememberForever ($ key , function () use ($ id , $ columns ) {
84
+ return parent ::first ($ columns );
85
+ });
86
+ }
87
+
88
+ public function get ($ columns = ['* ' ])
89
+ {
90
+ $ tag = str_slug (get_class ($ this ->model ));
91
+ $ key = "{$ tag }_ " . implode ('_ ' , $ columns );
92
+ $ key .= collect ($ this ->query ->wheres )->reduce (function ($ carry , $ where ) {
93
+ $ value = $ where ['value ' ] ?? implode ('_ ' , $ where ['values ' ]) ?? '' ;
94
+
95
+ return "{$ carry }- {$ where ['column ' ]}_ {$ value }" ;
96
+ });
97
+ $ key .= '- ' . implode ('- ' , collect ($ this ->eagerLoad )->keys ()->toArray ());
98
+
99
+ return $ this ->cache ([$ tag ])
100
+ ->rememberForever ($ key , function () use ($ columns ) {
101
+ return parent ::get ($ columns );
102
+ });
103
+ }
45
104
}
0 commit comments