Skip to content

Instantly share code, notes, and snippets.

@ae0n
Created July 7, 2021 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ae0n/8a90b0e11c8621978d09ea95ad608585 to your computer and use it in GitHub Desktop.
Save ae0n/8a90b0e11c8621978d09ea95ad608585 to your computer and use it in GitHub Desktop.
import cats.effect.{ExitCode, IO, IOApp}
import dev.profunktor.redis4cats._
import dev.profunktor.redis4cats.hlist._
import dev.profunktor.redis4cats.transactions._
import cats.implicits._
import dev.profunktor.redis4cats.effect.Log
import java.util.concurrent.TimeoutException
object Main extends IOApp {
def putStrLn(str: String): IO[Unit] = IO(println(str))
override def run(args: List[String]): IO[ExitCode] = {
implicit val l = Log.Stdout.instance[IO]
val key1 = "test1"
val commandsApi = Redis[IO].utf8("redis://localhost:6379")
commandsApi
.use { redis => // RedisCommands[IO, String, String]
val tx = RedisTransaction(redis)
val commands =
redis.set(key1, "foo").flatTap(_ => putStrLn("1 SET")) ::
redis
.get(key1)
.flatTap(s => putStrLn(s"2 GET ${s}")) ::
redis
.del(key1)
.flatTap(i => putStrLn(s"3 DEL ${i}")) ::
redis
.get(key1)
.flatTap(s => putStrLn(s"4 GET ${s}")) ::
HNil
val prog =
tx.exec(commands)
.flatMap {
case res1 ~: res2 ~: res3 ~: res4 ~: HNil =>
putStrLn(s"res1: $res1") >>
putStrLn(s"res2: $res2") >>
putStrLn(s"res3: $res3") >>
putStrLn(s"res4 SHOULD BE NONE: $res4")
}
.onError {
case TransactionAborted =>
Log[IO].error("[Error] - Transaction Aborted")
case TransactionDiscarded =>
Log[IO].error("[Error] - Transaction Discarded")
case _: TimeoutException =>
Log[IO].error("[Error] - Timeout")
case e =>
Log[IO].error(s"[Error] - $e")
}
prog
}
.as(ExitCode.Success)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment