README.md 5.3 KB
Newer Older
G
github-actions[bot] 已提交
1 2 3
[//]: # (This file was autogenerated using `zio-sbt-website` plugin via `sbt generateReadme` command.)
[//]: # (So please do not edit it manually. Instead, change "docs/index.md" file or sbt setting keys)
[//]: # (e.g. "readmeDocumentation" and "readmeSupport".)
D
Dejan Mijić 已提交
4

G
github-actions[bot] 已提交
5
# ZIO Redis
D
Dejan Mijić 已提交
6

G
github-actions[bot] 已提交
7
[![Development](https://img.shields.io/badge/Project%20Stage-Development-green.svg)](https://github.com/zio/zio/wiki/Project-Stages) ![CI Badge](https://github.com/zio/zio-redis/workflows/CI/badge.svg) [![Sonatype Releases](https://img.shields.io/nexus/r/https/oss.sonatype.org/dev.zio/zio-redis_2.13.svg?label=Sonatype%20Release)](https://oss.sonatype.org/content/repositories/releases/dev/zio/zio-redis_2.13/) [![Sonatype Snapshots](https://img.shields.io/nexus/s/https/oss.sonatype.org/dev.zio/zio-redis_2.13.svg?label=Sonatype%20Snapshot)](https://oss.sonatype.org/content/repositories/snapshots/dev/zio/zio-redis_2.13/) [![javadoc](https://javadoc.io/badge2/dev.zio/zio-redis-docs_2.13/javadoc.svg)](https://javadoc.io/doc/dev.zio/zio-redis-docs_2.13) [![ZIO Redis](https://img.shields.io/github/stars/zio/zio-redis?style=social)](https://github.com/zio/zio-redis)
G
github-actions[bot] 已提交
8 9 10

## Introduction

D
Dejan Mijić 已提交
11 12 13
[ZIO Redis](https://github.com/zio/zio-redis) is a ZIO-native Redis client.
It aims to provide a **type-safe** and **performant** API for accessing Redis
instances.
G
github-actions[bot] 已提交
14 15 16

## Installation

D
Dejan Mijić 已提交
17
To use ZIO Redis, add the following line to your `build.sbt`:
G
github-actions[bot] 已提交
18 19

```scala
G
github-actions[bot] 已提交
20
libraryDependencies += "dev.zio" %% "zio-redis" % "0.2.0"
G
github-actions[bot] 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34
```

## Example

To execute our ZIO Redis effect, we should provide the `RedisExecutor` layer to that effect. To create this layer we
should also provide the following layers:

- **RedisConfig** — Using default one, will connect to the `localhost:6379` Redis instance.
- **BinaryCodec** — In this example, we are going to use the built-in `ProtobufCodec` codec from zio-schema project.

To run this example we should put following dependencies in our `build.sbt` file:

```scala
libraryDependencies ++= Seq(
G
github-actions[bot] 已提交
35
  "dev.zio" %% "zio-redis" % "0.2.0",
36
  "dev.zio" %% "zio-schema-protobuf" % "0.4.9"
G
github-actions[bot] 已提交
37 38
)
```
39

G
github-actions[bot] 已提交
40 41 42
```scala
import zio._
import zio.redis._
43
import zio.schema._
G
github-actions[bot] 已提交
44 45 46
import zio.schema.codec._

object ZIORedisExample extends ZIOAppDefault {
47 48 49 50 51
  
  object ProtobufCodecSupplier extends CodecSupplier {
    def get[A: Schema]: BinaryCodec[A] = ProtobufCodec.protobufCodec
  }
  
G
github-actions[bot] 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64
  val myApp: ZIO[Redis, RedisError, Unit] = 
    for {
      redis <- ZIO.service[Redis]
      _     <- redis.set("myKey", 8L, Some(1.minutes))
      v     <- redis.get("myKey").returning[Long]
      _     <- Console.printLine(s"Value of myKey: $v").orDie
      _     <- redis.hSet("myHash", ("k1", 6), ("k2", 2))
      _     <- redis.rPush("myList", 1, 2, 3, 4)
      _     <- redis.sAdd("mySet", "a", "b", "a", "c")
    } yield ()

  override def run = 
    myApp.provide(Redis.local, ZLayer.succeed[CodecSupplier](ProtobufCodecSupplier))
G
github-actions[bot] 已提交
65 66 67
}
```

I
ioleo 已提交
68 69 70 71
## Testing

To test you can use the embedded redis instance by adding to your build:

D
Dejan Mijić 已提交
72
```scala
G
github-actions[bot] 已提交
73
libraryDependencies := "dev.zio" %% "zio-redis-embedded" % "0.2.0"
D
Dejan Mijić 已提交
74
```
I
ioleo 已提交
75 76 77 78 79 80

Then you can supply `EmbeddedRedis.layer.orDie` as your `RedisConfig` and you're good to go!

```scala
import zio._
import zio.redis._
81
import zio.redis.embedded.EmbeddedRedis
I
ioleo 已提交
82 83 84 85 86
import zio.schema.{DeriveSchema, Schema}
import zio.schema.codec.{BinaryCodec, ProtobufCodec}
import zio.test._
import zio.test.Assertion._
import java.util.UUID
87

I
ioleo 已提交
88
object EmbeddedRedisSpec extends ZIOSpecDefault {
89 90 91 92
  object ProtobufCodecSupplier extends CodecSupplier {
    def get[A: Schema]: BinaryCodec[A] = ProtobufCodec.protobufCodec
  }
  
I
ioleo 已提交
93 94 95 96
  final case class Item private (id: UUID, name: String, quantity: Int)
  object Item {
    implicit val itemSchema: Schema[Item] = DeriveSchema.gen[Item]
  }
97
  
I
ioleo 已提交
98 99 100 101 102 103 104 105 106 107
  def spec = suite("EmbeddedRedis should")(
    test("set and get values") {
      for {
        redis <- ZIO.service[Redis]
        item   = Item(UUID.randomUUID, "foo", 2)
        _     <- redis.set(s"item.${item.id.toString}", item)
        found <- redis.get(s"item.${item.id.toString}").returning[Item]
      } yield assert(found)(isSome(equalTo(item)))
    }
  ).provideShared(
108
    EmbeddedRedis.layer,
109
    ZLayer.succeed[CodecSupplier](ProtobufCodecSupplier),
G
github-actions[bot] 已提交
110
    Redis.singleNode
I
ioleo 已提交
111 112 113 114
  ) @@ TestAspect.silentLogging
}
```

G
github-actions[bot] 已提交
115 116 117 118 119 120 121 122 123 124
## Resources

- [ZIO Redis](https://www.youtube.com/watch?v=yqFt3b3RBkI) by Dejan Mijic — Redis is one of the most commonly used
  in-memory data structure stores. In this talk, Dejan will introduce ZIO Redis, a purely functional, strongly typed
  client library backed by ZIO, with excellent performance and extensive support for nearly all of Redis' features. He
  will explain the library design using the bottom-up approach - from communication protocol to public APIs. Finally, he
  will wrap the talk by demonstrating the client's usage and discussing its performance characteristics.

## Documentation

Z
zio-assistant[bot] 已提交
125
Learn more on the [ZIO Redis homepage](https://zio.dev/zio-redis)!
G
github-actions[bot] 已提交
126 127 128

## Contributing

Z
zio-assistant[bot] 已提交
129
For the general guidelines, see ZIO [contributor's guide](https://zio.dev/contributor-guidelines).
G
github-actions[bot] 已提交
130 131 132

## Code of Conduct

Z
zio-assistant[bot] 已提交
133
See the [Code of Conduct](https://zio.dev/code-of-conduct)
134 135 136 137 138

## Support

Come chat with us on [![Badge-Discord]][Link-Discord].

G
github-actions[bot] 已提交
139 140
[Badge-Discord]: https://img.shields.io/discord/629491597070827530?logo=discord "chat on discord"
[Link-Discord]: https://discord.gg/2ccFBr4 "Discord"
141

G
github-actions[bot] 已提交
142
## License
143

G
github-actions[bot] 已提交
144
[License](LICENSE)