This is an akka extension for accessing Couchbase 2.
- Add to application.conf
akka{
extensions = ["akka.contrib.couchbase.CBExtension"]
contrib.couchbase.buckets = [
{ bucket=bk1
# You can map bk1 to a different `real` bucket name in couchbase.
# If cb-bucket setting is missing then CBExtension will use `bucket` (here is bk1).
# One use-case of this setting is to support switching
# deploy environment without changing any line of code
cb-bucket="bk1_test"
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 net.spy.memcached.ops.StatusCode.ERR_NOT_FOUND
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(ERR_NOT_FOUND) =>
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" % "3.1.3"
We use Semantic Versioning, so changing in micro version is binary compatible.
Ex, v2.0.x is binary compatible with v2.0.0
- update scala 2.11.2, specs2 2.4, play-json 2.3.2, couchbase-client 1.4.4
- update play-json 2.3.1 (optional dependency), akka-actor 2.3.4
- note: couchbase-client is NOT updated to 1.4.3 because when run
sbt test
with v1.4.3 => many warning lines:WARN net.spy.memcached.MemcachedConnection: handling node for operation is not set
- update play-json 2.3.0 (optional dependency)
- update couchbase-client 1.4.2 (& spymemcached 2.11.3)
- update play-json 2.3.0-RC2 (optional dependency)
- update sbt 0.13.5-RC4, scala 2.11.1, akka 2.3.3 & specs2 2.3.12
- add convenient traits akka.contrib.couchbase.{HasKey0, ReadsKey0, WritesKey0, Key0}
- update couchbase-client 1.4.1. version 1.4.0 of couchbase-client depends on spymemcached 2.11.1 which has some critical bugs.
- Change nothing in code but update play-json (optional dependency) to 2.3.0-RC1 & akka-actor 2.3.2
- cross compile for scala 2.11 & scala 2.10
- update couchbase-client 1.4.0 & spymemcached 2.11.1
- update sbt 0.13.2
- Only change structure of CBException to use StatusCode type-safe feature in spymemcached 2.10.6. @see SPY-153 & the corresponding commit
In v2.x, you check whether the get operation return NotFound by matching
case CBException(NotFound)
where NotFound is a string, hardcode = "Not found".
Now, in v3.x, this checking must change to
case CBException(ERR_NOT_FOUND)
where ERR_NOT_FOUND is a value in enum net.spy.memcached.ops.StatusCode.
Of course, you can check with other StatusCode.
- support
cb-bucket
setting. This version is backward compatible with 2.1.x
- update play 2.2.2 & akka 2.2.4
- Add scala doc for CBJson.scala
- Remove handler when future completed in CbFutureAsScala
- 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)