Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2879

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T09:31:09+00:00 2024-11-26T09:31:09+00:00

Caching in Node.js using Memcached

  • 61k

I've already written articles about caching using Redis and also explained how we can cache our Api using node-cache.

In each of these articles, I've provided little background on using each of them, and in my opinion, Memcached is something I should have added to the list.

One of the great advantages of using Memcached in your applications is stability and performance, not to mention that the system resources it consumes and the space it takes up is minimal.

As with the examples in the articles mentioned above, I'll do something similar today, which is simple but can be easily replicated in your projects.

Let's code

In today's example I'm going to use my favorite framework, but the client we're going to use is agnostic, that is, the code that is present in this article can be reused for other frameworks.

The framework we are going to use today is tinyhttp which is very similar to Express. The reason for its use to me is pretty obvious, but I recommend visiting the github repository.

In addition, we will still install milliparsec, which is a super lightweight body parser, and the Memcached client we will be using will be memjs.

But today's topic isn't about frameworks, so let's start by installing the following dependencies:

npm i @tinyhttp/app @tinyhttp/logger milliparsec memjs 
Enter fullscreen mode Exit fullscreen mode

First we will import our tinnyhttp dependencies and we will register the respective middlewares:

import { App } from "@tinyhttp/app"; import { logger } from "@tinyhttp/logger"; import { json } from "milliparsec";  const app = new App();  app.use(logger()); app.use(json());  // More stuff comes here.  app.listen(3333); 
Enter fullscreen mode Exit fullscreen mode

Now we can create our route, which will only hold one parameter, which in this case will be the id:

app.post("/:id", (req, res) => {   // Logic goes here. }); 
Enter fullscreen mode Exit fullscreen mode

First, let's get the id value of the parameters. Next, we will create an object, in which we will have a property with the value of the id and the remaining properties will be all those coming from the http request body.

app.post("/:id", (req, res) => {   const { id } = req.params;   const data = { id, ...req.body };   // More logic goes here. }); 
Enter fullscreen mode Exit fullscreen mode

Then we will return a response, which will have status code 201 (to indicate that the data was added to Memcached) and the respective object that was created by us.

app.post("/:id", (req, res) => {   const { id } = req.params;   const data = { id, ...req.body };   return res.status(201).json(data); }); 
Enter fullscreen mode Exit fullscreen mode

However, we can't add anything to Memcached yet because it still needs to be configured. So we can already create our client. Like this:

import { App } from "@tinyhttp/app"; import { logger } from "@tinyhttp/logger"; import { json } from "milliparsec"; import { Client } from "memjs";  const app = new App(); const memcached = Client.create();  // Hidden for simplicity 
Enter fullscreen mode Exit fullscreen mode

Now we can go back to our route and add Memcached, for that we'll use the .set() method to input some data.

In this method we will pass three arguments, the first one will be our key, which in this case is the id.

The second argument will be the value of that same key, which we must convert to a string.

The third will be the time you want to persist that same data, in seconds.

In addition to this we will have to make our function asynchronous because the .set() method returns a Promise.

app.post("/:id", async (req, res) => {   const { id } = req.params;   const data = { id, ...req.body };   await memcached.set(id, JSON.stringify(data), { expires: 12 });   return res.status(201).json(data); }); 
Enter fullscreen mode Exit fullscreen mode

The next time you access the route, it will persist in Memcached, but we're not there yet.

That's because we still have to create a middleware that checks if there is a key with id equal to what we are passing in the parameters.

If there is a key equal to the id we passed in the parameters, we'll want to return the value of that key so we don't have to access our controller. If it doesn't exist, we go to our controller to create a new key.

If you're confused, relax because it will soon make sense. In this case, let's create a middleware called verifyCache:

const verifyCache = (req, res, next) => {   // Logic goes here. }; 
Enter fullscreen mode Exit fullscreen mode

First let's get the id value that is passed in the parameters.

const verifyCache = (req, res, next) => {   const { id } = req.params;   // More logic goes here. }; 
Enter fullscreen mode Exit fullscreen mode

Next, we'll use the Memcached client's .get() method. Let's pass two arguments in this method, the first argument will be the id. The second argument will be a callback and will also have two arguments. The first will be the error, the second will be the key value.

const verifyCache = (req, res, next) => {   const { id } = req.params;   memcached.get(id, (err, val) => {     // Even more logic goes here.   }); }; 
Enter fullscreen mode Exit fullscreen mode

If an error occurs, we have to handle it as follows:

const verifyCache = (req, res, next) => {   const { id } = req.params;   memcached.get(id, (err, val) => {     if (err) throw err;     // Even more logic goes here.   }); }; 
Enter fullscreen mode Exit fullscreen mode

Now, see that the key value is non-null, we want to return its value, for that we will send a response with status code 200 (to show that it was obtained from Memcached successfully) and we will send our json object (but first it must be converted from string to json).

If the key value is null, we will proceed to the controller.

const verifyCache = (req, res, next) => {   const { id } = req.params;   memcached.get(id, (err, val) => {     if (err) throw err;     if (val !== null) {       return res.status(200).json(JSON.parse(val));     } else {       return next();     }   }); }; 
Enter fullscreen mode Exit fullscreen mode

Now with the created middleware we just add it to our route:

app.post("/:id", verifyCache, async (req, res) => {   const { id } = req.params;   const data = { id, ...req.body };   await memcached.set(id, JSON.stringify(data), { expires: 12 });   return res.status(201).json(data); }); 
Enter fullscreen mode Exit fullscreen mode

Your final code should look like the following:

import { App } from "@tinyhttp/app"; import { logger } from "@tinyhttp/logger"; import { json } from "milliparsec"; import { Client } from "memjs";  const app = new App(); const memcached = Client.create();  app.use(logger()); app.use(json());  const verifyCache = (req, res, next) => {   const { id } = req.params;   memcached.get(id, (err, val) => {     if (err) throw err;     if (val !== null) {       return res.status(200).json(JSON.parse(val));     } else {       return next();     }   }); };  app.post("/:id", verifyCache, async (req, res) => {   const { id } = req.params;   const data = { id, ...req.body };   await memcached.set(id, JSON.stringify(data), { expires: 12 });   return res.status(201).json(data); });  app.listen(3333); 
Enter fullscreen mode Exit fullscreen mode

Conclusion

As always, I hope I was brief in explaining things and that I didn't confuse you. Have a great day! 🙌 🥳

beginnersjavascriptnodewebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.