@@ -22,8 +22,8 @@ Caching on the Shoulders of Giants
22
22
The most effective way to improve performance of an application is to cache
23
23
the full output of a page and then bypass the application entirely on each
24
24
subsequent request. Of course, this isn't always possible for highly dynamic
25
- websites, or is it? In this chapter, we 'll show you how the Symfony2 cache
26
- system works and why we think this is the best possible approach.
25
+ websites, or is it? In this chapter, you 'll see how the Symfony2 cache
26
+ system works and why this is the best possible approach.
27
27
28
28
The Symfony2 cache system is different because it relies on the simplicity
29
29
and power of the HTTP cache as defined in the :term: `HTTP specification `.
@@ -32,8 +32,8 @@ that defines basic communication on the Web. Once you understand the fundamental
32
32
HTTP validation and expiration caching models, you'll be ready to master
33
33
the Symfony2 cache system.
34
34
35
- For the purposes of learning how to cache with Symfony2, we'll cover the
36
- subject in four steps:
35
+ For the purposes of learning how to cache with Symfony2, the
36
+ subject is covered in four steps:
37
37
38
38
#. A :ref: `gateway cache <gateway-caches >`, or reverse proxy, is
39
39
an independent layer that sits in front of your application. The reverse
@@ -57,8 +57,8 @@ subject in four steps:
57
57
sidebar for only 5 minutes.
58
58
59
59
Since caching with HTTP isn't unique to Symfony, many articles already exist
60
- on the topic. If you're new to HTTP caching, we * highly * recommend Ryan
61
- Tomayko's article `Things Caches Do `_. Another in-depth resource is Mark
60
+ on the topic. If you're new to HTTP caching, Ryan
61
+ Tomayko's article `Things Caches Do `_ is * highly * recommended . Another in-depth resource is Mark
62
62
Nottingham's `Cache Tutorial `_.
63
63
64
64
.. index ::
@@ -118,8 +118,8 @@ different types of caches:
118
118
.. note ::
119
119
120
120
The significance of *private * versus *shared * caches will become more
121
- obvious as we talk about caching responses containing content that is
122
- specific to exactly one user (e.g. account information).
121
+ obvious when caching responses containing content that is
122
+ specific to exactly one user (e.g. account information) is discussed .
123
123
124
124
Each response from your application will likely go through one or both of
125
125
the first two cache types. These caches are outside of your control but follow
@@ -168,7 +168,7 @@ from your application and returning them to the client.
168
168
error_log($kernel->getLog());
169
169
170
170
The ``AppCache `` object has a sensible default configuration, but it can be
171
- finely tuned via a set of options you can set by overriding the
171
+ finely tuned via a set of options you can set by overriding the
172
172
:method: `Symfony\\ Bundle\\ FrameworkBundle\\ HttpCache\\ HttpCache::getOptions `
173
173
method::
174
174
@@ -236,8 +236,8 @@ misses.
236
236
The Symfony2 reverse proxy is a great tool to use when developing your
237
237
website or when you deploy your website to a shared host where you cannot
238
238
install anything beyond PHP code. But being written in PHP, it cannot
239
- be as fast as a proxy written in C. That's why we highly recommend you
240
- to use Varnish or Squid on your production servers if possible. The good
239
+ be as fast as a proxy written in C. That's why it is highly recommended you
240
+ use Varnish or Squid on your production servers if possible. The good
241
241
news is that the switch from one proxy server to another is easy and
242
242
transparent as no code modification is needed in your application. Start
243
243
easy with the Symfony2 reverse proxy and upgrade later to Varnish when
@@ -269,11 +269,10 @@ headers on the response.
269
269
270
270
Keep in mind that "HTTP" is nothing more than the language (a simple text
271
271
language) that web clients (e.g. browsers) and web servers use to communicate
272
- with each other. When we talk about HTTP caching, we're talking about the
273
- part of that language that allows clients and servers to exchange information
274
- related to caching.
272
+ with each other. HTTP caching is the part of that language that allows clients
273
+ and servers to exchange information related to caching.
275
274
276
- HTTP specifies four response cache headers that we're concerned with :
275
+ HTTP specifies four response cache headers that are looked at here :
277
276
278
277
* ``Cache-Control ``
279
278
* ``Expires ``
@@ -421,7 +420,7 @@ on a cache to store and return "fresh" responses.
421
420
found in two dedicated parts (`P4 - Conditional Requests `_ and `P6 -
422
421
Caching: Browser and intermediary caches `_).
423
422
424
- As a web developer, we strongly urge you to read the specification. Its
423
+ As a web developer, you are strongly urged to read the specification. Its
425
424
clarity and power - even more than ten years after its creation - is
426
425
invaluable. Don't be put-off by the appearance of the spec - its contents
427
426
are much more beautiful than its cover.
@@ -564,16 +563,16 @@ md5 of the content::
564
563
return $response;
565
564
}
566
565
567
- The :method: `Symfony\\ Component\\ HttpFoundation\\ Response::isNotModified `
568
- method compares the ``ETag `` sent with the ``Request `` with the one set
569
- on the ``Response ``. If the two match, the method automatically sets the
566
+ The :method: `Symfony\\ Component\\ HttpFoundation\\ Response::isNotModified `
567
+ method compares the ``ETag `` sent with the ``Request `` with the one set
568
+ on the ``Response ``. If the two match, the method automatically sets the
570
569
``Response `` status code to 304.
571
570
572
571
This algorithm is simple enough and very generic, but you need to create the
573
572
whole ``Response `` before being able to compute the ETag, which is sub-optimal.
574
573
In other words, it saves on bandwidth, but not CPU cycles.
575
574
576
- In the :ref: `optimizing-cache-validation ` section, we 'll show how validation
575
+ In the :ref: `optimizing-cache-validation ` section, you 'll see how validation
577
576
can be used more intelligently to determine the validity of a cache without
578
577
doing so much work.
579
578
@@ -623,9 +622,9 @@ header value::
623
622
return $response;
624
623
}
625
624
626
- The :method: `Symfony\\ Component\\ HttpFoundation\\ Response::isNotModified `
627
- method compares the ``If-Modified-Since `` header sent by the request with
628
- the ``Last-Modified `` header set on the response. If they are equivalent,
625
+ The :method: `Symfony\\ Component\\ HttpFoundation\\ Response::isNotModified `
626
+ method compares the ``If-Modified-Since `` header sent by the request with
627
+ the ``Last-Modified `` header set on the response. If they are equivalent,
629
628
the ``Response `` will be set to a 304 status code.
630
629
631
630
.. note ::
@@ -661,7 +660,7 @@ exposing a simple and efficient pattern::
661
660
$response = new Response();
662
661
$response->setETag($article->computeETag());
663
662
$response->setLastModified($article->getPublishedAt());
664
-
663
+
665
664
// Set response as public. Otherwise it will be private by default.
666
665
$response->setPublic();
667
666
@@ -694,7 +693,7 @@ headers that must not be present for ``304`` responses (see
694
693
Varying the Response
695
694
~~~~~~~~~~~~~~~~~~~~
696
695
697
- So far, we've assumed that each URI has exactly one representation of the
696
+ So far, it's been assumed that each URI has exactly one representation of the
698
697
target resource. By default, HTTP caching is done by using the URI of the
699
698
resource as the cache key. If two people request the same URI of a cacheable
700
699
resource, the second person will receive the cached version.
@@ -705,7 +704,7 @@ compress pages when the client supports it, any given URI has two representation
705
704
one when the client supports compression, and one when it does not. This
706
705
determination is done by the value of the ``Accept-Encoding `` request header.
707
706
708
- In this case, we need the cache to store both a compressed and uncompressed
707
+ In this case, you need the cache to store both a compressed and uncompressed
709
708
version of the response for the particular URI and return them based on the
710
709
request's ``Accept-Encoding `` value. This is done by using the ``Vary `` response
711
710
header, which is a comma-separated list of different headers whose values
@@ -853,8 +852,8 @@ First, to use ESI, be sure to enable it in your application configuration:
853
852
'esi' => array('enabled' => true),
854
853
));
855
854
856
- Now, suppose we have a page that is relatively static, except for a news
857
- ticker at the bottom of the content. With ESI, we can cache the news ticker
855
+ Now, suppose you have a page that is relatively static, except for a news
856
+ ticker at the bottom of the content. With ESI, you can cache the news ticker
858
857
independent of the rest of the page.
859
858
860
859
.. code-block :: php
@@ -868,7 +867,7 @@ independent of the rest of the page.
868
867
return $response;
869
868
}
870
869
871
- In this example, we've given the full-page cache a lifetime of ten minutes.
870
+ In this example, the full-page cache has a lifetime of ten minutes.
872
871
Next, let's include the news ticker in the template by embedding an action.
873
872
This is done via the ``render `` helper (See :ref: `templating-embedding-controller `
874
873
for more details).
@@ -963,8 +962,8 @@ the ``_internal`` route:
963
962
964
963
Since this route allows all actions to be accessed via a URL, you might
965
964
want to protect it by using the Symfony2 firewall feature (by allowing
966
- access to your reverse proxy's IP range). See the :ref: `Securing by IP<book-security-securing-ip> `
967
- section of the :doc: `Security Chapter </book/security >` for more information
965
+ access to your reverse proxy's IP range). See the :ref: `Securing by IP<book-security-securing-ip> `
966
+ section of the :doc: `Security Chapter </book/security >` for more information
968
967
on how to do this.
969
968
970
969
One great advantage of this caching strategy is that you can make your
0 commit comments