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 all commits
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,20 @@ 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 {
pong <- redis.ping
_ <- IO(assertEquals(pong, "PONG"))
oldClientName <- redis.getClientName()
_ <- IO(assertEquals(oldClientName, None))
res <- redis.setClientName(clientName)
_ <- IO(assert(res, s"Failed to set client name: '$clientName'"))
newClientName <- redis.getClientName()
_ <- IO(assertEquals(newClientName, Some(clientName)))
_ <- redis.getClientId()
} yield ()
}

def serverScenario(redis: RedisCommands[IO, String, String]): IO[Unit] =
for {
Expand Down
14 changes: 10 additions & 4 deletions site/docs/effects/connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import org.typelevel.log4cats.slf4j.Slf4jLogger

implicit val logger: Logger[IO] = Slf4jLogger.getLogger[IO]

val commandsApi: Resource[IO, ConnectionCommands[IO]] = {
Redis[IO].fromClient[String, String](null, null.asInstanceOf[RedisCodec[String, String]]).widen[ConnectionCommands[IO]]
val commandsApi: Resource[IO, ConnectionCommands[IO, String]] = {
Redis[IO].fromClient[String, String](null, null.asInstanceOf[RedisCodec[String, String]]).widen[ConnectionCommands[IO, String]]
}
```

Expand All @@ -34,8 +34,14 @@ import cats.effect.IO

def putStrLn(str: String): IO[Unit] = IO(println(str))

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