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 client commands #767

Merged
merged 3 commits into from
Jan 7, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package dev.profunktor.redis4cats.algebra

trait ConnectionCommands[F[_]] extends Ping[F] with Auth[F]
trait ConnectionCommands[F[_], K] extends Ping[F] with Auth[F] with Client[F, K]

trait Ping[F[_]] {
def ping: F[String]
Expand All @@ -27,3 +27,9 @@ trait Auth[F[_]] {
def auth(password: CharSequence): F[Boolean]
def auth(username: String, password: CharSequence): F[Boolean]
}

trait Client[F[_], K] {
def setClientName(name: K): F[Boolean]
def getClientName(): F[Option[K]]
def getClientId(): F[Long]
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ trait RedisCommands[F[_], K, V]
with SortedSetCommands[F, K, V]
with ListCommands[F, K, V]
with GeoCommands[F, K, V]
with ConnectionCommands[F]
with ConnectionCommands[F, K]
with ServerCommands[F, K]
with TransactionalCommands[F, K]
with PipelineCommands[F]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,15 @@ private[redis4cats] class BaseRedis[F[_]: FutureLift: MonadThrow: Log, K, V](
override def auth(username: String, password: CharSequence): F[Boolean] =
async.flatMap(_.auth(username, password).futureLift.map(_ == "OK"))

override def setClientName(name: K): F[Boolean] =
async.flatMap(_.clientSetname(name).futureLift.map(_ == "OK"))

override def getClientName(): F[Option[K]] =
async.flatMap(_.clientGetname().futureLift).map(Option.apply)

override def getClientId(): F[Long] =
async.flatMap(_.clientId().futureLift.map(Long.unbox))

/******************************* Server API **********************************/
override val flushAll: F[Unit] =
async.flatMap(_.flushall().futureLift.void)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,18 @@ trait TestScenarios { self: FunSuite =>
} yield ()
}

def connectionScenario(redis: RedisCommands[IO, String, String]): IO[Unit] =
redis.ping.flatMap(pong => IO(assertEquals(pong, "PONG"))).void
def connectionScenario(redis: RedisCommands[IO, String, String]): IO[Unit] = {
val clientName = "hello_world"
for {
_ <- redis.ping.flatMap(pong => IO(assertEquals(pong, "PONG"))).void
_ <- redis.getClientName().flatMap(name => IO(assertEquals(name, None)))
_ <- redis
gvolpe marked this conversation as resolved.
Show resolved Hide resolved
.setClientName(clientName)
.flatMap(result => IO(assert(result, s"Failed to set client name: '$clientName'")))
_ <- redis.getClientName().flatMap(name => IO(assertEquals(name, Some(clientName))))
_ <- redis.getClientId()
} yield ()
}

def serverScenario(redis: RedisCommands[IO, String, String]): IO[Unit] =
for {
Expand Down
7 changes: 6 additions & 1 deletion site/docs/effects/connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ import cats.effect.IO
def putStrLn(str: String): IO[Unit] = IO(println(str))

commandsApi.use { redis => // ConnectionCommands[IO]
redis.ping.flatMap(putStrLn) // "pong"
val clientName = "client_x"
for {
_ <- redis.ping.flatMap(putStrLn) // "pong"
_ <- redis.setClientName(clientName) // true
_ <- redis.getClientName().flatMap(putStrLn) // "client_x"
} yield ()
}
```