Skip to content

Commit

Permalink
Merge pull request #50 from kubukoz/ping
Browse files Browse the repository at this point in the history
Add ConnectionCommands with `ping`
  • Loading branch information
gvolpe authored Dec 25, 2018
2 parents 1ae62d2 + 70953a8 commit 6dadcea
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Redis stream-based client built on top of [Cats Effect](https://typelevel.org/ca
- [Sets API](https://redis.io/commands#set): `sadd`, `scard`, `srem`, `spop`, etc.
- [Sorted Sets API](https://redis.io/commands#sorted_set): `zcount`, `zcard`, `zrangebyscore`, `zrank`, etc.
- [Strings API](https://redis.io/commands#string): `get`, `set`, `del`, `expire`, etc (includes some generic methods).
- [Connection API](https://redis.io/commands#connection): `ping`

Other features are not considered at the moment but PRs and suggestions are very welcome.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2018-2019 Fs2 Redis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.gvolpe.fs2redis.algebra

trait ConnectionCommands[F[_]] extends Ping[F]

trait Ping[F[_]] {
def ping: F[String]
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ object Fs2Redis {
with SortedSetCommands[F, K, V]
with ListCommands[F, K, V]
with GeoCommands[F, K, V]
with ConnectionCommands[F]

private[fs2redis] def acquireAndRelease[F[_]: Concurrent: Log, K, V](
client: Fs2RedisClient,
Expand Down Expand Up @@ -779,6 +780,11 @@ private[fs2redis] class BaseFs2Redis[F[_], K, V](
conn.async.flatMap(c => F.delay(c.zscore(key, value)))
}.map(x => Option(Double.box(x)))

/******************************* Connection API **********************************/
override val ping: F[String] =
JRFuture {
conn.async.flatMap(c => F.delay(c.ping()))
}
}

private[fs2redis] trait Fs2RedisConversionOps {
Expand Down
4 changes: 4 additions & 0 deletions site/src/main/resources/microsite/data/menu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ options:
url: effects/strings.html
menu_section: effectapi

- title: Connection
url: effects/connection.html
menu_section: effectapi

43 changes: 43 additions & 0 deletions site/src/main/tut/effects/connection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
layout: docs
title: "Connection"
number: 11
---

# Connection API

Purely functional interface for the [Connection API](https://redis.io/commands#connection).

```tut:book:invisible
import cats.effect.{IO, Resource}
import cats.syntax.all._
import com.github.gvolpe.fs2redis.algebra.ConnectionCommands
import com.github.gvolpe.fs2redis.interpreter.Fs2Redis
import scala.concurrent.ExecutionContext
implicit val cs = IO.contextShift(ExecutionContext.global)
val commandsApi: Resource[IO, ConnectionCommands[IO]] = {
Fs2Redis[IO, String, String](null, null, null).widen[ConnectionCommands[IO]]
}
```

### Connection Commands usage

Once you have acquired a connection you can start using it:

```tut:book:silent
import cats.effect.IO
import cats.syntax.all._
def putStrLn(str: String): IO[Unit] = IO(println(str))
commandsApi.use { cmd => // ConnectionCommands[IO]
for {
pong <- cmd.ping
_ <- putStrLn(pong) //"pong"
} yield ()
}
```

Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ class Fs2RedisClusterSpec extends FunSuite with RedisClusterTest with Fs2TestSce

test("cluster: strings api")(withRedisCluster(stringsScenario))

test("cluster: connection api")(withRedisCluster(connectionScenario))

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Fs2RedisSpec extends FunSuite with DockerRedis with Fs2TestScenarios {

test("strings api")(withRedis(stringsScenario))

test("connection api")(withRedis(connectionScenario))
}

object LongCodec extends RedisCodec[String, Long] with ToByteBufEncoder[String, Long] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package com.github.gvolpe.fs2redis

import cats.effect.IO
import com.github.gvolpe.fs2redis.effects._
import io.lettuce.core.GeoArgs
import cats.implicits._
import com.github.gvolpe.fs2redis.interpreter.Fs2Redis
import io.lettuce.core.GeoArgs

Expand Down Expand Up @@ -135,4 +135,10 @@ trait Fs2TestScenarios {
} yield ()
}

def connectionScenario(cmd: Fs2Redis.RedisCommands[IO, String, String]): IO[Unit] =
for {
pong <- cmd.ping
_ <- IO { assert(pong === "PONG") }
} yield ()

}

0 comments on commit 6dadcea

Please sign in to comment.