HTTP Caching Flashcards
(7 cards)
What are 2 types of validating freshness of a the cache?
- expiration
- validation (check if response changed since last page call)
What does the ETag do?
Its part of validation expiration.
Its a hash of the response, which the server can compare with newer hashes. If its the same, the cache is still fresh.
What does the Last-Modified do?
Its part of validation expiration.
The browser safes the timestamp of the last request on a specific resource. When requesting the same resource again, it tells the server to give an updated version if the resource was updated after the saved timestamp (via “If-Modified-Since”). Therefore the server can use its cached response or create a fresh one once required.
Whats a reverse proxy?
Sits between browser and application. Offers some features like caching so only necessary requests are being directed to the application.
What does the “Expires”-Response-Header do?
Tells the reverse proxy a date until the specific response should be cached.
How can you add cache to a controller-route?
#[Cache(public: true, maxage: 3600, mustRevalidate: true)]
Whats the drawback of using the completely generated response for the etag?
It only saves bandwith, not CPU, since the response has to be generated to know if it was cached. More performant ways could be using the last modified dates and use the ressources needed for the response, e.g:
class ArticleController extends AbstractController { public function show(Article $article, Request $request): Response { $author = $article->getAuthor(); $articleDate = new \DateTime($article->getUpdatedAt()); $authorDate = new \DateTime($author->getUpdatedAt()); $date = $authorDate > $articleDate ? $authorDate : $articleDate; $response = new Response(); $response->setLastModified($date); // Set response as public. Otherwise it will be private by default. $response->setPublic(); if ($response->isNotModified($request)) { return $response; } // ... do more work to populate the response with the full content return $response; } }