Skip to content

Instantly share code, notes, and snippets.

@gideondk
Created July 11, 2012 13:16
Show Gist options
  • Save gideondk/3090339 to your computer and use it in GitHub Desktop.
Save gideondk/3090339 to your computer and use it in GitHub Desktop.
Redis write through cache for Scaliak (using Sedis)
abstract class WriteThroughRedisDomainHelper[T <: DomainObject](override val clientPool: ScaliakPbClientPool, val redisPool: Pool)(implicit mo: Manifest[T]) extends DomainObjectHelper[T](clientPool) {
override def fetch(k: String) = {
redisPool.withClient { _.get(k) }.map { parse(_) }.successNel.pure[IO].except { _.failNel.pure[IO] }.flatMap { x ⇒
~x.toOption match {
case Some(o) if (o != null) ⇒ x.pure[IO]
case None ⇒ super.fetch(k)
}
}
}
override def store(o: T) = {
for {
riak <- super.store(o)
_ <- redisPool.withClient(_.set(o.key, generate(o))).successNel.pure[IO].except { _.failNel.pure[IO] }
} yield riak
}
override def delete(o: T) = {
for {
riak <- super.delete(o)
_ <- redisPool.withJedisClient(_.set(o.key, null)).successNel.pure[IO].except { _.failNel.pure[IO] }
} yield riak
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment