1 / 7

How To Specify A Cache Validator For A Webpage In PHP

A Cache validator is defined within HTTP request and response headers and help determine if it is still valid to retrieve a file from the browseru2019s cache. Cache validators are important as they determine whether or not your request must be sent to the server (thus increasing load times and expending resources) or if the files can be retrieved directly from the local cache, therefore, improving load times and decreasing latency. Here, we will learn how to specify a validator in PHP

php1
Download Presentation

How To Specify A Cache Validator For A Webpage In PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. How To Specify A Cache Validator For A Webpage In PHP? www.phpsupport.co

  2. INTRODUCTION A Cache validator is defined within HTTP request and response headers and help determine if it is still valid to retrieve a file from the browser’s cache. Cache validators are important as they determine whether or not your request must be sent to the server (thus increasing load times and expending resources) or if the files can be retrieved directly from the local cache, therefore, improving load times and decreasing latency. Here, we will learn how to specify a validator in PHP

  3. How to Specify a cache validator? The first thing that is important to note about this warning is that you can only fix this for requests that are on your server. There are four different types of headers which can be utilized in different ways to fix this warning. The first two headers are last-modified and ETag. These headers help the browser determine if the file has changed since the last time it was requested. Or rather, they validate the cache. The next two headers are Cache- Control and Expires. These headers help determine how long the file should be held in cache before it fetches a new copy from the server.

  4. 1. LAST-MODIFIED HEADER The last-modified header is generally sent automatically from the server. This is one header you generally won’t need to manually add. It is sent to see if the file in the browser’s cache has been modified since the last time it was requested. Here is an example in the given picture

  5. 2. ETag Header The ETag header is similar to the last-modified header. It is also used to validate the cache of a file. If you are running Apache 2.4 or higher, the ETag header is already automatically added using the FileETag directive. The next two headers are Cache-Control and Expires. These headers help determine how long the file should be held in cache before it fetches a new copy from the server.

  6. 3. CACHE-CONTROL HEADER Cache-Control is a header made up of different directives which allow you to define the length of the cache. A few of the most common directives includes max-age that defines the amount of time a file should be cached for.public that allows any cache publicly to store the response.private that only cacheable by browser accessing the file. In our example above, you can see the asset is using the max-age directive. 604800 seconds would equal a cache of seven days.

  7. 4. Expires Header The last header is Expires header. All modern browsers support Cache-Control, hence that is all you need. However, it won’t hurt anything if you have both, but remember that only one will be used. It uses an actual date, whereas the Cache-Control header lets you specify an amount of time before expiry.

More Related