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:

  1. 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

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. 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

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. 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.

    ReplyDelete
  6. 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

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

    ReplyDelete
  8. 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.

    ReplyDelete
  9. 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

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. 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.

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. 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.

    ReplyDelete

  14. 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

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

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

    ReplyDelete
  17. 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

    ReplyDelete
  18. 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.

    ReplyDelete
  19. 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

    ReplyDelete
  20. 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

    ReplyDelete