Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A ZIO-based redis client

License

NotificationsYou must be signed in to change notification settings

zio/zio-redis

Repository files navigation

DevelopmentCI BadgeSonatype ReleasesSonatype SnapshotsjavadocZIO Redis

Introduction

ZIO Redis is a ZIO-native Redis client.It aims to provide atype-safe andperformant API for accessing Redisinstances.

Installation

To use ZIO Redis, add the following line to yourbuild.sbt:

libraryDependencies+="dev.zio"%%"zio-redis"%"1.1.2"

Example

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

  • RedisConfig — Using default one, will connect to thelocalhost:6379 Redis instance.
  • BinaryCodec — In this example, we are going to use the built-inProtobufCodec codec from zio-schema project.

To run this example we should put following dependencies in ourbuild.sbt file:

libraryDependencies++=Seq("dev.zio"%%"zio-redis"%"1.1.2","dev.zio"%%"zio-schema-protobuf"%"1.6.1")
importzio._importzio.redis._importzio.schema._importzio.schema.codec._objectZIORedisExampleextendsZIOAppDefault {objectProtobufCodecSupplierextendsCodecSupplier {defget[A:Schema]:BinaryCodec[A]=ProtobufCodec.protobufCodec  }valmyApp: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 ()overridedefrun=     myApp.provide(Redis.local,ZLayer.succeed[CodecSupplier](ProtobufCodecSupplier))}

Testing

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

libraryDependencies:="dev.zio"%%"zio-redis-embedded"%"1.1.2"

Then you can supplyEmbeddedRedis.layer.orDie as yourRedisConfig and you're good to go!

importzio._importzio.redis._importzio.redis.embedded.EmbeddedRedisimportzio.schema.{DeriveSchema,Schema}importzio.schema.codec.{BinaryCodec,ProtobufCodec}importzio.test._importzio.test.Assertion._importjava.util.UUIDobjectEmbeddedRedisSpecextendsZIOSpecDefault {objectProtobufCodecSupplierextendsCodecSupplier {defget[A:Schema]:BinaryCodec[A]=ProtobufCodec.protobufCodec  }finalcaseclassItemprivate (id:UUID,name:String,quantity:Int)objectItem {implicitvalitemSchema:Schema[Item]=DeriveSchema.gen[Item]  }defspec= 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(EmbeddedRedis.layer,ZLayer.succeed[CodecSupplier](ProtobufCodecSupplier),Redis.singleNode  )@@TestAspect.silentLogging}

Resources

  • ZIO Redis by Dejan Mijic — Redis is one of the most commonly usedin-memory data structure stores. In this talk, Dejan will introduce ZIO Redis, a purely functional, strongly typedclient library backed by ZIO, with excellent performance and extensive support for nearly all of Redis' features. Hewill explain the library design using the bottom-up approach - from communication protocol to public APIs. Finally, hewill wrap the talk by demonstrating the client's usage and discussing its performance characteristics.

Documentation

Learn more on theZIO Redis homepage!

Contributing

For the general guidelines, see ZIOcontributor's guide.

Code of Conduct

See theCode of Conduct

Support

Come chat with us onBadge-Discord.

License

License


[8]ページ先頭

©2009-2025 Movatter.jp