Synchronous API
Unfortunately Scala’s type inference doesn’t play very nicely with ScalaCache’s synchronous mode.
If you want to use synchronous mode, the synchronous API is recommended. This works in exactly the same way as the normal API; it is provided merely as a convenience so you don’t have to jump through hoops to make your code compile when using the synchronous mode.
scala> import scalacache._
import scalacache._
scala> import scalacache.memcached._
import scalacache.memcached._
scala> import scalacache.modes.sync._
import scalacache.modes.sync._
scala> import scalacache.serialization.binary._
import scalacache.serialization.binary._
scala> final case class Cat(id: Int, name: String, colour: String)
defined class Cat
scala> implicit val catsCache: Cache[Cat] = MemcachedCache("localhost:11211")
catsCache: scalacache.Cache[Cat] = scalacache.memcached.MemcachedCache@6d637df9
scala> val myValue: Option[Cat] = sync.get("eric")
myValue: Option[Cat] = None
scala> val ericTheCat = Cat(1, "Eric", "tuxedo")
ericTheCat: Cat = Cat(1,Eric,tuxedo)
There is also a synchronous version of caching
:
scala> val result = sync.caching("myKey")(ttl = None) {
| // do stuff...
| ericTheCat
| }
result: Cat = Cat(1,Eric,tuxedo)