I want to implement a write-behind and cache aside strategy at the same time. Basically, I want to read data from the cache if it exists in the cache otherwise fetch data from the database and set it to cache so can I read the same data from cache in next request.
I also want to functionality when anyone wants to update data first update cache data then after a time(eg. every 2 mints) cache automatically update the database.
I don't know which redis function can do this for me, can anybody give me a proper resource. If you tell me all code with this functionality it will be so helpful for me.
I did a lot of google, All are telling the strategy of write-behind and cache aside. But not anybody is telling me proper code and function which i will use for write back/behind cache in Node JS.
For this application, I am using node js.
Thanks A lot!!
Take a look at the following project that uses RedisGears (https://oss.redislabs.com/redisgears/) to implement write-behind and write-through on Redis.
https://github.com/RedisGears/rgsync
Related
I'm trying to use cache to prevent overlapping access to the database, and I wonder which of the two is the better option. And for example, when you want to save bulletin board information in cache, how do you determine if a message is added to the bulletin board? If you are in cache when you send an api request, you can get the data in cache and do it more efficiently, but don't you need access to the database to understand the data change?
I'm using node js along with mongo db. I need help for something related to caching. I have certain data like trending posts which will be updated for every 15 mins. But for a given instance of time period all the users making the api call for the trending posts data will get same response. Hence is there any way to save the cache data from db and update it periodically for 15 mins.
Note: I doubt if storing the data in json format in a file in server will be helpful? Is it prone to attacks or malicious usages? This data contains some confidential information too. So I guess that is not a good idea to store it in files. So is there anyother methods???
As mentioned by Kedar, this is where you would use some type of distributed cache like Redis.
There are NPM modules like mongodb-redis-cache
(https://www.npmjs.com/package/mongodb-redis-cache)
They syntax is quite simple, just append it onto the select. (it does require a redis server though, if you are not familiar with Redis or setting one up, I would look at a more managed solution then like on Digital ocean etc )
The idea here is that each call to Mongo will first check if there is a 'cached' key/value in redis, if not it runs the query, otherwise it will just pull from Redis.
My goals are to:
be able to set an item by key
be able to retrieve the item by key
set an expiration on each item
be able to invoke a callback upon expiration to refresh it.
Is there any JavaScript library that can do all that?
I think it depends on whether you'd like to do server side or client side caching. You could likely use the NPM node-cache module. If you're looking for a more detailed walk through on how to cache, take a look at this URL at scotch.io (https://scotch.io/tutorials/how-to-optimize-node-requests-with-simple-caching-strategies). That example uses Express and memory-cache. Hope this helps!
I build an API, which will send data to another API when has been collect 10 hashes. The client sends 1 hash per hour.
For example:
The client POST hash to API
API need to store it somewhere until the hashes number becomes to 10
When the number of hashes becomes 10 need to send data to another API and start from 0 again
My question related to the 2nd point. I can store the hashes in the array, the problem is that the data will be lost when the server will be shut down suddenly.
This is the only data which I need to store in API, so I don't want to use DBS.
By the way, it's my first time of developing API, so will be glad to your help.
Thanks in advance.
Sorry but your only options of storing data are either memory or disk.
If you store data in variables, you're using memory. It is fast and instant but it's not durable as you already said.
If you store data in database, you're using disk storage. It is slower but it is durable.
If you need durability, then database is your only option. Or maybe if you don't want to store the data in your machine, you could use cloud database such as firebase database.
Maybe your problem will be solved with Redis.
I had one feature where I needed to use some user's pieces of information on the server side in runtime and it could not be persisted at the database.
So, I used this.
In simple words, the redis will save the information in your cache and you can retrieve when you need.
There's no disk use and are more stable than a hand made memory control.
I hope this helps you.
I'm writing an auth module that contains several functions so my server can authenticate with an oAuth2 system using client_credentials. In the module I want to cache / save the credentials since they don't expire for some time (I'll refresh as needed).
Whats the best way to store the credentials?
Should I just create a var at the top of my node module? Should I create a class and instantiate it (const auth = new MyClass()) where my makes subsequent API calls (with the Bearer token)?
Creating a var variable means to store data in RAM, which is not a comprehensive approach.
I suggest you look at Redis, which was developed mostly for your purposes.
Redis is used as a database and for cache, since it’s super fast because the data is stored “in-memory” contrary to other databases in which the data is usually stored “on-disk.”
Moreover, there some thoughts about the fact that Node.js is less efficient at storing data than Redis. This article will clear more about my point.
So, in general, I guess using Redis will bring you more advantages in storing credentials.