Sunday, June 7, 2015

Finding the best in-memory LRU cache for Java/Scala

For one of our application we were standing before a decision - for every request we need to do a remote call. But doing so would increase our response time significantly. Fortunately, these calls can be easily cached and it can be fitted into RAM so we were not forced to use some external cache like Memcached.


So we were looking for a cache implementation on top of JVM (preferably Java 8 or Scala) and I was surprised how many good looking possibilities exists.
  • There is a cache as a part of Hystrix but that is only PWR (per web request)
  • Guava contains a Cache that looks very promising and has most of the features we were looking for
  • After some research our winner was Caffeine with a very nice documentation and API. By the way, this cache is also used in Spray


And how did we use it? First of all, we wanted a LRU cache. So cache that will be able to operate with a limited size (of RAM) and throw away the least important item when the size goes beyond a treshold.

Strongly inspired by the Spray implementation, this is how our Caffeine integration (in Scala) looks like:
package com.avast.someproject.crosscutting.cache

import com.github.benmanes.caffeine.cache.Caffeine

import scala.concurrent.{ExecutionContext, Future, Promise}

class CaffeineCache[V](cacheName: String, val maxCapacity: Int, val initialCapacity: Int) extends Cache[V] {
 private val store: com.github.benmanes.caffeine.cache.Cache[Object, Future[V]] = Caffeine.newBuilder()
   .initialCapacity(initialCapacity)
   .maximumSize(maxCapacity)
   .recordStats()
   .build()

 override def get(key: Object): Option[Future[V]] = {
   val valueFromCache = store.getIfPresent(key)
   if (valueFromCache == null) None else Some(valueFromCache)
 }

 override def get(key: Object, genValue: Object ⇒ Future[V])(implicit ec: ExecutionContext): Future[V] = {
   val promise = Promise[V]()
   store.get(key, (key:Object) => promise.future) match {
     case null => Future { throw new RuntimeException("Unable to retrieve value from cache") }
     case futureResult if futureResult == promise.future => {
       val future = genValue(key)
       future.onComplete { value ⇒
         promise.complete(value)
         // in case of exceptions we remove the cache entry (i.e. try again later)
         if (value.isFailure) {
           store.invalidate(key)
         }
       }
       promise.future
     }
     case futureResult => {
       futureResult
     }
   }
 }
}

