Several distinct caches for a single URL in HTML Cache?


Content caching is an effective way for fast and smooth user experience. Every URL in a web page can be linked to a single cache manifest section. However, there are certain large and rarely used files which decreases the overall speed of the Cache. Since they do not need to be changed but still gets re-downloaded every time the Cache gets updated, it is better to have such files in different cache with same URL. This will increase its speed and performance.

So, how to spilt HTML cache? Before that, let’s understand what an HTML cache is.

What is an HTML cache?

Caching is a way to store the given responses for a request and then reusing the stored response for upcoming similar requests. It is present from the original server to the web browser.

Reusing the stored responses increase the speed of responds of the server. It also increases the performance of the hardware.

HTML cache is caching the HTML content of a web page.

Example

<!DOCTYPE HTML> <html manifest = “example.appcache” > <body> <h1> This is an example </h1> </body> </html>

Types of Cache

HTTP Cache stores requests and responses for reusing those in upcoming requests. It can be of two types. They are shared cache and private cache.

Shared cache

It is a cache which stores data between original server and multiple clients. It stores a response and reuses it with all its clients.

Thus, it does not contain personalised contents.

Private cache

This cache stores contents only of the clients. It stores the responses for a single client user. Hence, the developers can store personalized contents in this cache.

Cache control

The cache control http header specifies instructions in order to manage the cache data from both requests and responses.

It has various directives such as no-transform, max-age, no-store, no-cache, private and public. Let us discuss a few below.

max-age

It is one of the response directives of cache control which indicates that the cache remains fresh until X seconds after the response is generated.

Here, fresh state tells the browser that the response is still valid. So, it can be reused. Whereas, stale state states that response is invalid that is, it has expired.

Syntax

Cache-Control: max-age = X

Example

Cache-Control: max-age = 856098

s-maxage

It is also a response directive which states whether the cache is fresh or stale. However, it is applicable only for shared cache.

Example

Cache-Control: s-maxage = 766498

Timestamp

A timestamp is the time noted of a file or log which indicates the time its content is added, modified or transmitted. Its reference date is January 1, 1970 UTC.

Example

This block of code gives the timestamp in milliseconds.

Date.now = function demo() { return new Date().getTime(); }

In Javascript, you can use Date.now()

In order to get the timestamp in seconds, divide it by 1000.

Example

function demo() { return Math.floor (Date.now() / 1000); }

Note − Math.floor() function is used to get the largest integer less than or equal to a given number.

Example

console.log( Math.floor(5.95));

The best way to solve the problem is to use max-age to all the files in the Cache section of manifest and to add timestamp to each file of the Cache.

If any of those files gets modified, the manifest will update the timestamp. Hence, only those files will be downloaded, while cache processing, which will have updated timestamp.

Example

First, let’s create a Cache manifest file

<!DOCTYPE HTML> <html manifest = “example.appcache” > <body> <h1> This is an example </h1> </body Cache-Control: max-age =567890> </html>

CACHE MANIFEST

Cache −

/example.html
/demo.js

Conclusion

Handling the cache properly create a great impact on your website. Caching provides high speed and increased performance of the user interface that is, user enjoys faster experience. It also enables the server of the website to manage more network traffic with same hardware. Most importantly, it is cost-effective and even serves same content repeatedly. In short, proper caching provides various gains to the developers.

Updated on: 13-Oct-2022

211 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements