-
Notifications
You must be signed in to change notification settings - Fork 13
/
fpc.php
155 lines (124 loc) · 4.71 KB
/
fpc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
//Cache Entry point function
function fast_cache()
{
$time_start = microtime(true);
$request = $_SERVER;
//var_dump($_SERVER['REQUEST_URI']);
//die();
if (
!isset($request['REQUEST_METHOD']) ||
$request['REQUEST_METHOD'] !== 'GET' ||
strpos($_SERVER['REQUEST_URI'], '/customer') === 0 ||
strpos($_SERVER['REQUEST_URI'], '/media') === 0 ||
strpos($_SERVER['REQUEST_URI'], '/admin') === 0 ||
strpos($_SERVER['REQUEST_URI'], '/checkout')
) {
//use only with web not cli
if (isset($request['REQUEST_METHOD'])) {
@header('SaaS-Cache: FALSE');
}
return false;
}
// We are using native Redis we are not using Magento 2 Broken Framework
//If you don't have native Redis installed this extension will not work
if (!class_exists('\Redis')) {
// die("Redis Extension is not installed");
return false;
}
$config = require __DIR__ . '/../app/etc/env.php';
$_cache = [];
//https://github.com/phpredis/phpredis/blob/develop/INSTALL.markdown
// It works only With Redis!
if (
!isset($config['cache']['frontend']['page_cache']['backend_options']['server']) &&
!isset($config['cache']['frontend']['page_cache']['backend_options']['port'])
) {
// die("config is not done");
return false;
}
$redisLoc = new Redis();
$redisLoc->pconnect(
$config['cache']['frontend']['page_cache']['backend_options']['server'],
$config['cache']['frontend']['page_cache']['backend_options']['port'],
0,
'FPC'
);
//$redis->select($config['cache']['frontend']['page_cache']['backend_options']['database']);
//var_dump($_SERVER);
//die();
$request = $_SERVER;
try {
$prefix = 'zc:k:' . $config['cache']['frontend']['page_cache']['id_prefix'];
$cacheKey = getCacheKey($request);
//echo "\n $cacheKey";
//uncomment to enable APCU cache
$apcu_value = false; //apcu_fetch($cacheKey);
if (!isset($_cache[$cacheKey]) || $apcu_value === false) {
$page = $redisLoc->multi(Redis::PIPELINE)
->select($config['cache']['frontend']['page_cache']['backend_options']['database'])
->hget($prefix . strtoupper($cacheKey), 'd')
->exec()[1];
//var_dump($page);
if ($page === false) {
//unset($_COOKIES);
header('SaaS-Cache: MISS');
return false;
}
$arch = substr($page, 0, 2);
if ($arch === 'gz') {
$page = gzuncompress(substr($page, 5));
} else if ($arch === '{"') {
$page = $page;
} else {
return false;
}
$page = json_decode($page, true);
$_cache[$cacheKey] = $page;
// apcu_add($cacheKey, $page);
} else {
//echo "\n from APCU";
$page = $_cache[$cacheKey];
//$page = $apcu_value;
}
//echo "fast:cache";
header('SaaS-Cache: HIT');
echo $page['content'];
$time_end = microtime(true);
$time = $time_end - $time_start;
echo 'Did FPC in ' . $time . ' seconds';
die();
//return $result;
} catch (\Throwable $e) {
echo $e->__toString();
}
}
// https://github.com/magento/magento2/blob/ba89f12cb331ffe8c9b9cb952ef15eccf762329e/app/code/Magento/PageCache/etc/varnish6.vcl#L121
// https://github.com/magento/magento2/blob/9544fb243d5848a497d4ea7b88e08609376ac39e/lib/internal/Magento/Framework/App/PageCache/Identifier.php
function getCacheKey($request)
{
$data = [
isset($request['SCHEME_HTTPS']) ? true : false, //https://github.com/magento/magento2/blob/2.3/lib/internal/Magento/Framework/HTTP/PhpEnvironment/Request.php#L386
getUrl($request),
//https://github.com/magento/magento2/blob/ba89f12cb331ffe8c9b9cb952ef15eccf762329e/app/code/Magento/PageCache/etc/varnish6.vcl#L121
NULL //$request->getCookieParams('X-Magento-Vary')
//?: $this->context->getVaryString()
];
if (isset($_GET['testFPC']))
var_dump($data);
return sha1(json_encode($data));
}
function getUrl($request)
{
$query = $request['QUERY_STRING'] === '' ? '' : '?' . $request['QUERY_STRING'];
//var_dump($request);
if (isset($request['HTTP_X_FORWARDED_PROTO'])) {
$shema = $request['HTTP_X_FORWARDED_PROTO'];
} else if (isset($request['REQUEST_SCHEME'])) {
$shema = $request['REQUEST_SCHEME'];
}
//var_dump($request);
return $shema . '://' . $request['HTTP_HOST'] . '' . $request['REQUEST_URI'];
}
//Start Fast Cache
fast_cache();