Class

com.eharmony.aloha

NoEvictionCache

Related Doc: package aloha

Permalink

class NoEvictionCache extends AnyRef

A cache that is a hybrid of the Memoizer in Listing 5.19 in Java Concurrency in Practice and https://www.bionicspirit.com/blog/2012/07/02/love-scala.html. Makes sure to put a Promise of the memoized value into the cache only if it's not already there. Then only compute the actual value (in a Future) and complete the Promise with the Future only if the promise was successfully inserted atomically. Otherwise, another promise was snuck in so use the Future associated with that Promise.

val cache = new NoEvictionCache
val slow = () => {Thread.sleep(5000); println("executing slow()"); 1}
val t1 = System.nanoTime                                      // Start the clock
val futures = Seq(                                            // Returns immediately (non-blocking)
      cache("one"){slow()},                                   // Only execute slow() once.
      cache("one"){slow()})
val a = Future.fold(futures)(0)(_+_)                          // Returns immediately (non-blocking)
val t2 = System.nanoTime
println("Runtime for setup: " + (1.0e-9*(t2-t1)).toFloat)     // Prints: "Runtime for setup: 0.001041"
a.onSuccess{ case i =>
  val t3 = System.nanoTime                                    // Determine elapsed time.
  println(i + ", runtime: " + (1.0e-9*(t3-t1)).toFloat)       // Prints: "2, runtime: 5.002082"
}
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. NoEvictionCache
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new NoEvictionCache()(implicit ec: ExecutionContext)

    Permalink

    ec

    an execution context in which to run the futures that are created.

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. def apply[A](key: String)(a: ⇒ A)(implicit arg0: RefInfo[A]): Future[A]

    Permalink

    Cache a value.

    Cache a value.

    A

    type of value to be memoized

    key

    string identifier

    a

    call-by-name value to be cached or retrieved from the cache.

    returns

    a Future containing the result of a

  5. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  6. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  7. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  8. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  9. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  10. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  11. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  12. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  13. def logging[A](k: (String, String), inserting: Boolean): Unit

    Permalink

    Made protected so that it can be overridden in a test class to test caching.

    Made protected so that it can be overridden in a test class to test caching.

    Attributes
    protected[this]
  14. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  15. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  17. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  18. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  19. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped