পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Thursday, March 2, 2017

Koa.js - Caching

Caching is the term for storing reusable responses in order to make subsequent requests faster. Every browser ships with an implementation of an HTTP cache! All we have to do is ensure that each server response provides correct HTTP header directives to instruct the browser on when and for how long the response can be cached by the browser.

Here are some benefits of including caching in your webapps:
  • Your network costs decrease. If your content is cached, you'll need to send less of it for every subsequent request.
  • Speed and performance of your website increases.
  • Your content can be made available even if your client is offline.
We'll be using the koa-static-cache middleware to implement caching in our app. Install these middleware using:
$ npm install --save koa-static-cache
Now go to your app.js file and add the following code to it:
var koa = require('koa');
var app = koa();

var path = require('path');
var staticCache = require('koa-static-cache');

app.use(staticCache(path.join(__dirname, 'public'), {
  maxAge: 365 * 24 * 60 * 60  //Add these files to caches for a year
}))

app.listen(3000);
The koa-static-cache middleware is used to cache server responses on client side. The cache-control header is set according to options we provide while initializing the cache object. We have set the expiration time of this cached response to 1 year. Below are the comparision of requests we have sent before and after the file was cached.
Before this file was cached, the returned status code was 200, ie, OK. The response headers had multiple information regarding the content to be cached and had also given us an ETag for the content.
The next time the request was sent, it was sent along with the etag. Since our content hadn't changed on the server, its corresponding Etag also remained the same and the client was told that the copy it has locally is up to date with what the server would provide and should use the local one instead of requesting again.
NOTE: For invalidating any cached file, you just need to change its file name and update the reference to it. This will ensure that you have a new file to send to the client and client cant load it back from the cache.

No comments:

Post a Comment