Skip to content

Commit 68dd4c2

Browse files
committed
Merge pull request #32 from ddeboer/docs
Improve docs
2 parents 0eae815 + e2f4b53 commit 68dd4c2

13 files changed

+624
-329
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@ FOSHttpCache
44
[![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/ddeboer/FOSHttpCache/badges/quality-score.png?s=5b808e92306a54228a81378ec20a47bb5313a5c7)](https://scrutinizer-ci.com/g/ddeboer/FOSHttpCache/)
55
[![Code Coverage](https://scrutinizer-ci.com/g/ddeboer/FOSHttpCache/badges/coverage.png?s=f9f57d6b28285f38782b38a08b1dbdb24901a764)](https://scrutinizer-ci.com/g/ddeboer/FOSHttpCache/)
66

7-
87
Introduction
98
------------
109

1110
This library integrates your PHP applications with HTTP caching proxies such as Varnish.
1211
Use this library to send invalidation requests from your application to the caching proxy
1312
and to test your caching and invalidation code against a Varnish setup.
1413

15-
If you use Symfony2, you want to look at the [FOSHttpCacheBundle](https://github.com/FriendsOfSymfony/FOSHttpCacheBundle)
16-
which provides the invalidator as a service to Symfony2, along with a couple of
17-
other useful Symfony2 specific features to help with caching and caching proxies.
14+
If you use Symfony2, have a look at the
15+
[FOSHttpCacheBundle](https://github.com/FriendsOfSymfony/FOSHttpCacheBundle).
16+
The bundle provides the invalidator as a service, along with a number of
17+
Symfony2-specific features to help with caching and caching proxies.
1818

1919
Features
2020
--------
2121

2222
* Send [cache invalidation requests](doc/cache-invalidator.md) with minimal impact on performance.
2323
* Use the built-in support for [Varnish](doc/varnish.md) or easily implement your own caching proxy client.
2424
* [Test your application](doc/testing-your-application.md) against your Varnish setup.
25+
* This library is compatible with [HHVM](http://www.hhvm.com/blog/).
2526

2627
Documentation
2728
-------------

doc/cache-invalidator.md

Lines changed: 100 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,153 @@
11
The Cache Invalidator
2-
=================
2+
=====================
33

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.
67

78
* [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)
1011
* [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)
1114
* [Tags](#tags)
15+
* [Custom Tags Header](#custom-tags-header)
1216
* [Flushing](#flushing)
1317
* [Error handling](#error-handling)
18+
* [Logging errors](#logging-errors)
1419

1520
Setup
1621
-----
1722

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):
2025

2126
```php
22-
use FOS\HttpCache\Invalidation\Varnish;
2327
use FOS\HttpCache\CacheInvalidator;
28+
use FOS\HttpCache\Invalidation;
2429

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);
2735
```
2836

29-
Invalidating paths and URLs
37+
Invalidating Paths and URLs
3038
---------------------------
3139

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.
3441

3542
Invalidate a path:
3643

3744
```php
38-
$cacheInvalidator->invalidatePath('/users');
45+
$cacheInvalidator->invalidatePath('/users')
46+
->flush()
47+
;
3948
```
4049

41-
Invalidate an URL:
50+
See below for the [flush](#flushing) method.
51+
52+
Invalidate a URL:
53+
4254
```php
43-
$cacheInvalidator->invalidatePath('http://www.example.com/users');
55+
$cacheInvalidator->invalidatePath('http://www.example.com/users')->flush();
4456
```
4557

46-
Refreshing paths and URLs
58+
Refreshing Paths and URLs
4759
-------------------------
4860

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.
5163

5264
Refresh a path:
5365

5466
```php
55-
$cacheInvalidator->refreshPath('/users');
67+
$cacheInvalidator->refreshPath('/users')->flush();
5668
```
5769

58-
Refresh an URL:
70+
Refresh a URL:
5971

6072
```php
61-
$cacheInvalidator->refreshPath('http://www.example.com/users');
73+
$cacheInvalidator->refreshPath('http://www.example.com/users')->flush();
6274
```
6375

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.
6680

67-
Make sure to configure your proxy for regular expressions first.
68-
(See [varnish ban](varnish.md#ban).)
81+
### URL, Content Type and Hostname
6982

7083
You can invalidate all URLs matching a regular expression by using the
7184
`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.
7386

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
7588
caching proxy:
7689

7790
```php
78-
$cacheInvalidator->invalidateRegex('.*css$');
91+
$cacheInvalidator->invalidateRegex('.*css$')->flush();
7992
```
8093

81-
To invalidate all png files for host example.com:
94+
To invalidate all .png files for host example.com:
8295

8396
```php
84-
$cacheInvalidator->invalidateRegex('.*', 'image/png', array('example.com'));
97+
$cacheInvalidator
98+
->invalidateRegex('.*', 'image/png', array('example.com'))
99+
->flush()
100+
;
85101
```
86102

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
92104

93105
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.
96108

97109
Cache client implementations should fill up the headers to at least have the
98110
default headers always present to simplify the cache configuration rules.
99111

100112
To invalidate on a custom header X-My-Header, you would do:
101113

102114
```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();
117116
```
118117

119118
Tags
120119
----
121120

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.
126122

127123
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.
131127

132-
Assume you sent 3 responses:
128+
Assume you sent four responses:
133129

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`
138134

139135
You can now invalidate some URLs using tags:
140136

141137
```php
142-
$cacheInvalidator->invalidateTags(array('group-a', 'tag-four'));
138+
$cacheInvalidator->invalidateTags(array('group-a', 'tag-four'))->flush();
143139
```
144140

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`
147143
will stay in the cache.
148144

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+
149151
Flushing
150152
--------
151153

@@ -160,16 +162,13 @@ $cacheInvalidator
160162
;
161163
```
162164

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.
166168

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.
173172

174173
Error handling
175174
--------------
@@ -184,6 +183,27 @@ These exception are of two types:
184183
* `\FOS\HttpCache\ProxyResponseException` when the caching proxy returns an
185184
error response, such as 403 Forbidden.
186185

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+
187207
### Logging errors
188208

189209
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
193213
```php
194214
use Monolog\Logger;
195215

196-
$logger = new Logger(...);
197-
$logger->pushHandler(...);
216+
$monolog = new Logger(...);
217+
$monolog->pushHandler(...);
198218
```
199219

200220
Then add the logger as a subscriber to the cache invalidator:
201221

202222
```php
203223
use FOS\HttpCache\EventListener\LogSubscriber;
204224

205-
$subscriber = new LogSubscriber($logger);
225+
$subscriber = new LogSubscriber($monolog);
206226
$cacheInvalidator->addSubscriber($subscriber);
207227
```
208228

@@ -219,4 +239,4 @@ try {
219239
} catch (ExceptionCollection $exceptions) {
220240
// At least one failed request, check your logs!
221241
}
222-
```
242+
```

0 commit comments

Comments
 (0)