Skip to content

Commit

Permalink
Change names and return type of getNow
Browse files Browse the repository at this point in the history
  • Loading branch information
kyri-petrou committed Jul 5, 2024
1 parent 0e74c5a commit ffda828
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
22 changes: 12 additions & 10 deletions zio-query/shared/src/main/scala/zio/query/Cache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,22 @@ object Cache {
ZIO.succeed(Cache.unsafeMake(expectedNumOfElements))

/**
* An 'Eager' cache is one that doesn't require an effect to look up its
* An 'InMemory' cache is one that doesn't require an effect to look up its
* value. Prefer extending this class when implementing a cache that doesn't
* perform any IO, such as a cache based on a Map.
*/
abstract class Eager extends Cache {
def getOrNull[E, A](request: Request[E, A]): Promise[E, A]
abstract class InMemory extends Cache {
def getNow[E, A](request: Request[E, A]): Option[Promise[E, A]]
def lookupNow[E, A](request: Request[E, A]): Either[Promise[E, A], Promise[E, A]]
def putNow[E, A](request: Request[E, A], result: Promise[E, A]): Unit
def removeNow[E, A](request: Request[E, A]): Unit

final def get[E, A](request: Request[E, A])(implicit trace: Trace): IO[Unit, Promise[E, A]] =
ZIO.suspendSucceed {
val p = getOrNull(request)
if (p eq null) Exit.fail(()) else Exit.succeed(p)
getNow(request) match {
case Some(p) => Exit.succeed(p)
case _ => Exit.fail(())
}
}

final def lookup[E, A, B](
Expand All @@ -103,11 +105,11 @@ object Cache {
ZIO.succeed(removeNow(request))
}

private final class Default(map: ConcurrentHashMap[Request[_, _], Promise[_, _]]) extends Eager {
private final class NonExpiringCache(map: ConcurrentHashMap[Request[_, _], Promise[_, _]]) extends InMemory {
private implicit val unsafe: Unsafe = Unsafe.unsafe

def getOrNull[E, A](request: Request[E, A]): Promise[E, A] =
map.get(request).asInstanceOf[Promise[E, A]]
def getNow[E, A](request: Request[E, A]): Option[Promise[E, A]] =
Option(map.get(request).asInstanceOf[Promise[E, A]])

def lookupNow[E, A](request: Request[E, A]): Either[Promise[E, A], Promise[E, A]] = {
val newPromise = Promise.unsafe.make[E, A](FiberId.None)
Expand All @@ -123,10 +125,10 @@ object Cache {
}

// TODO: Initialize the map with a sensible default value. Default is 16, which seems way too small for a cache
private[query] def unsafeMake(): Cache = new Default(new ConcurrentHashMap())
private[query] def unsafeMake(): Cache = new NonExpiringCache(new ConcurrentHashMap())

private[query] def unsafeMake(expectedNumOfElements: Int): Cache = {
val initialSize = Math.ceil(expectedNumOfElements / 0.75d).toInt
new Default(new ConcurrentHashMap(initialSize))
new NonExpiringCache(new ConcurrentHashMap(initialSize))
}
}
4 changes: 2 additions & 2 deletions zio-query/shared/src/main/scala/zio/query/ZQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1559,8 +1559,8 @@ object ZQuery {
}

cache match {
case cache: Cache.Eager => foldPromise(cache.lookupNow(request))
case cache => CachedResult.Effectful(cache.lookup(request).flatMap(foldPromise(_).toZIO))
case cache: Cache.InMemory => foldPromise(cache.lookupNow(request))
case cache => CachedResult.Effectful(cache.lookup(request).flatMap(foldPromise(_).toZIO))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ private[query] object BlockedRequests {
map: mutable.HashMap[Request[_, _], Exit[Any, Any]]
)(implicit trace: Trace): UIO[Unit] =
cache match {
case cache: Cache.Eager =>
case cache: Cache.InMemory =>
ZIO.succeedUnsafe { implicit unsafe =>
map.foreach { case (request: Request[Any, Any], exit) =>
cache
Expand Down

0 comments on commit ffda828

Please sign in to comment.