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
|
|
|
* @package Comodojo Dispatcher |
9
|
|
|
* @author Marco Giovinazzi <[email protected]> |
10
|
|
|
* @author Marco Castiello <[email protected]> |
11
|
|
|
* @license GPL-3.0+ |
12
|
|
|
* |
13
|
|
|
* LICENSE: |
14
|
|
|
* |
15
|
|
|
* This program is free software: you can redistribute it and/or modify |
16
|
|
|
* it under the terms of the GNU Affero General Public License as |
17
|
|
|
* published by the Free Software Foundation, either version 3 of the |
18
|
|
|
* License, or (at your option) any later version. |
19
|
|
|
* |
20
|
|
|
* This program is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU Affero General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* You should have received a copy of the GNU Affero General Public License |
26
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class ServerCache extends AbstractCache { |
31
|
|
|
|
32
|
|
|
// @NOTE: Server cache will not consider cacheable POST or PUT requests |
33
|
|
|
// because of dispatcher internal structure: if post request is cached |
34
|
|
|
// subsequent requests will never reach the service. |
35
|
|
|
protected static $cachable_methods = ['GET', 'HEAD']; |
36
|
|
|
|
37
|
|
|
protected static $cachable_statuses = [200, 203, 300, 301, 302, 404, 410]; |
38
|
|
|
|
39
|
|
|
protected static $cache_namespace = "DISPATCHERSERVICES"; |
40
|
|
|
|
41
|
|
|
public function read( |
42
|
|
|
Request $request, |
43
|
|
|
Response $response |
44
|
|
|
) { |
45
|
|
|
|
46
|
|
|
$name = self::getCacheName($request); |
47
|
|
|
|
48
|
|
|
$cache_object = $this->getCache()->setNamespace(self::$cache_namespace)->get($name); |
49
|
|
|
|
50
|
|
|
if ( is_null($cache_object) ) return false; |
51
|
|
|
|
52
|
|
|
$response->import($cache_object); |
53
|
|
|
|
54
|
|
|
return true; |
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function dump( |
59
|
|
|
Request $request, |
60
|
|
|
Response $response, |
61
|
|
|
Route $route |
62
|
|
|
) { |
63
|
|
|
|
64
|
|
|
$cache = strtoupper($route->getParameter('cache')); |
65
|
|
|
|
66
|
|
|
$ttl = $route->getParameter('ttl'); |
67
|
|
|
|
68
|
|
|
$name = self::getCacheName($request); |
69
|
|
|
|
70
|
|
|
$method = (string) $request->getMethod(); |
71
|
|
|
|
72
|
|
|
$status = $response->getStatus()->get(); |
73
|
|
|
|
74
|
|
|
if ( |
75
|
|
|
( $cache == 'SERVER' || $cache == 'BOTH' ) && |
76
|
|
|
in_array($method, self::$cachable_methods) && |
77
|
|
|
in_array($status, self::$cachable_statuses) |
78
|
|
|
){ |
79
|
|
|
|
80
|
|
|
$this->getCache() |
81
|
|
|
->setNamespace(self::$cache_namespace) |
82
|
|
|
->set($name, $response->export(), $ttl === null ? self::DEFAULTTTL : intval($ttl)); |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private static function getCacheName(Request $request) { |
89
|
|
|
|
90
|
|
|
return md5( (string) $request->getMethod() . (string) $request->getUri() ); |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|