How to implement cache (LRU Cache) using LinkedHashMap in java

Recently i wanted to implement a simple Least recently used (LRU) cache in one my applications. But my use case is simple enough that instead of going for something ehcache i decided to build it on own by using java.util.LinkedHashMap
As you can see from the code its very simple. All you have to do is extend java.util.LinkedHashMap and override its protected removeEldestEntry() method so that it checks if the size of map is greater than a size you specified while creating the Map if yes remove the eldest entry
Now the question is when Map is full which entry will it remove, you have 2 options
  1. Eldest: If you just want to remove the first entry that you inserted in the Map when adding a new entry then in your constructor you could use super(cacheSize, 0.75f);, so LinkedHashMap wont keep track of when a particular entry were accessed.
  2. Least recently used (LRU): But if you want to make sure that the entry that was least recently used should be removed then call super(cacheSize, 0.75f, true); from constructor of your LRUCache so that LinkedHashMap keeps track of when entry was accessed and removes the Least recently used entry

27 comments:

Anonymous said...

Here is the pic of the setup I have<

you can look at the image over here https://www.dropbox.com/s/ubik71rxfz2rg25/issue-class-not-found.JPG?dl=0

Ancy merina said...
This comment has been removed by the author.
Anonymous said...

I would like to say thank you Sunil for sharing this code over here. I am also working on Java and the codes you have shared here was very useful for me. implementing cache is necessary in various application and thank you so much for the updates.webdesign magazine

xcod4r said...

Swing Code Examples
to help learn java

Angelicajames said...
This comment has been removed by the author.
Angelicajames said...

Thanks for the post quite helpful to me. As you have mentioned with the help of java, we are creating a lot of tools that help us to make new software tools which help to improve the performance.

As a small business owner, there’s one relationship you have to maintain, and that’s the one with your customers and with your employees, Because if you give time to your company then you will get growth of customers and client in your industry. Chiropractic Management Software offers a lot of perspective in Chiropractic industry and no matter how much technology changes, one thing that remains constant is that hiring is the key to growth. When you add employees, you give your business the potential to expand the services it offers, as well as improve its efficiency. Chiropractors can manage appointments and patient files in the cloud. Free Medicare and online consultant claiming.

Segment Web said...

Hi there, nice to see your blog. Good articles.

If you want to logo designing services than visit - Logo Designing Services Jaipur | Logo Designer in Jaipur | Logo maker in Jaipur

Mobile App Development company in Mumbai said...

Thanks for sharing valuable Information, I really very impressive on your blog. I hope you continue on blogging job.

4 Square Logic said...

The purpose of a service-based business website is to convince website visitors that they should become customers of the service company. This is done by positioning the company as a dependable, trustworthy and experienced service provider in the target market. If you want all these things in your business then start your approach from a website and Join our Best Website development services we are building an outstanding website with best latest inventive features.

Norton.com/setup said...

QuickBooks Desktop Tech Support Phone Number - With the latest software release of QuickBooks Desktop 2019, it has successfully outgrown other business software products in the competitive market. QuickBooks Desktop gives you the flexibility to manage your accounting, inventory, point of sale, payroll, payment and increase your productivity. It also allows you to track items, vendors and customers.

QuickBooks POS Tech Support Phone Number | QuickBooks Payroll Tech Support Phone Number

super hero said...
This comment has been removed by the author.
super hero said...

Misbah Digital Marketing is an excellent online marketing agency in Bangalore.Misbah Digital Marketing!
Misbah Digital Marketing! having certified professionals in Google Ads, Google Analytics and Bing Ads.

Shree Priya said...
This comment has been removed by the author.
youtubecomactivate said...

If you are facing any problem with YouTube not working your device like smart TV or Roku device etc. so you can contact us, Our YouTube Activate customer care team 24*7 available for resolving your issues, furthermore gives the safest solution by simple steps.

Trust Logics said...

Jobs in Hyderabad | Openings on Angular JS

Catherine said...

Very good article Characteristics of Typography

Techsolve Solutions said...

Thanks for sharing useful information. Keep on sharing.

Web designer in canada
Digital marketing agency in canada

DragonH said...

You know your projects stand out of the herd.
λ¨ΉνŠ€κ²€μ¦

wrecking ball said...


Nice post. I used to be checking constantly this blog and I am impressed! Extremely useful info particularly the ultimate section πŸ™‚ I take care of such information a lot. I was seeking this certain information for a long time. Thank you and best of luck.
essay on cristiano ronaldo

shakunthala said...

found informative blog
keep it up.
looking forward to it.
job guaranteed courses in bangalore

rahulramesh said...

It keeps me to engage on the content and it gives good explanation of the topics.
JAVA Training in Chennai
Java Online Course
JAVA Training in Bangalore
JVM Architecture

Dissertation writing services uk said...

I have read it completely and got really impressed. Love to read more about similar types of articles. Thanks a lot!!

thesttl said...

Very good post thanks for sharing the article with us if you want to change your life with digital transformation services and offshore software development then do visit the site

Anonymous said...

Thank you for the valubale post...

https://tensionmatleyaar.com/
https://movementbuzz.com/
http://www.sportfunda.com/

Anonymous said...

Great post. I was checking continuously this blog, and I am impressed! Are you want to know that what is USA Dedicated Server? if yes, then I can help you and I will give you the best info.

Accounting Service said...

Great Sevice blog ! I If you're having trouble with QuickBooks or just want to know more about the software, you can dial
Quickbooks Customer Support+1 855-377-7767

Quickbooks Customer Service said...

Very Nice Quickbooks Informations If you have any query rabout your Quickbooks account, please contact regarding Quickbooks Software then Contact Quickbooks Customer Support +1 855-604-1500