This is an akka extension for accessing Couchbase 2.
- Add to application.conf
akka{
extensions = ["akka.contrib.couchbase.CBExtension"]
contrib.couchbase.buckets = [
{ bucket=bk1
password="some password"
nodes="http://cb1.sandinh.com:8091/pools;http://cb2.sandinh.com:8091/pools"
},
{ bucket=bk2
password="some password"
nodes="http://cb3.sandinh.com:8091/pools"
}
]
}
- Get CouchbaseClient from an ActorSystem. This example use Play framework 2 - in which, we can get ActorSystem by importing. Of course, you can get CouchbaseClient without Play.
import play.api.libs.concurrent.Akka
import play.api.Play.current
val cb = CBExtension(Akka.system).buckets("bk1")
- Use CouchbaseClient. If use asynchronous methods then we can use CbFutureAsScala to implicit convert spymemcache ListenableFuture to scala Future This code use play-json - a json parser library that do NOT depends on play framework
import akka.contrib.couchbase.CbFutureAsScala._
import play.api.libs.json.Json
import scala.concurrent.ExecutionContext.Implicits.global
case class User(name: String, age: Int)
implicit val fmt = Json.format[User]
def getOrCreate(key: String): Future[User] =
cb.asyncGet(key).asScala.map{
case s: String => Json.fromJson[User](Json.parse(s)).get
}.recoverWith{
case CBException(NotFound) =>
val u = User("Bob", 18)
cb.set(key, Json.stringify(Json.toJson(u))).asScala.map(_ => u)
}
This library is published to maven central.
If you use sbt then:
libraryDependencies += "com.sandinh" %% "couchbase-akka-extension" % "2.1.0"
We use Semantic Versioning, so changing in micro version is binary compatible.
Ex, v2.0.x is binary compatible with v2.0.0
- Add scala doc for CBJson.scala
- Change
ReadsKey1[T, A] { this: CBReads[T] =>
toReadsKey1[T, A] extends CBReads[T] {
. This change can break binary compatibility, but keep source compatibility. @See case "not throws StackOverflowError" in CBJsonSpec for more information.
- only update akka-actor 2.2.3
- update akka-actor 2.2.2
- add CBJson util traits for reads/ writes couchbase. See sample usage in CBJsonSpec
- add optional dependency
"com.typesafe.play" %% "play-json" % "2.2.0" % "optional"
. This is mandatory if you use CBJson.
- update couchbase-client 1.2.1 (& spymemcached 2.10.1)
- add val akka.contrib.couchbase.CbFutureAsScala.NotFound
- Add unit test
- (NOT compatible) change from:
implicit def xxCbFutureAsScala[T](underlying: XxCbFuture[T]): Future[T]
to:
implicit class RichXxCbFuture(underlying: XxCbFuture[T]){
def asScala: Future[T]
}
So, in v2.0.0, you must call .asScala to convert CbFuture to scala Future (similar to collection.JavaConverters._ )
First stable release
This software is licensed under the Apache 2 license: http://www.apache.org/licenses/LICENSE-2.0
Copyright 2013 Sân Đình (http://sandinh.com)