HTTP Caching: Difference between revisions

Jump to navigation Jump to search
no edit summary
(Created page with "One of the often overlooked superpowers of HTTP is the built in support for caching. This allows you to only spend the effort of generating a response every so often inste...")
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 7: Line 7:


== Time Based Caching ==
== Time Based Caching ==
An easy way to implement caching is by specifying that a response is valid for a certain amount of time. For example if your resource is static and the same for all users you can set the following [[Cache-Control Header]]: <code>Cache-Control: public,maxage=300</code> This will keep the resource in your cache for five minutes.
An easy way to implement caching is by specifying that a response is valid for a certain amount of time. For example if your resource is static and the same for all users you can set the following [[Cache-Control header]]: <code>Cache-Control: public,maxage=300</code> This will keep the resource in your cache for five minutes.


== ETag Based Caching ==
== ETag Based Caching ==
If it is easy to generate a unique identifier that always changes when the response changes you can use that as an ETag identifier. This is done using the [[ETag Header]]:
If it is easy to generate a unique identifier that always changes when the response changes you can use that as an ETag identifier. This is done using the [[ETag Header]]:


  ETag: W/"my-unique-identifier"
ETag: W/"my-unique-identifier"
  Cache-Control: public,maxage=5,must-revalidate
Cache-Control: public,maxage=5,must-revalidate


This example will ask the cache to keep serving this response to requests coming in in the next 5 seconds. If a matching request comes in after the five seconds the cache will check whether it still has the last version using the [[If-None-Match header]].
This example will ask the cache to keep serving this response to requests coming in in the next 5 seconds. If a matching request comes in after the five seconds the cache will check whether it still has the last version using the [[If-None-Match header]].
Line 33: Line 33:
* Don't put data that changes independently into the same request. Instead use a resource that returns [[URL | URLs]] to the child requests and leverage HTTP2 and caching.
* Don't put data that changes independently into the same request. Instead use a resource that returns [[URL | URLs]] to the child requests and leverage HTTP2 and caching.
* Instead of disabling caching, set the <code>maxage</code> to a low value like 5 seconds.
* Instead of disabling caching, set the <code>maxage</code> to a low value like 5 seconds.
[[Category: Terminology]]

Navigation menu