1818class CacheResponse
1919{
2020 /**
21- * 缓存命中状态,1为命中,0为未命中
21+ * @var \Illuminate\Http\Request
22+ */
23+ protected $ request ;
24+
25+ /**
26+ * @var \Closure
27+ */
28+ protected $ next ;
29+
30+ /**
31+ * 缓存分钟
2232 *
23- * @var int
33+ * @var int|null
2434 */
25- protected $ cache_hit = 1 ;
35+ protected $ minutes ;
2636
2737 /**
28- * 缓存Key
38+ * 缓存数据
2939 *
30- * @var string
40+ * @var array
3141 */
32- protected $ cache_key ;
42+ protected $ responseCache ;
3343
3444 /**
35- * 缓存失效时间
45+ * 缓存命中状态,1为命中,0为未命中
46+ *
47+ * @var int
48+ */
49+ protected $ cacheHit = 1 ;
50+
51+ /**
52+ * 缓存Key
3653 *
3754 * @var string
3855 */
39- protected $ cache_expire_at ;
56+ protected $ cacheKey ;
4057
4158 /**
4259 * Handle an incoming request
4360 *
4461 * @param \Illuminate\Http\Request $request
4562 * @param \Closure $next
63+ * @param int|null $minutes
4664 *
4765 * @return mixed
4866 */
4967 public function handle ($ request , Closure $ next , $ minutes = null )
5068 {
51- $ responseCache = $ this ->getResponseCache ($ request , $ next , $ minutes );
69+ $ this ->prepare ($ request , $ next , $ minutes );
5270
53- $ response = response ($ responseCache ['content ' ]);
71+ $ this ->responseCache ();
72+
73+ $ response = response ($ this ->responseCache ['content ' ]);
5474
5575 return $ this ->addHeaders ($ response );
5676 }
5777
5878 /**
59- * 返回Response-Cache
79+ * 预备
6080 *
61- * @param \Illuminate\Http\Request $request
62- * @param Closure $next
63- * @param int|null $minutes
81+ * @return mixed
82+ */
83+ protected function prepare ($ request , Closure $ next , $ minutes = null )
84+ {
85+ $ this ->request = $ request ;
86+ $ this ->next = $ next ;
87+
88+ // 初始化值
89+ $ this ->cacheKey = $ this ->resolveKey ();
90+ $ this ->minutes = $ this ->resolveMinutes ($ minutes );
91+ }
92+
93+ /**
94+ * 生成或读取Response-Cache
6495 *
6596 * @return array
6697 */
67- protected function getResponseCache ( $ request , $ next , $ minutes )
98+ protected function responseCache ( )
6899 {
69- $ this ->cache_key = $ key = $ this ->resolveRequestKey ($ request );
70-
71- $ responseCache = Cache::remember (
72- $ key ,
73- $ resolveMinutes = $ this ->resolveMinutes ($ minutes ),
74- function () use ($ request , $ next , $ resolveMinutes ) {
100+ $ this ->responseCache = Cache::remember (
101+ $ this ->cacheKey ,
102+ $ this ->minutes ,
103+ function () {
75104 $ this ->cacheMissed ();
76105
77- $ response = $ next( $ request );
106+ $ response = ( $ this -> next )( $ this -> request );
78107
79108 return $ this ->resolveResponseCache ($ response ) + [
80- 'cacheExpireAt ' => Carbon::now ()->addMinutes ($ resolveMinutes )->format ('Y-m-d\TH :i:s ' ),
109+ 'cacheExpireAt ' => Carbon::now ()->addMinutes ($ this -> minutes )->format ('Y-m-d H :i:s T ' ),
81110 ];
82111 }
83112 );
84113
85- $ this ->cache_expire_at = $ responseCache ['cacheExpireAt ' ];
86-
87- return $ responseCache ;
114+ return $ this ->responseCache ;
88115 }
89116
90117 /**
@@ -123,9 +150,9 @@ protected function addHeaders($response)
123150 protected function getHeaders ()
124151 {
125152 $ headers = [
126- 'X-Cache ' => $ this ->cache_hit ? 'Hit ' : 'Missed ' ,
127- 'X-Cache-Key ' => $ this ->cache_key ,
128- 'X-Cache-ExpireAt ' => $ this ->cache_expire_at ,
153+ 'X-Cache ' => $ this ->cacheHit ? 'Hit from cache ' : 'Missed ' ,
154+ 'X-Cache-Key ' => $ this ->cacheKey ,
155+ 'X-Cache-Expires ' => $ this ->responseCache [ ' cacheExpireAt ' ] ,
129156 ];
130157
131158 return $ headers ;
@@ -134,13 +161,11 @@ protected function getHeaders()
134161 /**
135162 * 根据请求获取指定的Key
136163 *
137- * @param Illuminate\Http\Request $request
138- *
139164 * @return string
140165 */
141- protected function resolveRequestKey ( Request $ request )
166+ protected function resolveKey ( )
142167 {
143- return md5 ($ request ->fullUrl ());
168+ return md5 ($ this -> request ->fullUrl ());
144169 }
145170
146171 /**
@@ -174,6 +199,6 @@ protected function getDefaultMinutes()
174199 */
175200 protected function cacheMissed ()
176201 {
177- $ this ->cache_hit = 0 ;
202+ $ this ->cacheHit = 0 ;
178203 }
179204}
0 commit comments