cache-plugin.rst 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. =================
  2. HTTP Cache plugin
  3. =================
  4. Guzzle can leverage HTTP's caching specifications using the ``Guzzle\Plugin\Cache\CachePlugin``. The CachePlugin
  5. provides a private transparent proxy cache that caches HTTP responses. The caching logic, based on
  6. `RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html>`_, uses HTTP headers to control caching behavior,
  7. cache lifetime, and supports Vary, ETag, and Last-Modified based revalidation:
  8. .. code-block:: php
  9. use Guzzle\Http\Client;
  10. use Doctrine\Common\Cache\FilesystemCache;
  11. use Guzzle\Cache\DoctrineCacheAdapter;
  12. use Guzzle\Plugin\Cache\CachePlugin;
  13. use Guzzle\Plugin\Cache\DefaultCacheStorage;
  14. $client = new Client('http://www.test.com/');
  15. $cachePlugin = new CachePlugin(array(
  16. 'storage' => new DefaultCacheStorage(
  17. new DoctrineCacheAdapter(
  18. new FilesystemCache('/path/to/cache/files')
  19. )
  20. )
  21. ));
  22. // Add the cache plugin to the client object
  23. $client->addSubscriber($cachePlugin);
  24. $client->get('http://www.wikipedia.org/')->send();
  25. // The next request will revalidate against the origin server to see if it
  26. // has been modified. If a 304 response is received the response will be
  27. // served from cache
  28. $client->get('http://www.wikipedia.org/')->send();
  29. The cache plugin intercepts GET and HEAD requests before they are actually transferred to the origin server. The cache
  30. plugin then generates a hash key based on the request method and URL, and checks to see if a response exists in the cache. If
  31. a response exists in the cache, the cache adapter then checks to make sure that the caching rules associated with the response
  32. satisfy the request, and ensures that response still fresh. If the response is acceptable for the request any required
  33. revalidation, then the cached response is served instead of contacting the origin server.
  34. Vary
  35. ----
  36. Cache keys are derived from a request method and a request URL. Multiple responses can map to the same cache key and
  37. stored in Guzzle's underlying cache storage object. You should use the ``Vary`` HTTP header to tell the cache storage
  38. object that the cache response must have been cached for a request that matches the headers specified in the Vary header
  39. of the request. This allows you to have specific cache entries for the same request URL but variations in a request's
  40. headers determine which cache entry is served. Please see the http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.44
  41. for more information.
  42. Cache options
  43. -------------
  44. There are several options you can add to requests or clients to modify the behavior of the cache plugin.
  45. Override cache TTL
  46. ~~~~~~~~~~~~~~~~~~
  47. You can override the number of seconds a cacheable response is stored in the cache by setting the
  48. ``cache.override_ttl`` parameter on the params object of a request:
  49. .. code-block:: php
  50. // If the response to the request is cacheable, then the response will be cached for 100 seconds
  51. $request->getParams()->set('cache.override_ttl', 100);
  52. If a response doesn't specify any freshness policy, it will be kept in cache for 3600 seconds by default.
  53. Custom caching decision
  54. ~~~~~~~~~~~~~~~~~~~~~~~
  55. If the service you are interacting with does not return caching headers or returns responses that are normally
  56. something that would not be cached, you can set a custom ``can_cache`` object on the constructor of the CachePlugin
  57. and provide a ``Guzzle\Plugin\Cache\CanCacheInterface`` object. You can use the
  58. ``Guzzle\Plugin\Cache\CallbackCanCacheStrategy`` to easily make a caching decision based on an HTTP request and
  59. response.
  60. Revalidation options
  61. ~~~~~~~~~~~~~~~~~~~~
  62. You can change the revalidation behavior of a request using the ``cache.revalidate`` parameter. Setting this
  63. parameter to ``never`` will ensure that a revalidation request is never sent, and the response is always served from
  64. the origin server. Setting this parameter to ``skip`` will never revalidate and uses the response stored in the cache.
  65. Normalizing requests for caching
  66. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  67. Use the ``cache.key_filter`` parameter if you wish to strip certain query string parameters from your
  68. request before creating a unique hash for the request. This parameter can be useful if your requests have query
  69. string values that cause each request URL to be unique (thus preventing a cache hit). The ``cache.key_filter``
  70. format is simply a comma separated list of query string values to remove from the URL when creating a cache key.
  71. For example, here we are saying that the ``a`` and ``q`` query string variables should be ignored when generating a
  72. cache key for the request:
  73. .. code-block:: php
  74. $request->getParams()->set('cache.key_filter', 'a, q');
  75. Other options
  76. ~~~~~~~~~~~~~
  77. There are many other options available to the CachePlugin that can meet almost any caching requirement, including
  78. custom revalidation implementations, custom cache key generators, custom caching decision strategies, and custom
  79. cache storage objects. Take a look the constructor of ``Guzzle\Plugin\Cache\CachePlugin`` for more information.
  80. Setting Client-wide cache settings
  81. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  82. You can specify cache settings for every request created by a client by adding cache settings to the configuration
  83. options of a client.
  84. .. code-block:: php
  85. $client = new Guzzle\Http\Client('http://www.test.com', array(
  86. 'request.params' => array(
  87. 'cache.override_ttl' => 3600,
  88. 'params.cache.revalidate' => 'never'
  89. )
  90. ));
  91. echo $client->get('/')->getParams()->get('cache.override_ttl');
  92. // >>> 3600
  93. echo $client->get('/')->getParams()->get('cache.revalidate');
  94. // >>> never
  95. Cache revalidation
  96. ------------------
  97. If the cache plugin determines that a response to a GET request needs revalidation, a conditional GET is transferred
  98. to the origin server. If the origin server returns a 304 response, then a response containing the merged headers of
  99. the cached response with the new response and the entity body of the cached response is returned. Custom revalidation
  100. strategies can be injected into a CachePlugin if needed.
  101. Cache adapters
  102. --------------
  103. Guzzle doesn't try to reinvent the wheel when it comes to caching or logging. Plenty of other frameworks have
  104. excellent solutions in place that you are probably already using in your applications. Guzzle uses adapters for
  105. caching and logging. The cache plugin requires a cache adapter so that is can store responses in a cache. Guzzle
  106. currently supports cache adapters for `Doctrine 2.0 <http://www.doctrine-project.org/>`_ and the
  107. `Zend Framework <http://framework.zend.com>`_.
  108. Doctrine cache adapter
  109. ~~~~~~~~~~~~~~~~~~~~~~
  110. .. code-block:: php
  111. use Doctrine\Common\Cache\ArrayCache;
  112. use Guzzle\Cache\DoctrineCacheAdapter;
  113. use Guzzle\Plugin\Cache\CachePlugin;
  114. $backend = new ArrayCache();
  115. $adapter = new DoctrineCacheAdapter($backend);
  116. $cache = new CachePlugin($adapter);
  117. Zend Framework cache adapter
  118. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  119. .. code-block:: php
  120. use Guzzle\Cache\ZendCacheAdapter;
  121. use Zend\Cache\Backend\TestBackend;
  122. $backend = new TestBackend();
  123. $adapter = new ZendCacheAdapter($backend);
  124. $cache = new CachePlugin($adapter);