Comments (9)
@AnandChowdhary Fraud looks like a nice project! Glad to see that you built something useful for you business on nodecache!
from node-cache.
Nice idea for a new project that implements nodecache and does just this.
from node-cache.
I'm developing a solution for this requirement during my time in the train.
Have a look: https://github.com/mpneuried/node-cache-persist
It's not published yet, because the documentation is missing and the api is not fixed yet.
from node-cache.
and where is saving cache this project now?
from node-cache.
Had the same problem, just wrote some short functions; no need for a library, the combination of .keys and .mget creates the magic:
const NodeCache = require('node-cache');
const fs = require('fs');
const path = require('path');
const cache = new NodeCache();
const saveCache = (cache, directoryPath = __dirname) => {
cache.keys((err, keys) => {
if (!err) {
cache.mget(keys, (err, cacheData) => {
if (!err) {
const stringifiedData = JSON.stringify(cacheData);
fs.writeFileSync(path.join(directoryPath, 'cache.json'), stringifiedData)
} else {
console.error(err);
}
});
} else {
console.error(err);
}
});
}
const loadCache = (cache, directoryPath = __dirname) => {
const filePath = path.join(directoryPath, 'cache.json');
if (fs.existsSync(filePath)) {
const jsonData = fs.readFileSync(filePath, 'utf8');
const data = JSON.parse(jsonData);
for (const [key, val] of Object.entries(data)) {
if (data.hasOwnProperty(key)) {
cache.set(key, val);
}
}
}
};
loadCache(cache);
//and then put it into an interval or save it after every change or whatever suits your needs
setInterval(() => {
saveCache(cache);
}, 60*1000);
from node-cache.
@supukarmin: fs.writeFileSync(path.join(directoryPath, 'cache.json'), stringifiedData)
This will block your proccess while saving. I think you are better off with fs.writeFile
Actually all fs.*Sync functions you use, have counterparts without the Sync-suffix, that will not block your process and use the callblack-pattern for the flow of your node-app.
from node-cache.
@supukarmin It DOES ALWAYS block your process.
Read more about it here: https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/
Edit: small example to demonstrate:
function blockEverything() {
console.log("now the process will be blocked forever");
while(true) {}
}
setInterval(() => {
console.log("not-blocked");
}, 500)
setTimeout(blockEverything, 2500);
Edit: it seems that @supukarmin deleted a comment here... 😞
from node-cache.
I made a library which uses a file system-based key-JSON storage, based on nodecache. It's kind of the opposite of "save the cache", since the primary source is the file system, but every key which is fetched is then saved in the cache. If you're still interested in having a file system store for your cache, check it out: https://github.com/AnandChowdhary/fraud/.
from node-cache.
Closed because the filesystem is out of scope (at least for now)
from node-cache.
Related Issues (20)
- Compatible with Node 14? HOT 1
- Library is not Simple and fast NodeJS internal caching HOT 6
- can we do something like if it's not accessed for x amount of time delete it? HOT 1
- General cache stats HOT 1
- Make NodeCache compliant con the Map interface on future releases
- Persisting node-cache HOT 6
- What does negative vsize value mean?
- Takes long time to cache (set&get) large JSON objects HOT 2
- Invalidate cache from command line tool HOT 1
- Expired value HOT 1
- useClone on a value by value basis
- Use one instance of NodeCache per DB collection... HOT 2
- Bug report
- Lack of fetch function HOT 1
- It costs too much time!!!!!!!!! HOT 2
- Is there any reason why this can't be used on the frontend? HOT 4
- node-cache is automatically empty on node js vercel deployment HOT 2
- Can I access the cache across different script? HOT 3
- Recursive call: RangeError: Maximum call stack size exceeded when deleteOnExpire:false
- Getting: Cannot read private member from an object whose class did not declare it
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-cache.