- Notifications
You must be signed in to change notification settings - Fork64
zio/zio-redis
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
ZIO Redis is a ZIO-native Redis client.It aims to provide atype-safe andperformant API for accessing Redisinstances.
To use ZIO Redis, add the following line to yourbuild.sbt
:
libraryDependencies+="dev.zio"%%"zio-redis"%"1.1.2"
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 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 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))}
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}
- 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.
Learn more on theZIO Redis homepage!
For the general guidelines, see ZIOcontributor's guide.
See theCode of Conduct
About
A ZIO-based redis client