@@ -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
|
@@ -171,7 +171,7 @@ from your application and returning them to the client.
|
171 | 171 | error_log($kernel->getLog());
|
172 | 172 |
|
173 | 173 | The ``AppCache`` object has a sensible default configuration, but it can be
|
174 |
| -finely tuned via a set of options you can set by overriding the |
| 174 | +finely tuned via a set of options you can set by overriding the |
175 | 175 | :method:`Symfony\\Bundle\\FrameworkBundle\\HttpCache\\HttpCache::getOptions`
|
176 | 176 | method::
|
177 | 177 |
|
@@ -239,8 +239,8 @@ misses.
|
239 | 239 | The Symfony2 reverse proxy is a great tool to use when developing your
|
240 | 240 | website or when you deploy your website to a shared host where you cannot
|
241 | 241 | install anything beyond PHP code. But being written in PHP, it cannot
|
242 |
| -be as fast as a proxy written in C. That's why we highly recommend you |
243 |
| -to use Varnish or Squid on your production servers if possible. The good |
| 242 | +be as fast as a proxy written in C. That's why it is highly recommended you |
| 243 | +use Varnish or Squid on your production servers if possible. The good |
244 | 244 | news is that the switch from one proxy server to another is easy and
|
245 | 245 | transparent as no code modification is needed in your application. Start
|
246 | 246 | easy with the Symfony2 reverse proxy and upgrade later to Varnish when
|
@@ -272,11 +272,10 @@ headers on the response.
|
272 | 272 |
|
273 | 273 | Keep in mind that "HTTP" is nothing more than the language (a simple text
|
274 | 274 | language) that web clients (e.g. browsers) and web servers use to communicate
|
275 |
| -with each other. When we talk about HTTP caching, we're talking about the |
276 |
| -part of that language that allows clients and servers to exchange information |
277 |
| -related to caching. |
| 275 | +with each other. HTTP caching is the part of that language that allows clients |
| 276 | +and servers to exchange information related to caching. |
278 | 277 |
|
279 |
| -HTTP specifies four response cache headers that we're concerned with: |
| 278 | +HTTP specifies four response cache headers that are looked at here: |
280 | 279 |
|
281 | 280 | * ``Cache-Control``
|
282 | 281 | * ``Expires``
|
@@ -424,7 +423,7 @@ on a cache to store and return "fresh" responses.
|
424 | 423 | found in two dedicated parts (`P4 - Conditional Requests`_ and `P6 -
|
425 | 424 | Caching: Browser and intermediary caches`_).
|
426 | 425 |
|
427 |
| -As a web developer, we strongly urge you to read the specification. Its |
| 426 | +As a web developer, you are strongly urged to read the specification. Its |
428 | 427 | clarity and power - even more than ten years after its creation - is
|
429 | 428 | invaluable. Don't be put-off by the appearance of the spec - its contents
|
430 | 429 | are much more beautiful than its cover.
|
@@ -567,16 +566,16 @@ md5 of the content::
|
567 | 566 | return $response;
|
568 | 567 | }
|
569 | 568 |
|
570 |
| -The :method:`Symfony\\Component\\HttpFoundation\\Response::isNotModified` |
571 |
| -method compares the ``ETag`` sent with the ``Request`` with the one set |
572 |
| -on the ``Response``. If the two match, the method automatically sets the |
| 569 | +The :method:`Symfony\\Component\\HttpFoundation\\Response::isNotModified` |
| 570 | +method compares the ``ETag`` sent with the ``Request`` with the one set |
| 571 | +on the ``Response``. If the two match, the method automatically sets the |
573 | 572 | ``Response`` status code to 304.
|
574 | 573 |
|
575 | 574 | This algorithm is simple enough and very generic, but you need to create the
|
576 | 575 | whole ``Response`` before being able to compute the ETag, which is sub-optimal.
|
577 | 576 | In other words, it saves on bandwidth, but not CPU cycles.
|
578 | 577 |
|
579 |
| -In the :ref:`optimizing-cache-validation` section, we'll show how validation |
| 578 | +In the :ref:`optimizing-cache-validation` section, you'll see how validation |
580 | 579 | can be used more intelligently to determine the validity of a cache without
|
581 | 580 | doing so much work.
|
582 | 581 |
|
@@ -626,9 +625,9 @@ header value::
|
626 | 625 | return $response;
|
627 | 626 | }
|
628 | 627 |
|
629 |
| -The :method:`Symfony\\Component\\HttpFoundation\\Response::isNotModified` |
630 |
| -method compares the ``If-Modified-Since`` header sent by the request with |
631 |
| -the ``Last-Modified`` header set on the response. If they are equivalent, |
| 628 | +The :method:`Symfony\\Component\\HttpFoundation\\Response::isNotModified` |
| 629 | +method compares the ``If-Modified-Since`` header sent by the request with |
| 630 | +the ``Last-Modified`` header set on the response. If they are equivalent, |
632 | 631 | the ``Response`` will be set to a 304 status code.
|
633 | 632 |
|
634 | 633 | .. note::
|
@@ -664,7 +663,7 @@ exposing a simple and efficient pattern::
|
664 | 663 | $response = new Response();
|
665 | 664 | $response->setETag($article->computeETag());
|
666 | 665 | $response->setLastModified($article->getPublishedAt());
|
667 |
| -
|
| 666 | + |
668 | 667 | // Set response as public. Otherwise it will be private by default.
|
669 | 668 | $response->setPublic();
|
670 | 669 |
|
@@ -697,7 +696,7 @@ headers that must not be present for ``304`` responses (see
|
697 | 696 | Varying the Response
|
698 | 697 | ~~~~~~~~~~~~~~~~~~~~
|
699 | 698 |
|
700 |
| -So far, we've assumed that each URI has exactly one representation of the |
| 699 | +So far, it's been assumed that each URI has exactly one representation of the |
701 | 700 | target resource. By default, HTTP caching is done by using the URI of the
|
702 | 701 | resource as the cache key. If two people request the same URI of a cacheable
|
703 | 702 | resource, the second person will receive the cached version.
|
@@ -708,7 +707,7 @@ compress pages when the client supports it, any given URI has two representation
|
708 | 707 | one when the client supports compression, and one when it does not. This
|
709 | 708 | determination is done by the value of the ``Accept-Encoding`` request header.
|
710 | 709 |
|
711 |
| -In this case, we need the cache to store both a compressed and uncompressed |
| 710 | +In this case, you need the cache to store both a compressed and uncompressed |
712 | 711 | version of the response for the particular URI and return them based on the
|
713 | 712 | request's ``Accept-Encoding`` value. This is done by using the ``Vary`` response
|
714 | 713 | header, which is a comma-separated list of different headers whose values
|
@@ -856,8 +855,8 @@ First, to use ESI, be sure to enable it in your application configuration:
|
856 | 855 | 'esi' => array('enabled' => true),
|
857 | 856 | ));
|
858 | 857 |
|
859 |
| -Now, suppose we have a page that is relatively static, except for a news |
860 |
| -ticker at the bottom of the content. With ESI, we can cache the news ticker |
| 858 | +Now, suppose you have a page that is relatively static, except for a news |
| 859 | +ticker at the bottom of the content. With ESI, you can cache the news ticker |
861 | 860 | independent of the rest of the page.
|
862 | 861 |
|
863 | 862 | .. code-block:: php
|
@@ -871,7 +870,7 @@ independent of the rest of the page.
|
871 | 870 | return $response;
|
872 | 871 | }
|
873 | 872 |
|
874 |
| -In this example, we've given the full-page cache a lifetime of ten minutes. |
| 873 | +In this example, the full-page cache has a lifetime of ten minutes. |
875 | 874 | Next, let's include the news ticker in the template by embedding an action.
|
876 | 875 | This is done via the ``render`` helper (See :ref:`templating-embedding-controller`
|
877 | 876 | for more details).
|
@@ -966,8 +965,8 @@ the ``_internal`` route:
|
966 | 965 |
|
967 | 966 | Since this route allows all actions to be accessed via a URL, you might
|
968 | 967 | want to protect it by using the Symfony2 firewall feature (by allowing
|
969 |
| -access to your reverse proxy's IP range). See the :ref:`Securing by IP<book-security-securing-ip>` |
970 |
| -section of the :doc:`Security Chapter </book/security>` for more information |
| 968 | +access to your reverse proxy's IP range). See the :ref:`Securing by IP<book-security-securing-ip>` |
| 969 | +section of the :doc:`Security Chapter </book/security>` for more information |
971 | 970 | on how to do this.
|
972 | 971 |
|
973 | 972 | One great advantage of this caching strategy is that you can make your
|
|
0 commit comments