VOOZH about

URL: https://www.javacodegeeks.com/2016/12/quick-tip-prevent-caches-exploding.html

⇱ Quick Tip To Prevent Your Caches From Exploding - Java Code Geeks


There are many scenarios when you can benefit from caching commonly used objects in your application, especially in web and micro-service oriented environments. The most simple type of caching you can do in Java is probably to introduce a private HashMap that you query before calculating an object to make sure you don’t do the job twice.

Here is an example of that:

public class PrimeService {

 private Map<Long, BigInteger> cache = new HashMap<>();
 
 public BigInteger getPrime(long minimum) {
 return cache.computeIfAbsent(minimum, 
 m -> BigInteger.valueOf(m).nextProbablePrime()
 );
 }
 
}

This is a quick solution to the problem, but sadly not very efficient. After a few months of people entering all kinds of crazy numbers into the service, we will have a very large HashMap that might cause us to run out of memory.

Here is a quick trick to solve that. Instead of using a HashMap, you can use a LinkedHashMap and simply override the method removeEldestEntry. That allows you to configure your own limit function to prevent the map from exploding.

public class PrimeService {

 public final static int CACHE_MAX_SIZE = 1_000;

 private Map<Long, BigInteger> cache = new LinkedHashMap<>() {
 @Override
 protected boolean removeEldestEntry(Map.Entry<ID, Boolean> eldest) {
 return size() > PrimeService.CACHE_MAX_SIZE;
 }
 };
 
 public BigInteger getPrime(long minimum) {
 return cache.computeIfAbsent(minimum, 
 m -> BigInteger.valueOf(m).nextProbablePrime()
 );
 }
 
}

We have now successfully limited the cache, preventing it from explosion. Old entries will be removed as new ones are added. It should be noted that this soluton works well in small applications, but for more advanced scenarious you might want to use an external caching solution instead. If you are interested in caching entries in a SQL database without writing tons of SQL code, I would recommend Speedment since it is lightweight and has a very fluent API.

Until next time!

Reference: Quick Tip To Prevent Your Caches From Exploding from our JCG partner Emil Forslund at the Age of Java blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Emil Forslund
Emil Forslund
December 22nd, 2016Last Updated: December 21st, 2016
0 121 1 minute read
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz