diff --git a/modules/effects/src/main/scala/dev/profunktor/redis4cats/algebra/strings.scala b/modules/effects/src/main/scala/dev/profunktor/redis4cats/algebra/strings.scala index a12bf17a..62639ea4 100644 --- a/modules/effects/src/main/scala/dev/profunktor/redis4cats/algebra/strings.scala +++ b/modules/effects/src/main/scala/dev/profunktor/redis4cats/algebra/strings.scala @@ -26,6 +26,7 @@ trait StringCommands[F[_], K, V] with Increment[F, K, V] with Bits[F, K, V] { def del(key: K*): F[Unit] + def exists(key: K*): F[Boolean] def expire(k: K, seconds: FiniteDuration): F[Unit] } diff --git a/modules/effects/src/main/scala/dev/profunktor/redis4cats/interpreter/Redis.scala b/modules/effects/src/main/scala/dev/profunktor/redis4cats/interpreter/Redis.scala index 4400a09f..f0a9861b 100644 --- a/modules/effects/src/main/scala/dev/profunktor/redis4cats/interpreter/Redis.scala +++ b/modules/effects/src/main/scala/dev/profunktor/redis4cats/interpreter/Redis.scala @@ -137,6 +137,11 @@ private[redis4cats] class BaseRedis[F[_]: ContextShift, K, V]( async.flatMap(c => F.delay(c.del(key: _*))) }.void + def exists(key: K*): F[Boolean] = + JRFuture { + async.flatMap(c => F.delay(c.exists(key: _*))) + }.map(x => x == key.size.toLong) + def expire(key: K, expiresIn: FiniteDuration): F[Unit] = JRFuture { async.flatMap(c => F.delay(c.expire(key, expiresIn.toSeconds))) diff --git a/modules/tests/src/test/scala/dev/profunktor/redis4cats/Fs2TestScenarios.scala b/modules/tests/src/test/scala/dev/profunktor/redis4cats/Fs2TestScenarios.scala index 3221b017..6e314ae7 100644 --- a/modules/tests/src/test/scala/dev/profunktor/redis4cats/Fs2TestScenarios.scala +++ b/modules/tests/src/test/scala/dev/profunktor/redis4cats/Fs2TestScenarios.scala @@ -134,8 +134,12 @@ trait Fs2TestScenarios { for { x <- cmd.get(key) _ <- IO { assert(x.isEmpty) } + exist1 <- cmd.exists(key) + _ <- IO { assert(!exist1) } isSet1 <- cmd.setNx(key, "some value") _ <- IO { assert(isSet1) } + exist2 <- cmd.exists(key) + _ <- IO { assert(exist2) } y <- cmd.get(key) _ <- IO { assert(y.contains("some value")) } isSet2 <- cmd.setNx(key, "should not happen") @@ -150,11 +154,17 @@ trait Fs2TestScenarios { _ <- IO { assert(val3.isEmpty) } isSet5 <- cmd.mSetNx(Map("multikey1" -> "someVal1", "multikey2" -> "someVal2")) _ <- IO { assert(!isSet5) } + exist3 <- cmd.exists(key, "multikey1", "multikey2") + _ <- IO { assert(exist3) } + exist4 <- cmd.exists(key, "multikey1", "_not_existing_key_") + _ <- IO { assert(!exist4) } w <- cmd.get(key) _ <- IO { assert(w.contains("some value")) } _ <- cmd.del(key) z <- cmd.get(key) _ <- IO { assert(z.isEmpty) } + exist5 <- cmd.exists(key) + _ <- IO { assert(!exist5) } } yield () }