-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathREADME
More file actions
executable file
·232 lines (146 loc) · 7.27 KB
/
Copy pathREADME
File metadata and controls
executable file
·232 lines (146 loc) · 7.27 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# isicsHttpCachePlugin #
Excited by **Symfony2 Http Cache** but still have heavy load projects to maintain under Symfony 1.4?
Inspired by Symfony2 (concepts and code), this plugin is for you!
isicsHttpCachePlugin fits to our (basic) actual requirements and doesn't already provides all HTTP cache mechanisms. But feel free to contribute!
It comes with :
* 1 new method for sfWebRequest:
* `getETags()`
* 4 new methods for sfWebResponse:
* `isNotModified(sfWebRequest $request)`
* `setETag($etag)`
* `setLastModified($date)`
* `setMaxAge($duration)`
* ESI support via 2 new helpers:
* `include_component_esi($module_name, $component_name, $vars)`
* `include_partial_esi($template_name, $vars)`
* An invalidation method: `isicsHttpCacheService::invalidate($url_pattern, $method)`
_The new methods are unit tested._
## Benchmark ##
We've made a first benchmark with an homepage between 2 versions with exactly the same cache strategy:
* a classical version using Symfony cache (optimized, 0 SQL queries)
6 cached components with different TTL and so no cache for action
* a version using Varnish
TTL for action and 7 ESI tags (1 userbar without caching and the 6 others with different TTL)
Symfony is still called at each request but only for userbar.
The benchmark was made with Siege tool.
It shows a 10 factor between Varnish and Symfony cache (_400 req/sec vs 40_).
## Installation ##
Install the plugin:
$ symfony plugin:install -stability=beta isicsHttpCachePlugin
Enable the plugin in your `/config/ProjectConfiguration.class.php`:
[php]
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
$this->enablePlugins(array(
...
'isicsHttpCachePlugin',
));
}
}
For ESI support, enable module and helpers in your application(s) `settings.yml`:
[yml]
all:
.settings:
standard_helpers: [..., isicsHttpCache]
enabled_modules: [..., isicsHttpCacheESI]
## ESI (Edge Side Includes) support ##
The new helpers `include_component_esi`, `include_partial_esi` are very similar to Symfony ones.
If ESI are enabled (true by default in prod env) and only if a gateway handling ESI is installed (we detect that thanks to a dedicated header), an ESI tag is rendered rather than the content.
Rendering the content is the role of a special action.
Else, the content is direclty rendered exactly like with the Symfony helpers.
As of Symfony2, cache expiration and validation mechanisms are supported.
Both helpers accept an optional `cache` argument.
The value has a mixed type:
* For expiration, simply give a duration in seconds:
[php]
<?php include_component_esi('news', 'showLatest', array('limit' => 5, 'cache' => 300)) ?>
In this example, the latest news component will be refresh every 5 minutes.
* For validation, the argument value must be a string.
[php]
<?php include_component_esi('news', 'showLatest', array('limit' => 5, 'cache' => 'NewsService') ?>
It corresponds to a class name that must defines one or both static methods `getLastModified()` and `getETag()`:
[php]
<?php
static public function getLastModified($vars)
{
return NewsPeer::getLastModified();
}
and/or :
[php]
<?php
static public function getETag($vars)
{
return NewsPeer::computeETag();
}
Note the `$vars` argument. It is the vars you passed to the helpers. Use it as you like ;)
## Invalidation ##
The method `isicsHttpCachePlugin::invalidate()` gives you ability to invalidate one or more url.
It uses HTTP BAN and PURGE requests.
Prefer PURGE to clear one url cache and BAN to clear many url thanks to regexp.
Note that you have to setup correctly your reverse proxy to handle this requests.
For Varnish, you can use those examples: https://www.varnish-cache.org/docs/3.0/tutorial/purging.html
### Task ###
You can test this tool in CLI with a dedicated task:
* To clear one url (PURGE method implied):
$ symfony isics-http-cache:invalidate http://www.mydomain.tld/
This will clear homepage cache.
* Or:
$ symfony isics-http-cache:invalidate --method=BAN http://www.mydomain.tld/.*
This will clear all the pages under this host.
### Custom HTTP headers ###
The plugin adds custom HTTP headers to ESI components response in order to facilitate granular cache invalidation.
* x-symfony-viewname : This header can be used to invalidate cache objects when a component / partial template has been modified. Invalidation example (varnish) :
ban obj.http.x-symfony-viewname == product/someview
* x-docuri : This header can be used to invalidate cache objects when a document attributes have changed in datastore. Header value is identical to component variable $_docUri. Invalidation example (varnish) :
ban obj.http.x-docuri == product/123
* x-guid : This header can be used to invalidate the cache object corresponding to the component / partial currently executed. Header value is identical to component variable $_guid. If it is not set, it is automatically generated using uniqid(). Invalidation example (varnish) :
ban obj.http.x-guid == the_guid
### Varnish admin connection
A class ripped from https://github.com/timwhitlock/php-varnish to ease cache invalidation from PHP:
<?php
$host = sfConfig::get('app_varnish_host');
$port = sfConfig::get('app_varnish_port');
$secret = sfConfig::get('app_varnish_secret');
if ($host == '' || $port == '' || $secret == '')
{
throw new Exception('Impossible to configure Varnish Socket! Are you\'re app.yml file well configured?');
}
$socket = new VarnishAdminSocket($host, $port);
$socket->set_auth($secret."\n");
$socket->connect();
$socket->purge('obj.http.x-docuri == myx-docuri-which-goes-well');
$socket->quit();
### Filter ###
A symfony filter can be added in your apps/[apps]/config/filters.yml file between cache and execution filters:
esi_ui:
class: isicsHttpCacheEsiUiFilter
## Configuration ##
By default, ESI are enabled in prod env.
Although, you should have to add the IP of your gateway.
This can be done in your application(s) `app.yml`:
[yml]
all:
isics_http_cache_plugin:
esi:
enabled: false
allowed_ips: [127.0.0.1]
### Varnish configuration ###
Exactly like Symfony2, you have to setup Varnish to add a particular header telling that ESI are supported:
sub vcl_recv {
set req.http.Surrogate-Capability = "abc=ESI/1.0";
}
sub vcl_fetch {
if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
set beresp.do_esi = true;
unset beresp.http.Surrogate-Control;
}
}
## Recommandations ##
* Do not give large vars to ESI helpers since the vars are serialized and passed to the internal action on the url.
## Known issues ##
* Slots are broken when using ESI.
## Roadmap ##
* Try to make ESI render as fast as possible.
* Write functional tests (not so easy with plugins).