1
|
|
|
<?php namespace Comodojo\Dispatcher\Cache; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Dispatcher\Request\Model as Request; |
4
|
|
|
use \Comodojo\Dispatcher\Router\Route; |
5
|
|
|
use \Comodojo\Dispatcher\Response\Model as Response; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Server-cache handler |
9
|
|
|
* |
10
|
|
|
* This class handles the process to read/write response content in the cache |
11
|
|
|
* |
12
|
|
|
* @NOTE: Server cache will not consider cacheable POST or PUT requests |
13
|
|
|
* because of dispatcher internal structure: if post request is cached |
14
|
|
|
* subsequent requests will never reach the service. |
15
|
|
|
* |
16
|
|
|
* @package Comodojo Dispatcher |
17
|
|
|
* @author Marco Giovinazzi <[email protected]> |
18
|
|
|
* @author Marco Castiello <[email protected]> |
19
|
|
|
* @license MIT |
20
|
|
|
* |
21
|
|
|
* LICENSE: |
22
|
|
|
* |
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
29
|
|
|
* THE SOFTWARE. |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
class ServerCache extends AbstractCache { |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array $cachable_methods |
36
|
|
|
* Methods that allow cache usage |
37
|
|
|
*/ |
38
|
|
|
protected static $cachable_methods = ['GET', 'HEAD']; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array $cachable_statuses |
42
|
|
|
* List of HTTP return codes that allow cache usage |
43
|
|
|
*/ |
44
|
|
|
protected static $cachable_statuses = [200, 203, 300, 301, 302, 404, 410]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string $cache_namespace |
48
|
|
|
* Server-cache namespace |
49
|
|
|
*/ |
50
|
|
|
protected static $cache_namespace = "DISPATCHERSERVICES"; |
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var bool $bypass |
54
|
|
|
* In case true, cache will be ignored (cache bypass) |
55
|
|
|
*/ |
56
|
|
|
protected $bypass = false; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Read the cached response starting from the request definition (if any) |
60
|
|
|
* |
61
|
|
|
* @param Request $request |
62
|
|
|
* The request model |
63
|
|
|
* @param Response $response |
64
|
|
|
* The response object that will be hydrated in case of success |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
1 |
|
public function read( |
68
|
|
|
Request $request, |
69
|
|
|
Response $response |
70
|
|
|
) { |
71
|
|
|
|
72
|
1 |
|
if ($this->bypass === true) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$name = self::getCacheName($request); |
77
|
1 |
|
$cache_object = $this->getCache() |
78
|
1 |
|
->setNamespace(self::$cache_namespace) |
79
|
1 |
|
->get($name); |
80
|
|
|
|
81
|
1 |
|
if (is_null($cache_object)) { |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
$response->import($cache_object); |
86
|
1 |
|
return true; |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Dump the full request object in the cache |
92
|
|
|
* |
93
|
|
|
* @param Request $request |
94
|
|
|
* The request model (to extract cache definition) |
95
|
|
|
* @param Response $response |
96
|
|
|
* The response object that will be cached |
97
|
|
|
* @param Route $route |
98
|
|
|
* The route model (to extract cache parameters) |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
2 |
|
public function dump( |
102
|
|
|
Request $request, |
103
|
|
|
Response $response, |
104
|
|
|
Route $route |
105
|
|
|
) { |
106
|
|
|
|
107
|
2 |
|
if ( $this->bypass === true ) { |
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
$cache = strtoupper($route->getParameter('cache')); |
112
|
2 |
|
$ttl = $route->getParameter('ttl'); |
113
|
2 |
|
$name = self::getCacheName($request); |
114
|
2 |
|
$method = (string)$request->getMethod(); |
115
|
2 |
|
$status = $response->getStatus()->get(); |
116
|
|
|
|
117
|
|
|
if ( |
118
|
2 |
|
($cache == 'SERVER' || $cache == 'BOTH') && |
|
|
|
|
119
|
2 |
|
in_array($method, self::$cachable_methods) && |
120
|
2 |
|
in_array($status, self::$cachable_statuses) |
121
|
2 |
|
) { |
122
|
1 |
|
$this->getCache() |
123
|
1 |
|
->setNamespace(self::$cache_namespace) |
124
|
1 |
|
->set( |
125
|
1 |
|
$name, |
126
|
1 |
|
$response->export(), |
127
|
1 |
|
$ttl === null ? self::DEFAULTTTL : intval($ttl) |
128
|
1 |
|
); |
129
|
1 |
|
return true; |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
return false; |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Setup the cache bypass mode |
138
|
|
|
* |
139
|
|
|
* @return AbstractCache |
140
|
|
|
*/ |
141
|
|
|
public function bypassCache() { |
142
|
|
|
|
143
|
|
|
$this->bypass = true; |
144
|
|
|
return $this; |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Extract and compute the cache object name |
150
|
|
|
* |
151
|
|
|
* @param Request $request |
152
|
|
|
* The request model |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
2 |
|
private static function getCacheName(Request $request) { |
156
|
|
|
|
157
|
2 |
|
return md5((string)$request->getMethod().(string)$request->getUri()); |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.