I am implementing a node.js server running a matching algorithm. Since I want to keep the server responding as quickly as possible, I would like to be able to retrieve data from a cache instead of querying the database every single time. For example, I need to keep information about 10000 - 50000 users. My fear is that this would take up a lot of RAM. Is there any way to store that much data in a cache without the risk of running out of RAM?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- Keeping track of variable instances
You can use memcached or redis, who themselves care about memory and its lack removed obsolete data
node-memchache is faster than redis and is easier to implement. You can see https://github.com/ptarjan/node-cache
from what I read as to what you are trying to do it seems you are contradicting yourself. AS to how I see, using cache has the following objectives:
Now, based on what I read from your question it seems your objective if #2. If that is so, then you have no option but to size for how much data do you want to keep in memory. That sizing should then be used to have enough RAM to hold the results and not going back to teh database.
Caching frameworks like ehCache also provide the option of swapping data from disk. Check this link from ehCache (http://www.ehcache.org/documentation/user-guide/storage-options#memory-use-spooling-and-expiry-strategy); this should help you design for how the cache should be setup aka eviction strategies to keep the sizes controllable and also when (and how) you should be ready to spool the data to a underlying disk-storage. Spooling to a disk means you would need to have HDD. But then you would need some benchmarking as to if DB is faster than the underlying cache-store.
you can chose in JVM caches like ehCachs, JCS or even go for much more sophisticated frameworks like memcache, redis which will do all this for you OOTB.
cheers kapil
With mcache it's fairly easy to cache data from database queries in Node.js.
Here is an example for caching a query from MySQL: