index.md 2.5 KB
Newer Older
1 2 3
---
id: index
title: "Introduction to ZIO Redis"
4
sidebar_label: "ZIO Redis"
5 6
---

7 8 9 10 11 12 13 14 15 16 17 18 19 20
[ZIO Redis](https://github.com/zio/zio-redis) is a ZIO native Redis client.

> The client is still a work-in-progress. Watch this space!

@PROJECT_BADGES@

## Introduction

ZIO Redis is in the experimental phase of development, but its goals are:

- **Type Safety**
- **Performance**
- **Minimum Dependency**
- **ZIO Native**
21 22 23

## Installation

24
Since the ZIO Redis is in the experimental phase, it is not released yet, but we can use snapshots:
25 26 27 28

```scala
libraryDependencies += "dev.zio" %% "zio-redis" % "@VERSION@"
```
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

## 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(
  "dev.zio" %% "zio-redis" % "@VERSION@",
  "dev.zio" %% "zio-schema-protobuf" % "0.3.0"
)
```

```scala mdoc:compile-only
import zio._
import zio.redis._
import zio.schema.codec._

object ZIORedisExample extends ZIOAppDefault {
  val myApp: ZIO[Redis, RedisError, Unit] = for {
54 55 56 57 58 59 60
    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")
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  } yield ()

  override def run = myApp.provide(
    RedisLive.layer,
    RedisExecutor.layer,
    ZLayer.succeed(RedisConfig.Default),
    ZLayer.succeed[BinaryCodec](ProtobufCodec)
  )
}
```

## 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.