66 comments:

  1. Please consider using AsyncLoadingCache with the scala-java8-compat bridge (FutureConverters). If you want to ensure the processing thread is on the ExecutionContext, you can bridge that to a Java Executor and provide it to the cache's builder. By letting the cache handle the asynchronous logic you simplify your code and avoid minor mistakes (e.g. incorrect statistics). For style, you can use an import renaming to avoid the class name clash on Cache.

    Spray uses a original library so they added asynchronous computations feature manually. That library begat Guava's, where asynchronous support and performance fixes were planned but never happened. Thanks to Java 8, there was an excuse to give it another shot in Caffeine. I'm glad its working out for you.

    Cheers,
    Ben

    ReplyDelete
    Replies
    1. Java Training Institutes Java Training Institutes
      Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

      Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai

      Delete
    2. Great post. This article is really very interesting and enjoyable. I think it’s must be helpful and informative for us. Thanks for sharing your nice post. study in australia consultant in jalandhar

      Delete
  2. Cloud has beome the common word that is being used by most of the professional these day. The reason for relying on this technology is security. Your content too lecture the same. Thanks for sharing this worth able information in here. Keep blogging article like this.

    Hadoop Training Chennai | Hadoop Training in Chennai | Big Data Training in Chennai

    ReplyDelete
  3. Dear admin, The way you have explained the concept is mezmerizing. Thank you so much for sharing tis worth able content with us. The concept taken here will be useful for my future programs and i will surely implement them in my study. Keep blogging article like this.


    JAVA Training in Chennai | Best JAVA Training in Chennai | Android training in chennai

    ReplyDelete
  4. Thanks for sharing such informative article on Loadrunner Automation testing tool. This load testing tool will provide most precise information about the quality of software. Loadrunner Training in Chennai | Best Loadrunner training institute in Chennai

    ReplyDelete
  5. Excellent Post!!! Your interview questions on QTP automation tool will assist freshers and experienced professionals to sharpen their skills and be successful in job interview. QTP Training in Chennai | QTP training

    ReplyDelete
  6. We are using Caffeine from Scala via scala-cache, check out https://github.com/cb372/scalacache#caffeine

    ReplyDelete
  7. The expansion of internet and other business intelligence leads to large volume of data. Industries are looking for talented professionals to maintain and process huge volume of data with latest tools available in the market. Taking Hadoop Training in Chennai | Big Data Training in Chennai will ensure better career prospects for talented professionals.

    ReplyDelete
  8. Great Blog!!

    WE are offering website services and Design in affordable price for your Business website or else!!!!
    ios 7 app design

    ReplyDelete
  9. Java is the best language this easy to learning on this useful article thanks for sharing looking most of the professionals knowledge great post! safety management course in chennai|Industrial safety management course in Chennai|diploma in fire and safety courses in chennai|iosh course training in india|nebosh courses in Tamil nadu keep posting!

    ReplyDelete
  10. Very happy to see this blog. Gives a wonderful information with coded explanaion. Thank you for this blog. very useful to me.
    Hadoop Training in Chennai

    ReplyDelete
  11. HTML5 is the recently arrived most trending technology that has bright future. If you are interested in studying HTML5 training visit this website.
    html5 training in chennai | html5 training chennai | html5 course in chennai

    ReplyDelete
  12. The future of software testing is on positive note. It offers huge career prospects for talented professionals to be skilled software testers. software testing course in Chennai | Software Testing Training in Chennai | Software testing course in Chennai

    ReplyDelete
  13. This is the best application on the drop today. There access to enjoy great moments of relaxation: age of war 2|
    age of war 5
    Great! Thanks for sharing the information.Summon creatures to fight enemy units and demolish the opposing castle. Your castle is equipped with a crossbow, which you can use to shoot enemies age of war 6. Make sure you upgrade skills to increase your chances of winning battles.
    The goal of Age of War is to survive longer than the computer and to outlast him you’ll need to train the right troops while balancing your offence and defence in this high paced, quick thinking flash game age of war 4
    . Train troops of you own to combat the computers. As you kill off the computer troops, you will gain EXP and you will eventually advance to the next age.
    Choose a starter deck and prepare for an epic war!age of war 3

    Command your units in each battle to attack the enemy’s castle, while protecting your own base earn to die 2. Earn and upgrade cards to help you conquer the land.age of war

    happy wheels | tank trouble
    Thanks for the best blog.it was very useful for me.keep sharing such ideas in the future as well.this was actually what i was looking for,and i am glad to came here!
    cubefield It contains a plethora of tools and objects for level building such as harpoon guns,blocks and vans. Users can upload their maps to a public server where they are accessible

    ReplyDelete
  14. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it. The Struts, Spring, Hibernate are the advanced level of programming language which are most widely used.
    hibernate training in chennai | hibernate training

    ReplyDelete
  15. The usage of third party storage system for the data storage can be avoided in cloud computing and we can store, access the data through internet.
    cloud computing training in chennai | cloud computing courses in chennai

    ReplyDelete
  16. Ethical hacking describes hacking performed by a company or individual to help them to identify potential threats on a computer or network.
    Ethical hacking Course in Chennai | Ethical hacking Training in Chennai

    ReplyDelete
  17. Updating with the latest technology and implementing it is the only way to survive in our niche. Thanks for making me this article. You have done a great job by sharing this content in here. Keep writing article like this.
    SAS Training in Chennai | SAS Course in Chennai

    ReplyDelete
  18. Excellent post ! keep sharing such a useful post.

    php course in chennai

    ReplyDelete
  19. Great blog! nice post you have shared. Thanks for sharing.

    web design training in chennai

    ReplyDelete
  20. Very useful content thanks for sharing such a informative content which provided me the required information on the various technology.
    AngularJS Training in Chennai | AngularJS course in Chennai

    ReplyDelete
  21. Thanks for Sharing the valuable information and thanks for sharing the wonderful article..We are glad to see such a wonderful article..
    QTP Training in Chennai | QTP Training Institute in Chennai | QTP Training

    ReplyDelete
  22. Thanks for sharing this informative content that guided me to know the details about the training offered in different technology.
    Web Designing Course in Chennai | web designing training in chennai

    ReplyDelete
  23. Nice interesting information on the latest arrived technology which helped me to get update according to the recent trends.
    Salesforce Training in Chennai | Salesforce Course in Chennai

    ReplyDelete
  24. Very interesting content which helps me to get the in depth knowledge about the technology. To know more details about the course visit this website.
    Digital marketing course in Chennai | Digital marketing training in Chennai

    ReplyDelete
  25. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Dot net training in Chennai | dot net course in Chennai

    ReplyDelete
  26. Excellent post!!! The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    oracle training in chennai

    ReplyDelete
  27. Its interesting and informative. Thanks for your great information! erp in chennai|erp solutions chennai

    ReplyDelete
  28. Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

    iOS Training in Chennai

    ReplyDelete
  29. I really enjoyed while reading your article, the information you have delivered in this post was damn good. Keep sharing your post with efficient news.
    Regards,
    Salesforce Training in Chennai|Salesforce Training

    ReplyDelete
  30. Thanks for your great information. Sign up and Register Your Events Today! Upcoming Events in India. We make registration process easier and track number of students attending the event.

    ReplyDelete
  31. Thanks for your great information. Keep Updating.ERP in Chennai | ERP Providers in Chennai. Brave Technologies Private Limited is an one of the best Low Cost ERP Software Solution for all Industries.

    ReplyDelete
  32. Hi, Really your post was very informative. Today's internet era learn Hadoop Online Training will helps you to reach your goal.Selenium Training

    ReplyDelete
  33. I have read your blog its very Interesting. Thanks for sharing. ERP Providers in Chennai | ERP in Chennai. We provide best quality cost effective ERP software applications and end to end enterprise solutions to all the industry verticals with the affordable cost.

    ReplyDelete
  34. Your blog is fantastic, good quality perform. I know several can accept myself even when they just don't take the time to inform you. Case Study Solution

    ReplyDelete
  35. Really Nice Blog. Thank you for Sharing. We are the best erp software providers in chennai. For more details call +91 9677025199 or email us on info@bravetechnologies.in.
    ERP in Chennai

    ReplyDelete
  36. Excellent post. I have read your blog it's very interesting and informative. Keep sharing.
    erp software in chennai

    ReplyDelete
  37. thank you for such a great article with us. hope it will be much useful for us. please keep on updating..
    Web designing course in chennai

    ReplyDelete
  38. Actuary Assignment Help, Actuarial science assignment help in the actuarial field of science from primary to complex level for the understudies of US UK.
    Actuary Finance Assignment Help

    ReplyDelete
  39. Nice Blog.Thank you for Sharing. I'm working in brave technologies private limited.we are leading cloud erp software solution in chennai

    ReplyDelete
  40. very usefull informatation.and iam expecting more posts like this please keep updating us........

    ReplyDelete
  41. Awesome blog with useful content., Thanks for sharing such a nice blog. Java Training in Chennai | Dot Net Training in Chennai

    ReplyDelete
  42. Awesome blog. I enjoyed reading your articles. This is truly a great read for me.
    Suchmaschinenoptimierung in Lüdenscheid

    ReplyDelete

  43. The website is looking bit flashy and it catches the visitors eyes. A design is pretty simple and a good user-friendly interface.

    Programmierung in Lüdenscheid

    ReplyDelete