com.ybrikman.ping.scalaapi.dedupe

Cache

Related Docs: object Cache | package dedupe

class Cache[K, V] extends AnyRef

A Scala wrapper for a Java's ConcurrentHashMap (CHM). Exposes the basic underlying methods of CHM and adds a getOrElseUpdate(key, value) method that lazily evaluates the value parameter only if the key is not already present in the cache.

You may be asking, why not just use Scala's ConcurrentMap interface, which already has a getOrElseUpdate method?

val cache = new ConcurrentHashMap().asScala cache.getOrElseUpdate("foo", "bar") // BAD idea

The answer is because this method is inherited from the MapLike trait, and is NOT a thread safe (atomic) operation!

The strategy used in the class below is to wrap all values with a LazyWrapper class that only evaluates the value when explicitly accessed. In the getOrElseUpdate method, we avoid accessing the passed in value unless we know it was the one actually inserted into the cache.

For more info, see: http://boundary.com/blog/2011/05/

TODO: investigate if boundary's NonBlockingHashMap is as good as they say it is (and what tests they have to prove it).

TODO: Java-friendly API

K
V

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Cache
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Cache()

    Empty constructor that uses default values for initial capacity, concurrency level, and load factor

    Empty constructor that uses default values for initial capacity, concurrency level, and load factor

    returns

  2. new Cache(config: Configuration)

    Overloaded constructor that creates the cache with initial capacity, concurrency level, and load factor read from config

    Overloaded constructor that creates the cache with initial capacity, concurrency level, and load factor read from config

    config
    returns

  3. new Cache(initialCapacity: Int, loadFactor: Float, concurrencyLevel: Int)

    initialCapacity
    loadFactor
    concurrencyLevel

Value Members

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

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

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

    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  5. def clear(): Unit

    Remove all keys and values from the cache

  6. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  7. def contains(key: K): Boolean

    Returns true if this key is associated with a value in the cache and false otherwise.

    Returns true if this key is associated with a value in the cache and false otherwise.

    key
    returns

  8. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  9. def equals(other: Any): Boolean

    Definition Classes
    Cache → AnyRef → Any
  10. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. def get(key: K): Option[V]

    Optionally return the value associated with the given key

    Optionally return the value associated with the given key

    key
    returns

  12. def getAll: Map[K, V]

    Returns all elements of the cache.

    Returns all elements of the cache. Use this method only if you really need all of the elements. Calling it will cause all lazy values to be calculated.

  13. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  14. def getOrElseUpdate(key: K, value: ⇒ V): V

    Get the value associated with the given key.

    Get the value associated with the given key. If no value is already associated, then associate the given value with the key and use it as the return value.

    Like Scala's ConcurrentMap, the value parameter will be lazily evaluated: that is, it'll only be evaluated if there wasn't already a value associated with the given key. However, unlike Scala's ConcurrentMap, this method is a thread safe (atomic) operation.

    key
    value
    returns

  15. def hashCode(): Int

    Definition Classes
    Cache → AnyRef → Any
  16. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  17. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  18. final def notify(): Unit

    Definition Classes
    AnyRef
  19. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  20. def put(key: K, value: V): Option[V]

    Associate the given key with the given value.

    Associate the given key with the given value. Optionally return any value previously associated with the key.

    key
    value
    returns

  21. def putIfAbsent(key: K, value: V): Option[V]

    If the given key is already associated with a value, return that value.

    If the given key is already associated with a value, return that value. Otherwise, associate the key with the given value and return None.

    key
    value
    returns

  22. def remove(key: K): Option[V]

    Remove the key and any associated value from the cache.

    Remove the key and any associated value from the cache. Optionally return any previously associated value.

    key
    returns

  23. def size: Int

    Return how many elements are in the cache

    Return how many elements are in the cache

    returns

  24. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  25. def toString(): String

    Definition Classes
    Cache → AnyRef → Any
  26. final def wait(): Unit

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

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

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped