1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
4
|
|
|
* @license https://github.com/flipbox/relay-simplecache/blob/master/LICENSE |
5
|
|
|
* @link https://github.com/flipbox/relay-simplecache |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Flipbox\Relay\Middleware; |
9
|
|
|
|
10
|
|
|
use Flipbox\Http\Stream\Factory as StreamFactory; |
11
|
|
|
use Psr\Http\Message\RequestInterface; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
use Psr\Http\Message\StreamInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Flipbox Factory <[email protected]> |
17
|
|
|
* @since 1.0.0 |
18
|
|
|
*/ |
19
|
|
|
class SimpleCache extends AbstractSimpleCache |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @inheritdoc |
23
|
|
|
*/ |
24
|
|
|
public function __invoke( |
25
|
|
|
RequestInterface $request, |
26
|
|
|
ResponseInterface $response, |
27
|
|
|
callable $next = null |
28
|
|
|
): ResponseInterface { |
29
|
|
|
parent::__invoke($request, $response); |
30
|
|
|
|
31
|
|
|
$key = $this->getCacheKey($request); |
32
|
|
|
|
33
|
|
|
$value = $this->cache->get($key); |
34
|
|
|
|
35
|
|
|
// If it's cached |
36
|
|
|
if ($value !== null) { |
37
|
|
|
return $this->applyCacheToResponseBody($key, $response, $value); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->info( |
41
|
|
|
"Item not found in cache. [key: {key}, type: {type}]", |
42
|
|
|
[ |
43
|
|
|
'type' => get_class($this->cache), |
44
|
|
|
'key' => $key |
45
|
|
|
] |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
/** @var ResponseInterface $response */ |
49
|
|
|
$response = $next($request, $response); |
50
|
|
|
|
51
|
|
|
// Only cache successful responses |
52
|
|
|
if ($this->isResponseSuccessful($response)) { |
53
|
|
|
$this->cacheResponse($key, $response); |
54
|
|
|
} else { |
55
|
|
|
$this->info( |
56
|
|
|
"Did not save to cache because request was unsuccessful. [key: {key}, statusCode: {statusCode}]", |
57
|
|
|
[ |
58
|
|
|
'key' => $key, |
59
|
|
|
'statusCode' => $response->getStatusCode() |
60
|
|
|
] |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
return $response; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $key |
68
|
|
|
* @param ResponseInterface $response |
69
|
|
|
* @param mixed $value |
70
|
|
|
* @return ResponseInterface |
71
|
|
|
* @throws \Flipbox\Http\Stream\Exceptions\InvalidStreamException |
72
|
|
|
*/ |
73
|
|
|
protected function applyCacheToResponseBody(string $key, ResponseInterface $response, $value) |
74
|
|
|
{ |
75
|
|
|
$this->info( |
76
|
|
|
"Item found in cache. [key: {key}, type: {type}]", |
77
|
|
|
[ |
78
|
|
|
'type' => get_class($this->cache), |
79
|
|
|
'key' => $key |
80
|
|
|
] |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
return $response->withBody( |
84
|
|
|
StreamFactory::create($value) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $key |
90
|
|
|
* @param ResponseInterface $response |
91
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
92
|
|
|
*/ |
93
|
|
|
protected function cacheResponse(string $key, ResponseInterface $response) |
94
|
|
|
{ |
95
|
|
|
/** @var StreamInterface $body */ |
96
|
|
|
$body = $response->getBody(); |
97
|
|
|
|
98
|
|
|
$this->cache->set($key, $body->getContents()); |
99
|
|
|
|
100
|
|
|
$body->rewind(); |
101
|
|
|
|
102
|
|
|
$this->info( |
103
|
|
|
"Save item to cache. [key: {key}, type: {type}]", |
104
|
|
|
[ |
105
|
|
|
'type' => get_class($this->cache), |
106
|
|
|
'key' => $key |
107
|
|
|
] |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|