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 MIT |
12
|
|
|
* |
13
|
|
|
* LICENSE: |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21
|
|
|
* THE SOFTWARE. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
class ServerCache extends AbstractCache { |
25
|
|
|
|
26
|
|
|
// @NOTE: Server cache will not consider cacheable POST or PUT requests |
27
|
|
|
// because of dispatcher internal structure: if post request is cached |
28
|
|
|
// subsequent requests will never reach the service. |
29
|
|
|
protected static $cachable_methods = ['GET', 'HEAD']; |
30
|
|
|
|
31
|
|
|
protected static $cachable_statuses = [200, 203, 300, 301, 302, 404, 410]; |
32
|
|
|
|
33
|
|
|
protected static $cache_namespace = "DISPATCHERSERVICES"; |
34
|
|
|
|
35
|
|
|
protected $bypass = false; |
36
|
|
|
|
37
|
|
|
public function read( |
38
|
|
|
Request $request, |
39
|
|
|
Response $response |
40
|
|
|
) { |
41
|
|
|
|
42
|
|
|
if ( $this->bypass === true ) return false; |
43
|
|
|
|
44
|
|
|
$name = self::getCacheName($request); |
45
|
|
|
|
46
|
|
|
$cache_object = $this->getCache()->setNamespace(self::$cache_namespace)->get($name); |
47
|
|
|
|
48
|
|
|
if (is_null($cache_object)) return false; |
49
|
|
|
|
50
|
|
|
$response->import($cache_object); |
51
|
|
|
|
52
|
|
|
return true; |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function dump( |
57
|
|
|
Request $request, |
58
|
|
|
Response $response, |
59
|
|
|
Route $route |
60
|
|
|
) { |
61
|
|
|
|
62
|
|
|
if ( $this->bypass === true ) return; |
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
|
|
|
public function bypassCache() { |
89
|
|
|
|
90
|
|
|
$this->bypass = true; |
91
|
|
|
return $this; |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private static function getCacheName(Request $request) { |
96
|
|
|
|
97
|
|
|
return md5((string)$request->getMethod().(string)$request->getUri()); |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|