Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exists command support #152

Merged
merged 2 commits into from
Oct 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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[Long]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better if we return F[Boolean] since Redis would return either 1 if the key exists or 0 if it doesn't, and that can be easily matched to true and false.

On the other hand, I don't really like what Redis does with accepting multiple keys as input. It'll just return the sum of the partial results. For example:

redis> SET key1 "Hello"
"OK"
redis> EXISTS key1
(integer) 1
redis> EXISTS nosuchkey
(integer) 0
redis> SET key2 "World"
"OK"
redis> EXISTS key1 key2 nosuchkey
(integer) 2

I think we could do better and return true if ALL the keys exist and false if at least one key doesn't exist. Similar to what the Monoid[All] instance would do.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense

def expire(k: K, seconds: FiniteDuration): F[Unit]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[Long] =
JRFuture {
async.flatMap(c => F.delay(c.exists(key: _*)))
}.map(x => Long.box(x))

def expire(key: K, expiresIn: FiniteDuration): F[Unit] =
JRFuture {
async.flatMap(c => F.delay(c.expire(key, expiresIn.toSeconds)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,12 @@ trait Fs2TestScenarios {
for {
x <- cmd.get(key)
_ <- IO { assert(x.isEmpty) }
exist1 <- cmd.exists(key)
_ <- IO { assert(exist1 == 0L) }
isSet1 <- cmd.setNx(key, "some value")
_ <- IO { assert(isSet1) }
exist2 <- cmd.exists(key)
_ <- IO { assert(exist2 == 1L) }
y <- cmd.get(key)
_ <- IO { assert(y.contains("some value")) }
isSet2 <- cmd.setNx(key, "should not happen")
Expand All @@ -150,11 +154,15 @@ trait Fs2TestScenarios {
_ <- IO { assert(val3.isEmpty) }
isSet5 <- cmd.mSetNx(Map("multikey1" -> "someVal1", "multikey2" -> "someVal2"))
_ <- IO { assert(!isSet5) }
exist3 <- cmd.exists(key, "multikey1", "_not_existing_key_")
_ <- IO { assert(exist3 == 2L) }
w <- cmd.get(key)
_ <- IO { assert(w.contains("some value")) }
_ <- cmd.del(key)
z <- cmd.get(key)
_ <- IO { assert(z.isEmpty) }
exist4 <- cmd.exists(key)
_ <- IO { assert(exist4 == 0L) }
} yield ()
}

Expand Down