18
18
class CacheResponse
19
19
{
20
20
/**
21
- * 缓存命中状态,1为命中,0为未命中
21
+ * @var \Illuminate\Http\Request
22
+ */
23
+ protected $ request ;
24
+
25
+ /**
26
+ * @var \Closure
27
+ */
28
+ protected $ next ;
29
+
30
+ /**
31
+ * 缓存分钟
22
32
*
23
- * @var int
33
+ * @var int|null
24
34
*/
25
- protected $ cache_hit = 1 ;
35
+ protected $ minutes ;
26
36
27
37
/**
28
- * 缓存Key
38
+ * 缓存数据
29
39
*
30
- * @var string
40
+ * @var array
31
41
*/
32
- protected $ cache_key ;
42
+ protected $ responseCache ;
33
43
34
44
/**
35
- * 缓存失效时间
45
+ * 缓存命中状态,1为命中,0为未命中
46
+ *
47
+ * @var int
48
+ */
49
+ protected $ cacheHit = 1 ;
50
+
51
+ /**
52
+ * 缓存Key
36
53
*
37
54
* @var string
38
55
*/
39
- protected $ cache_expire_at ;
56
+ protected $ cacheKey ;
40
57
41
58
/**
42
59
* Handle an incoming request
43
60
*
44
61
* @param \Illuminate\Http\Request $request
45
62
* @param \Closure $next
63
+ * @param int|null $minutes
46
64
*
47
65
* @return mixed
48
66
*/
49
67
public function handle ($ request , Closure $ next , $ minutes = null )
50
68
{
51
- $ responseCache = $ this ->getResponseCache ($ request , $ next , $ minutes );
69
+ $ this ->prepare ($ request , $ next , $ minutes );
52
70
53
- $ response = response ($ responseCache ['content ' ]);
71
+ $ this ->responseCache ();
72
+
73
+ $ response = response ($ this ->responseCache ['content ' ]);
54
74
55
75
return $ this ->addHeaders ($ response );
56
76
}
57
77
58
78
/**
59
- * 返回Response-Cache
79
+ * 预备
60
80
*
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
64
95
*
65
96
* @return array
66
97
*/
67
- protected function getResponseCache ( $ request , $ next , $ minutes )
98
+ protected function responseCache ( )
68
99
{
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 () {
75
104
$ this ->cacheMissed ();
76
105
77
- $ response = $ next( $ request );
106
+ $ response = ( $ this -> next )( $ this -> request );
78
107
79
108
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 ' ),
81
110
];
82
111
}
83
112
);
84
113
85
- $ this ->cache_expire_at = $ responseCache ['cacheExpireAt ' ];
86
-
87
- return $ responseCache ;
114
+ return $ this ->responseCache ;
88
115
}
89
116
90
117
/**
@@ -123,9 +150,9 @@ protected function addHeaders($response)
123
150
protected function getHeaders ()
124
151
{
125
152
$ 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 ' ] ,
129
156
];
130
157
131
158
return $ headers ;
@@ -134,13 +161,11 @@ protected function getHeaders()
134
161
/**
135
162
* 根据请求获取指定的Key
136
163
*
137
- * @param Illuminate\Http\Request $request
138
- *
139
164
* @return string
140
165
*/
141
- protected function resolveRequestKey ( Request $ request )
166
+ protected function resolveKey ( )
142
167
{
143
- return md5 ($ request ->fullUrl ());
168
+ return md5 ($ this -> request ->fullUrl ());
144
169
}
145
170
146
171
/**
@@ -174,6 +199,6 @@ protected function getDefaultMinutes()
174
199
*/
175
200
protected function cacheMissed ()
176
201
{
177
- $ this ->cache_hit = 0 ;
202
+ $ this ->cacheHit = 0 ;
178
203
}
179
204
}
0 commit comments