Created
July 11, 2012 13:16
-
-
Save gideondk/3090339 to your computer and use it in GitHub Desktop.
Redis write through cache for Scaliak (using Sedis)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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