Skip to content

Commit 71ab863

Browse files
committed
修复部分bug
1 parent 1ddfcfe commit 71ab863

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/Middleware/CacheResponse.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
*/
1616
class CacheResponse
1717
{
18+
/**
19+
* 缓存命中状态,1为命中,0为未命中
20+
*
21+
* @var integer
22+
*/
23+
protected $cache_hit = 1;
24+
1825
/**
1926
* Handle an incoming request.
2027
*
@@ -44,6 +51,8 @@ protected function getResponseCache($request, $next, $minutes)
4451
$key = $this->resolveRequestKey($request);
4552

4653
return Cache::remember($key, $this->resolveMinutes($minutes), function () use ($request, $next) {
54+
$this->cacheMissed();
55+
4756
$response = $next($request);
4857

4958
return $this->resolveResponseCache($response);
@@ -70,9 +79,11 @@ protected function resolveResponseCache($response)
7079
*/
7180
protected function addHeaders($response)
7281
{
73-
return $response->headers->add(
82+
$response->headers->add(
7483
$this->getHeaders()
7584
);
85+
86+
return $response;
7687
}
7788

7889
/**
@@ -83,7 +94,7 @@ protected function addHeaders($response)
8394
protected function getHeaders()
8495
{
8596
$headers = [
86-
'X-Cache-Hit' => 1,
97+
'X-Cache-Hit' => $this->cache_hit,
8798
];
8899

89100
return $headers;
@@ -122,4 +133,14 @@ protected function getDefaultMinutes()
122133
{
123134
return 10;
124135
}
136+
137+
/**
138+
* 缓存未命中
139+
*
140+
* @return mixed
141+
*/
142+
protected function cacheMissed()
143+
{
144+
$this->cache_hit = 0;
145+
}
125146
}

0 commit comments

Comments
 (0)