Caching Implicits
When deriving the encoders and decoders to your case classes, the library is built to be able to automatically resolve
the implicit CCCassFormatEncoder
/CCCassFormatDecoder
. This will work fine, but if you are using these
encoders/decoders often, it may be worth it to cache them so that they do not have to be built at every call site. The
best and easiest way to do this is to derive these implicits in the companion object to your case classes, as follows:
(quick note: the ImplicitCaching
object is a workaround for the compilation of these docs. It is unnecessary to wrap
your case class/companion object definition in code)
scala> object ImplicitCaching {
| case class Query(s: String)
| object Query {
| implicit val encoder: CCCassFormatEncoder[Query] = CCCassFormatEncoder.derive
| implicit val decoder: CCCassFormatDecoder[Query] = CCCassFormatDecoder.derive
| }
| }
defined object ImplicitCaching
scala> sSession.selectStar("mytable", ImplicitCaching.Query("a str")).execute()
res3: com.weather.scalacass.Result[Iterator[com.datastax.driver.core.Row]] = Right(non-empty iterator)
the derive
function will implicitly create the encoder/decoder in the companion object, and now at every call site,
this implicit is used instead of one that would be built by the library.