I have a api export with a lot of data so I tried to used the node-cache so the api will be faster since take more than 2 min to get the data, I am new on this so I found the follow code: Notes: env node js/express
const NodeCache = require("node-cache");
const axios = require('axios');
const myCache = new NodeCache({stdTTL:100000})
axios.get('http://localhost:5000/test/example)
.then(function (response) {
console.log(response)
myCache.set("exampleCache",response,10000);
res.send(response)
Any suggestion or recommend will be great!
By default, the NodeCache module clones the object before saving it.
In your case, you're trying to cache the Axios response object, which cannot be cloned by NodeCache (it's a big and complex object), so you're getting the error.
Instead, I recommend cloning the HTTP response data (response.data), which is much smaller and clonable. Or, you can clone the whole Axios response object, like in your code example, but you need to set the option {useClones: false} during initialization of NodeCache, this way the NodeCache will save just the reference to the object.
I was getting the same error, what I did was to stringify the content before saving it and parse it after retrieval. It worked like a charm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With