Skip to content

Commit

Permalink
feature: add Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Mar 24, 2023
1 parent 8f55ee7 commit 65f1ade
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ lazy val server = (project in file("server")).settings(
).dependsOn(runtime, registry)

lazy val registry = (project in file("registry")).settings(
libraryDependencies := zioTestDependencies ++ Seq("dev.zio" %% "zio" % zio, "dev.zio" %% "zio-http" % zioHttp)
libraryDependencies := zioTestDependencies ++ Seq(
"dev.zio" %% "zio" % zio,
"dev.zio" %% "zio-http" % zioHttp,
"dev.zio" %% "zio-redis" % "0.1.0",
)
).dependsOn(runtime)

val scala2Version = "2.13.10"
Expand Down
20 changes: 20 additions & 0 deletions registry/src/main/scala/tailcall/registry/SchemaRegistry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tailcall.registry

import tailcall.runtime.ast.{Blueprint, Digest}
import zio._
import zio.redis.Redis

trait SchemaRegistry {
def add(blueprint: Blueprint): Task[Digest]
Expand All @@ -16,6 +17,8 @@ object SchemaRegistry {
def memory: ZLayer[Any, Nothing, SchemaRegistry] =
ZLayer.fromZIO(for { ref <- Ref.make(Map.empty[Digest, Blueprint]) } yield Memory(ref))

def redis: ZLayer[Redis, Nothing, SchemaRegistry] = ZLayer.fromFunction(FromRedis(_))

def add(blueprint: Blueprint): ZIO[SchemaRegistry, Throwable, Digest] =
ZIO.serviceWithZIO[SchemaRegistry](_.add(blueprint))

Expand All @@ -42,4 +45,21 @@ object SchemaRegistry {
override def drop(digest: Digest): UIO[Boolean] =
ref.modify(map => if (map.contains(digest)) (true, map - digest) else (false, map))
}

final case class FromRedis(redis: Redis) extends SchemaRegistry {
override def add(blueprint: Blueprint): Task[Digest] = {
val digest: Digest = blueprint.digest
for { _ <- redis.set(digest.hex, blueprint) } yield digest
}

override def get(id: Digest): Task[Option[Blueprint]] = redis.get(id.hex).returning[Blueprint]

override def list(index: RuntimeFlags, max: RuntimeFlags): Task[List[Blueprint]] =
for {
hexes <- redis.keys("*").returning[String]
blueprints <- ZIO.foreach(hexes)(hex => redis.get(hex).returning[Blueprint])
} yield blueprints.slice(index, index + max).toList.flatMap(_.toList)

override def drop(digest: Digest): Task[Boolean] = redis.del(digest.hex).map(_ > 0)
}
}

0 comments on commit 65f1ade

Please sign in to comment.