Skip to content

Instantly share code, notes, and snippets.

@patriknw
Created October 3, 2012 12:16
Show Gist options
  • Save patriknw/3826631 to your computer and use it in GitHub Desktop.
Save patriknw/3826631 to your computer and use it in GitHub Desktop.
Spotlight for ConsistentHashingRouter
import akka.routing.ConsistentHashingRouter
import akka.routing.ConsistentHashingRouter.ConsistentHashMapping
import akka.actor.Actor
import akka.pattern.ask
case class Entry(key: String, value: String)
class Cache extends Actor {
var cache = Map.empty[String, String]
def receive = {
case Entry(key, value) => cache += (key -> value)
case key: String => sender ! cache.get(key)
}
}
def hashMapping: ConsistentHashMapping = {
case Entry(key, _) => key
case s: String => s
}
val cache = system.actorOf(Props[Cache].withRouter(
ConsistentHashingRouter(10, hashMapping = hashMapping)),
name = "cache")
cache ! Entry("hello", "HELLO")
cache ? "hello" onSuccess { case x => println(x) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment