How to cache results in scala?

前端 未结 5 582
孤街浪徒
孤街浪徒 2020-12-31 01:51

This page has a description of Map\'s getOrElseUpdate usage method:

object WithCache{
  val cacheFun1 = collection.mutable.Map[Int, Int]()
  def         


        
相关标签:
5条回答
  • 2020-12-31 02:23

    Since it hasn't been mentioned before let me put on the table the light Spray-Caching that can be used independently from Spray and provides expected size, time-to-live, time-to-idle eviction strategies.

    0 讨论(0)
  • 2020-12-31 02:28

    Take a look at spray caching (super simple to use)

    http://spray.io/documentation/1.1-SNAPSHOT/spray-caching/

    makes the job easy and has some nice features

    for example :

          import spray.caching.{LruCache, Cache}
    
          //this is using Play for a controller example getting something from a user and caching it
          object CacheExampleWithPlay extends Controller{
    
            //this will actually create a ExpiringLruCache and hold data for 48 hours
            val myCache: Cache[String] = LruCache(timeToLive = new FiniteDuration(48, HOURS))
    
            def putSomeThingInTheCache(@PathParam("getSomeThing") someThing: String) = Action {
              //put received data from the user in the cache
              myCache(someThing, () => future(someThing))
              Ok(someThing)
            }
    
            def checkIfSomeThingInTheCache(@PathParam("checkSomeThing") someThing: String) = Action {
              if (myCache.get(someThing).isDefined)
                Ok(s"just $someThing found this in the cache")
              else
                NotFound(s"$someThing NOT found this in the cache")
            }
          }
    
    0 讨论(0)
  • 2020-12-31 02:36

    On the scala mailing list they sometimes point to the MapMaker in the Google collections library. You might want to have a look at that.

    0 讨论(0)
  • 2020-12-31 02:44

    See the Memo pattern and the Scalaz implementation of said paper.

    Also check out a STM implementation such as Akka.

    Not that this is only local caching so you might want to lookinto a distributed cache or STM such as CCSTM, Terracotta or Hazelcast

    0 讨论(0)
  • 2020-12-31 02:46

    For simple caching needs, I'm still using Guava cache solution in Scala as well. Lightweight and battle tested.

    If it fit's your requirements and constraints generally outlined below, it could be a great option:

    • Willing to spend some memory to improve speed.
    • Expecting that keys will sometimes get queried more than once.
    • Your cache will not need to store more data than what would fit in RAM. (Guava caches are local to a single run of your application. They do not store data in files, or on outside servers.)

    Example for using it will be something like this:

      lazy val cachedData = CacheBuilder.newBuilder()
        .expireAfterWrite(60, TimeUnit.MINUTES)
        .maximumSize(10)
        .build(
          new CacheLoader[Key, Data] {
            def load(key: Key): Data = {
              veryExpansiveDataCreation(key)
            }
          }
        )
    

    To read from it, you can use something like:

      def cachedData(ketToData: Key): Data = {
        try {
          return cachedData.get(ketToData)
        } catch {
          case ee: Exception => throw new YourSpecialException(ee.getMessage);
        }
      }
    
    0 讨论(0)
提交回复
热议问题