All Downloads are FREE. Search and download functionalities are using the official Maven repository.

im.actor.util.cache.CacheHelpers.scala Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package im.actor.util.cache

import com.github.benmanes.caffeine.cache.{ Caffeine, Cache }

import scala.concurrent.{ ExecutionContext, Future }

object CacheHelpers {

  def createCache[K <: AnyRef, V <: AnyRef](maxSize: Long): Cache[K, V] = Caffeine.newBuilder().maximumSize(maxSize).build[K, V]

  def withCachedFuture[K, V](key: K)(future: ⇒ Future[V])(
    implicit
    cache: Cache[K, Future[V]],
    ec:    ExecutionContext
  ): Future[V] =
    Option(cache getIfPresent key) match {
      case Some(result) ⇒
        result
      case None ⇒
        val result = future
        cache.put(key, result)

        result recover {
          case e ⇒
            cache.invalidate(key)
            throw e
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy