-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathxtremecache 1.0.6.php
193 lines (162 loc) · 5.62 KB
/
xtremecache 1.0.6.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* Serve cached pages with no request processing
* @author Salerno Simone
* @version 1.0.6
* @license MIT
*/
require __DIR__.DS.'vendor'.DS.'phpfastcache.php';
class XtremeCache extends Module {
/**
* Cache Time-To-Live in seconds
* Since cache gets cleaned quite often, use a very high value
*/
const CACHE_TTL = 999999;
/**
* Cache driver
*/
const DRIVER = 'sqlite';
/**
* Cache engine
* @var BasePhpFastCache
*/
private $fast_cache;
public function __construct() {
$this->name = 'xtremecache';
$this->tab = 'frontend_features';
$this->version = '1.0.6';
$this->author = 'Simone Salerno';
parent::__construct();
$this->displayName = $this->l('Xtreme cache');
$this->description = $this->l('Cache non-dynamic pages in the front office.');
$this->fast_cache = $this->getFastCache();
}
/**
* Handle non-explicitly handled hooks
* @param string $name hook name
* @param array $arguments
*/
public function __call($name, $arguments) {
if (0 === strpos(strtolower($name), 'hookaction')) {
$this->fast_cache->clean();
}
}
/**
* Install and register hooks
* @return bool
*/
public function install() {
return parent::install() &&
$this->registerHook('actionDispatcher') &&
$this->registerHook('actionRequestComplete') &&
$this->registerHook('actionCategoryAdd') &&
$this->registerHook('actionCategoryUpdate') &&
$this->registerHook('actionCategoryDelete') &&
$this->registerHook('actionProductAdd') &&
$this->registerHook('actionProductUpdate') &&
$this->registerHook('actionProductDelete') &&
$this->registerHook('actionProductSave') &&
$this->registerHook('actionEmptySmartyCache');
}
/**
* Uninstall and clear cache
* @return bool
*/
public function uninstall() {
//delete all cached files
$this->fast_cache->clean();
return $this->unregisterHook('actionDispatcher') &&
$this->unregisterHook('actionRequestComplete') &&
parent::uninstall();
}
/**
* Check if page exists in cache
* If it exists, serve and abort
* @param array $params
*/
public function hookActionDispatcher($params) {
if (!$this->isActive())
return;
$controller_type = $params['controller_type'];
//if front page and not in the checkout process
if ($params['controller_class'] !== 'OrderController' &&
$params['controller_class'] !== 'OrderOpcController' &&
Dispatcher::FC_FRONT === $controller_type) {
$cached = $this->fast_cache->get($this->getCacheKey());
if (NULL !== $cached) {
//empty output buffer
ob_get_clean();
die($cached);
}
}
}
/**
* Cache page content for front pages
* @param string $params
*/
public function hookActionRequestComplete($params) {
if (!$this->isActive())
return;
$controller = $params['controller'];
if (is_subclass_of($controller, 'FrontController') &&
!is_subclass_of($controller, 'OrderController') &&
!is_subclass_of($controller, 'OrderOpcController')) {
require_once(_PS_TOOL_DIR_.'minify_html'.DS.'minify_html.class.php');
$key = $this->getCacheKey();
$output = Minify_HTML::minify($params['output']);
//mark page as cached
$debugInfo = sprintf(
"<!-- served from cache with key %s [driver: %s] [generated on %s] -->",
$key,
static::DRIVER,
date('Y-m-d H:i:s')
);
$output = $debugInfo . $output;
$this->fast_cache->set($key, $output, static::CACHE_TTL);
}
}
/**
* Check if should use cache
* @return boolean
*/
private function isActive() {
//turn off on debug mode
if (_PS_MODE_DEV_ || _PS_DEBUG_PROFILING_)
return false;
//check that customer is not logged in
$customer = $this->context->customer;
if ($customer && $customer instanceof Customer && $customer->id > 0)
return false;
//for guest checkout, check that cart is empty
global $cookie;
$cart = new Cart($cookie->id_cart);
if ($cart && $cart instanceof Cart && $cart->nbProducts() > 0)
return false;
//disable on ajax and non-GET requests
$active = !Tools::getValue('ajax', false);
$active = $active && $_SERVER['REQUEST_METHOD'] === 'GET';
return $active;
}
/**
* Get cache engine
* @return BasePhpFastCache
*/
private function getFastCache() {
phpFastCache::setup('path', __DIR__.DS.'xcache');
return phpFastCache('sqlite');
}
/**
* Map url to cache key
* @return string
*/
private function getCacheKey($url=NULL) {
if ($url === NULL)
$url = $_SERVER['REQUEST_URI'];
$url = 'device-'.$this->context->getDevice().
'-lang-'.$this->context->language->id.
'-shop-'.$this->context->shop->id.'-'.
$url;
$url = md5($url);
return $url;
}
}