Friday, April 19, 2024
HomeJavaInternet useful resource caching: Consumer-side

Internet useful resource caching: Consumer-side


Let’s put the speculation that we have now seen above into follow. I’ll arrange a two-tiered HTTP cache:

  • The primary tier caches assets regionally for 10 seconds utilizing Cache-Management
  • The second tier makes use of ETag to keep away from optimizing the info load over the community

I’ll use Apache APISIX. APISIX sits on the shoulder of giants, specifically NGINX. NGINX provides ETag response headers by default.

We solely want so as to add the Cache-Management response header. We obtain it with the response-rewrite plugin:

upstreams:
  - id: 1
    sort: roundrobin
    nodes:
      "content material:8080": 1
routes:
  - uri: /*
    upstream_id: 1
    plugins:
      response-rewrite:
        headers:
          set:
            Cache-Management: "max-age=10"

Let’s do it and not using a browser first.

HTTP/1.1 200 OK
Content material-Kind: textual content/html; charset=utf-8
Content material-Size: 147
Connection: keep-alive
Date: Thu, 24 Nov 2022 08:21:36 GMT
Settle for-Ranges: bytes
Final-Modified: Wed, 23 Nov 2022 13:58:55 GMT
ETag: "637e271f-93"
Server: APISIX/3.0.0
Cache-Management: max-age=10

To forestall the server from sending the identical useful resource, we are able to use the ETag worth in an If-None-Match request header:

curl -H 'If-None-Match: "637e271f-93"'   -v localhost:9080

The result’s a 304 Not Modified as anticipated:

HTTP/1.1 304 Not Modified
Content material-Kind: textual content/html; charset=utf-8
Content material-Size: 147
Connection: keep-alive
Date: Thu, 24 Nov 2022 08:26:17 GMT
Settle for-Ranges: bytes
Final-Modified: Wed, 23 Nov 2022 13:58:55 GMT
ETag: "637e271f-93"
Server: APISIX/3.0.0
Cache-Management: max-age=10

Now, we are able to do the identical inside a browser. If we use the resend function a second time earlier than 10 seconds have handed, the browser returns the useful resource from the cache with out sending the request to the server.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments