1
1
The Cache Invalidator
2
- =================
2
+ =====================
3
3
4
- Use the CacheInvalidator to explicitly invalidate or refresh paths, URLs or
5
- headers.
4
+ Use the cache invalidator to invalidate or refresh paths, URLs and headers.
5
+ It is the invalidator that you will probably use most when interacting with
6
+ the library.
6
7
7
8
* [ Setup] ( #setup )
8
- * [ Invalidating paths and URLs] ( #invalidating-paths-and-urls )
9
- * [ Refreshing paths and URLs] ( #refreshing-paths-and-urls )
9
+ * [ Invalidating Paths and URLs] ( #invalidating-paths-and-urls )
10
+ * [ Refreshing Paths and URLs] ( #refreshing-paths-and-urls )
10
11
* [ Invalidating with a Regular Expression] ( #invalidating-with-a-regular-expression )
12
+ * [ URL, Content Type and Hostname] ( #urls-content-type-and-hostname )
13
+ * [ Any Header] ( #any-header )
11
14
* [ Tags] ( #tags )
15
+ * [ Custom Tags Header] ( #custom-tags-header )
12
16
* [ Flushing] ( #flushing )
13
17
* [ Error handling] ( #error-handling )
18
+ * [ Logging errors] ( #logging-errors )
14
19
15
20
Setup
16
21
-----
17
22
18
- The CacheInvalidator wraps a low-level caching proxy client. To construct the
19
- invalidator, pass the proxy client to it. For instance, when using [ Varnish ] ( varnish.md ) :
23
+ Create the cache invalidator by passing a [ proxy client] ( proxy-clients.md ) as
24
+ an [ adapter ] ( http://en.wikipedia.org/wiki/Adapter_pattern ) :
20
25
21
26
``` php
22
- use FOS\HttpCache\Invalidation\Varnish;
23
27
use FOS\HttpCache\CacheInvalidator;
28
+ use FOS\HttpCache\Invalidation;
24
29
25
- $varnish = new Varnish(...);
26
- $cacheInvalidator = new CacheInvalidator($varnish);
30
+ $client = new Invalidation\Varnish();
31
+ // or
32
+ $client = new Invalidation\Nginx();
33
+
34
+ $cacheInvalidator = new CacheInvalidator($client);
27
35
```
28
36
29
- Invalidating paths and URLs
37
+ Invalidating Paths and URLs
30
38
---------------------------
31
39
32
- Make sure to configure your proxy for purging first.
33
- (See [ varnish] ( varnish.md#purge ) .)
40
+ Make sure to [ configure your proxy] ( proxy-configuration.md ) for purging first.
34
41
35
42
Invalidate a path:
36
43
37
44
``` php
38
- $cacheInvalidator->invalidatePath('/users');
45
+ $cacheInvalidator->invalidatePath('/users')
46
+ ->flush()
47
+ ;
39
48
```
40
49
41
- Invalidate an URL:
50
+ See below for the [ flush] ( #flushing ) method.
51
+
52
+ Invalidate a URL:
53
+
42
54
``` php
43
- $cacheInvalidator->invalidatePath('http://www.example.com/users');
55
+ $cacheInvalidator->invalidatePath('http://www.example.com/users')->flush() ;
44
56
```
45
57
46
- Refreshing paths and URLs
58
+ Refreshing Paths and URLs
47
59
-------------------------
48
60
49
- Make sure to configure your proxy for refreshing first.
50
- (See [ varnish ] ( varnish.md#refresh ) .)
61
+ Make sure to [ configure your proxy] ( proxy-configuration.md ) for refreshing
62
+ first.
51
63
52
64
Refresh a path:
53
65
54
66
``` php
55
- $cacheInvalidator->refreshPath('/users');
67
+ $cacheInvalidator->refreshPath('/users')->flush() ;
56
68
```
57
69
58
- Refresh an URL:
70
+ Refresh a URL:
59
71
60
72
``` php
61
- $cacheInvalidator->refreshPath('http://www.example.com/users');
73
+ $cacheInvalidator->refreshPath('http://www.example.com/users')->flush() ;
62
74
```
63
75
64
- Invalidating a path with a Regular Expression
65
- ---------------------------------------------
76
+ Invalidating With a Regular Expression
77
+ --------------------------------------
78
+
79
+ Make sure to [ configure your proxy] ( proxy-configuration.md ) for banning first.
66
80
67
- Make sure to configure your proxy for regular expressions first.
68
- (See [ varnish ban] ( varnish.md#ban ) .)
81
+ ### URL, Content Type and Hostname
69
82
70
83
You can invalidate all URLs matching a regular expression by using the
71
84
` invalidateRegex ` method. You can further limit the cache entries to invalidate
72
- with a regular expression for the content type and/or the host name .
85
+ with a regular expression for the content type and/or the application hostname .
73
86
74
- For instance, to invalidate all .css files for all host names handled by this
87
+ For instance, to invalidate all .css files for all hostnames handled by this
75
88
caching proxy:
76
89
77
90
``` php
78
- $cacheInvalidator->invalidateRegex('.*css$');
91
+ $cacheInvalidator->invalidateRegex('.*css$')->flush() ;
79
92
```
80
93
81
- To invalidate all png files for host example.com:
94
+ To invalidate all . png files for host example.com:
82
95
83
96
``` php
84
- $cacheInvalidator->invalidateRegex('.*', 'image/png', array('example.com'));
97
+ $cacheInvalidator
98
+ ->invalidateRegex('.*', 'image/png', array('example.com'))
99
+ ->flush()
100
+ ;
85
101
```
86
102
87
- If you need other criteria than path, content type and hosts, use the
88
- ` invalidate ` method.
89
-
90
- Invalidating requests with any headers
91
- --------------------------------------
103
+ ### Any Header
92
104
93
105
You can also invalidate the cache based on any headers. If you use non-default
94
- headers, make sure to configure your proxy accordingly to have them taken into
95
- account. (See [ varnish ban ] ( varnish.md#ban ) .)
106
+ headers, make sure to [ configure your proxy accordingly] ( proxy-configuration.md )
107
+ to have them taken into account.
96
108
97
109
Cache client implementations should fill up the headers to at least have the
98
110
default headers always present to simplify the cache configuration rules.
99
111
100
112
To invalidate on a custom header X-My-Header, you would do:
101
113
102
114
``` php
103
- $cacheInvalidator->invalidate(array('X-My-Header' => 'my-value'));
104
- ```
105
-
106
- Fluent interface
107
- ----------------
108
-
109
- The cache invalidator offers a fluent interface:
110
-
111
- ``` php
112
- $cacheInvalidator
113
- ->invalidatePath('/bad/guys')
114
- ->invalidatePath('/good/guys')
115
- ->refreshPath('/')
116
- ;
115
+ $cacheInvalidator->invalidate(array('X-My-Header' => 'my-value'))->flush();
117
116
```
118
117
119
118
Tags
120
119
----
121
120
122
- Make sure to [ configure your proxy for tagging] ( varnish.md#tagging ) first.
123
- The examples in this section assume you left the ` tagsHeader ` unchanged. You
124
- can call ` CacheInvalidator::setTagsHeader ` to change the HTTP header used to
125
- identify tags.
121
+ Make sure to [ configure your proxy for tagging] ( proxy-configuration.md ) first.
126
122
127
123
You will have to make sure your web application adds the correct tags on all
128
- responses. The [ HttpCacheBundle ] ( https://github.com/FriendsOfSymfony/FOSHttpCacheBundle )
129
- provides means to help you with this, but without Symfony there is no generic
130
- way to do this .
124
+ responses by setting the ` X-Cache-Tags ` header. The
125
+ [ FOSHttpCacheBundle ] ( https://github.com/FriendsOfSymfony/FOSHttpCacheBundle )
126
+ does this for you when you’re using Symfony .
131
127
132
- Assume you sent 3 responses:
128
+ Assume you sent four responses:
133
129
134
- * /one had the header ` X-Cache-Tags: tag-one `
135
- * /two had the header ` X-Cache-Tags: tag-two, group-a `
136
- * /three had the header ` X-Cache-Tags: tag-three, group-a `
137
- * /four had the header ` X-Cache-Tags: tag-four, group-b `
130
+ * ` /one ` had the header ` X-Cache-Tags: tag-one `
131
+ * ` /two ` had the header ` X-Cache-Tags: tag-two, group-a `
132
+ * ` /three ` had the header ` X-Cache-Tags: tag-three, group-a `
133
+ * ` /four ` had the header ` X-Cache-Tags: tag-four, group-b `
138
134
139
135
You can now invalidate some URLs using tags:
140
136
141
137
``` php
142
- $cacheInvalidator->invalidateTags(array('group-a', 'tag-four'));
138
+ $cacheInvalidator->invalidateTags(array('group-a', 'tag-four'))->flush() ;
143
139
```
144
140
145
- This will ban all requests having either the tag group-a OR tag-four. In the
146
- above example, this will invalidate " /two", " /three" and " /four" . Only " /one"
141
+ This will ban all requests having either the tag ` group-a ` /or/ ` tag-four ` . In
142
+ the above example, this will invalidate ` /two ` , ` /three ` and ` /four ` . Only ` /one `
147
143
will stay in the cache.
148
144
145
+ ### Custom Tags Header
146
+
147
+ Tagging uses a custom HTTP header to identify tags. You can change the default
148
+ header ` X-Cache-Tags ` by calling ` setTagsHeader() ` . Make sure to reflect this
149
+ change in your [ caching proxy configuration] ( varnish-configuration.md#tagging ) .
150
+
149
151
Flushing
150
152
--------
151
153
@@ -160,16 +162,13 @@ $cacheInvalidator
160
162
;
161
163
```
162
164
163
- Note: When using the Symfony Bundle, the cache invalidator is automatically
164
- flushed. When using the Bundle, you only need to manually call flush when not
165
- in a request context. (E.g. from a command.)
165
+ Try delaying flush until after the response has been sent to the client’s
166
+ browser. This keeps the performance impact of sending invalidation requests to
167
+ a minimum.
166
168
167
- To keep the performance impact of sending invalidation requests to a minimum,
168
- make sure to only flush /after/ the response has been sent to the client’s
169
- browser.
170
-
171
- The Varnish client also sends all invalidation requests in parallel to further
172
- reduce the time used by invalidation.
169
+ When using the [ Symfony bundle] ( https://github.com/FriendsOfSymfony/FOSHttpCacheBundle ) ,
170
+ you don’t have to call ` flush() ` , as the bundle flushes the invalidator for you
171
+ after the response has been sent.
173
172
174
173
Error handling
175
174
--------------
@@ -184,6 +183,27 @@ These exception are of two types:
184
183
* ` \FOS\HttpCache\ProxyResponseException ` when the caching proxy returns an
185
184
error response, such as 403 Forbidden.
186
185
186
+ So, to catch exceptions:
187
+
188
+ ``` php
189
+ use FOS\HttpCache\Exception\ExceptionCollection;
190
+
191
+ $cacheInvalidator
192
+ ->invalidatePath('/users');
193
+
194
+ try {
195
+ $cacheInvalidator->flush();
196
+ } catch (ExceptionCollection $exceptions) {
197
+ // The first exception that occurred
198
+ var_dump($exceptions->getFirst());
199
+
200
+ // Iterate over the exception collection
201
+ foreach ($exceptions as $exception) {
202
+ var_dump($exception);
203
+ }
204
+ }
205
+ ```
206
+
187
207
### Logging errors
188
208
189
209
You can log any exceptions in the following way. First construct a logger that
@@ -193,16 +213,16 @@ implements `\Psr\Log\LoggerInterface`. For instance, when using
193
213
``` php
194
214
use Monolog\Logger;
195
215
196
- $logger = new Logger(...);
197
- $logger ->pushHandler(...);
216
+ $monolog = new Logger(...);
217
+ $monolog ->pushHandler(...);
198
218
```
199
219
200
220
Then add the logger as a subscriber to the cache invalidator:
201
221
202
222
``` php
203
223
use FOS\HttpCache\EventListener\LogSubscriber;
204
224
205
- $subscriber = new LogSubscriber($logger );
225
+ $subscriber = new LogSubscriber($monolog );
206
226
$cacheInvalidator->addSubscriber($subscriber);
207
227
```
208
228
@@ -219,4 +239,4 @@ try {
219
239
} catch (ExceptionCollection $exceptions) {
220
240
// At least one failed request, check your logs!
221
241
}
222
- ```
242
+ ```
0 commit comments