Skip to content

Commit

Permalink
Add support for KEEPTTL option in SET command (#714)
Browse files Browse the repository at this point in the history
Authored-by: Mamdouh Abdelaziz <mamdouh.ahmed.abouelfetouh.abdelaziz@zalando.de>
  • Loading branch information
MamdouhAhmed committed Jul 23, 2022
1 parent d222616 commit 65978dc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ object effects {

/** Set Expiration in Seconds */
case class Ex(duration: FiniteDuration) extends Ttl

/** Set KeepTtl */
case object Keep extends Ttl
}
}
case class SetArgs(existence: Option[SetArg.Existence], ttl: Option[SetArg.Ttl])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ private[redis4cats] class BaseRedis[F[_]: FutureLift: MonadThrow: Log, K, V](
setArgs.ttl.foreach {
case SetArg.Ttl.Px(d) => jSetArgs.px(d.toMillis)
case SetArg.Ttl.Ex(d) => jSetArgs.ex(d.toSeconds)
case SetArg.Ttl.Keep => jSetArgs.keepttl()
}

async.flatMap(_.set(key, value, jSetArgs).futureLift.map(_ == "OK"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,16 @@ trait TestScenarios { self: FunSuite =>
j <- redis.expire("_not_existing_key_", 50.millis)
_ <- IO(assertEquals(j, false))
_ <- redis.del("f1")
k <- redis.set("k", "", SetArgs(SetArg.Ttl.Ex(10.seconds)))
_ <- IO(assertEquals(k, true))
kTtl <- redis.ttl("k")
_ <- IO(assert(kTtl.nonEmpty))
_ <- redis.set("k", "v", SetArgs(SetArg.Ttl.Keep))
kv <- redis.get("k")
_ <- IO(assertEquals(kv, Some("v")))
kTtl2 <- redis.ttl("k")
_ <- IO(assert(kTtl2.nonEmpty))
_ <- redis.del("k")
} yield ()
}

Expand Down

0 comments on commit 65978dc

Please sign in to comment.