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

com.twitter.finagle.memcached.util.AtomicMap.scala Maven / Gradle / Ivy

There is a newer version: 21.2.0
Show newest version
package com.twitter.finagle.memcached.util

import scala.collection.mutable

/**
 * Improve concurrency with fine-grained locking. A hash of synchronized hash
 * tables, keyed on the request key.
 */
class AtomicMap[A, B](maps: Seq[mutable.Map[A, B]]) {
  def this(concurrencyLevel: Int) = this {
    (0 until concurrencyLevel) map { i => mutable.Map[A, B]() }
  }
  def this() = this(16)

  private[this] val concurrencyLevel = maps.size

  def lock[C](key: A)(f: mutable.Map[A, B] => C) = {
    val map = maps((key.hashCode % concurrencyLevel).abs)
    f.synchronized {
      f(map)
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy