How to use node.js mongoDB cache

Asked 2 years ago, Updated 2 years ago, 90 views

We are developing node.js + express + mongoDB. There are times when I call up the db and organize it like a bulletin board, but it doesn't change that often, so it feels very inefficient to do DB I/O every time the page is loaded. Is there a way to create a DB cache on nodejs server so that it can only be updated when there is a change in the DB?

node.js mongodb

2022-09-21 15:40

1 Answers

Why don't you use CashDivis like Memcached or Redis?

Create data at logic end, update cache at update

If it's in the cache, if it's not in the cache, we can just put it in the cache and bring it inYo

To write roughly in pseudocode(?),

if create or update // when recording
    db.save // save it in the DB
    cache.set(post, text) // write to cache

When I bring it up from the main page,

if cache.has(post) // If there is data in the cache,
    return cache.get(post) // return existing data

Else //What if there is no data in the cache?
    db.get // get it out of the D.B
    Cache.set(post, text) // Save to Cache

    return post // save and return

I think it's like this


2022-09-21 15:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.