cacheman-file
cacheman-file copied to clipboard
Fix bug in cluster mode.
Hi
this.cache is bind to a cluster, This means, if this.cache is updated on different cluster, key may not present in other clusters and hence returns an empty response. Consider I have two clusters, A and B
A = B = OLD A sets the new data and updates itself as A = NEW but B doesn't update since despite it is reading from file, it relying on this.cache key exist or not.
if (Fs.existsSync(cacheFile)) {
data = Fs.readFileSync(cacheFile);
data = JSON.parse(data);
console.log("LOGGED IN CACHEMAN")
console.log(data) //Despite there is a data here.
} else {
return fn(null, null);
}
if (!this.cache[key]) {
//returns just because this.cache is bind to a cluster.
console.log("EMPTY CACHE OBJECT")
return fn(null, null);
}
Thanks for the comment and it was very helpful. I have written a test case which will fail. https://github.com/VarunBatraIT/cacheman-file/blob/multi-instance/test/test-multi.js This test is for multi-instances which is as good as a cluster. It fails with the current codes. About the rest principle, I agree to that get must not write data and I am not writing any data, I am just maintaining a record of written things which is ok to my best knowledge! This is what I have to do because we are limited by the architecture of code-base of cacheman-file. The problem is, we are relying on this.data for data to exist or not. Even when we read data, data is clearly present, we are returning null just because this.data has no key.
General Architecture of Storage in Question: Despite that data is being stored by a different instance as long as key matches, it should get the data. I can have multiple instances in two different places rather than a global variable. This is how mysql/redis works. There can be multiple instances for both which are writing and retrieving data from the same location and it works. It doesn't matter which instance stored the data, other can retrieve it. Same logic must apply to cacheman file storage. If it was a memory storage, I would have understood since that is a problem with node.
"cacheman-file is meant to be a local temp file store" as long as it works! :+1:
I completely agree with you that redis is clearly the best solution but that doesn't mean that file can't be a solution :) It fails for multi-instances in same app and clusters too.