fullOfQuestion
fullOfQuestion

Reputation: 605

keep data in memory instead of saving in database java

I want to run a java function and it generates a json that has about 1M size. I need the json for input of next call that is 2 minute later. I can save the json in a database, but the time is spent on saving in database is not acceptable for me. can I keep this data in memory and use for next call? I need also read this data from node.js. how can I do this job?

Upvotes: 0

Views: 2388

Answers (2)

Vik Gamov
Vik Gamov

Reputation: 5456

hazelcast is a Java library that provides API's to solve caching use case. It extends java collections with capabilities suitable for caching use case - eviction, ttl for entries, read through and write through cache, etc.

For node.js use case please find my answer here https://stackoverflow.com/a/36704734/27563

Let me know if you have any questions.

Thanks

Upvotes: 1

Alexander Petrov
Alexander Petrov

Reputation: 9492

Why dont you use a persistent asynchronous queue in between you application and your database. This way you will just fire and forget the persist operation and serve the result as fast as possible.

If you want to also keep the object in memory your best bet would be something like Infinispan or Hazelcast. Infinispan offers it own persistent store for the cache and good database integration. Hazelcast on the other end works more as In memory key value store but some persistence can easily be implemented with it as well. Hazel cast is very easy to start with and the learning curve is not that steep.

The good thing about this infrastructure is that you can have safety that your data is in sync with database. For example you can configure how many backups of particular object to be kept and these backups are created asynchronously or synchronously depending on how you configure them. You can also send the data to the database. If persistence is strong requirement probably Infinispan is better in this regards.

When I was reading second time your post I realized that maybe you need something significantly simpler when it comes to caching. If you just need a local cache with no backup capabilities, just go for EHCache.

Upvotes: 2

Related Questions