99
1010class Builder extends EloquentBuilder
1111{
12- protected function eagerLoadRelation (array $ models , $ name , Closure $ constraints )
12+ protected function cache (array $ tags = [] )
1313 {
14- $ relation = $ this ->getRelation ($ name );
15- $ relation ->addEagerConstraints ($ models );
16- $ constraints ($ relation );
14+ $ cache = cache ();
1715
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 ;
1921 }
2022
2123 protected function cacheResults (Relation $ relation , array $ models , string $ name ) : array
2224 {
2325 $ parentIds = implode ('_ ' , collect ($ models )->pluck ('id ' )->toArray ());
2426 $ parentName = str_slug (get_class ($ relation ->getParent ()));
2527 $ childName = str_slug (get_class ($ relation ->getRelated ()));
26- $ cache = cache ();
2728
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 (
3330 "{$ parentName }_ {$ parentIds }- {$ childName }s " ,
3431 function () use ($ relation , $ models , $ name ) {
3532 return $ relation ->match (
@@ -42,4 +39,66 @@ function () use ($relation, $models, $name) {
4239
4340 return $ cachedResults ;
4441 }
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+ }
45104}
0 commit comments