Skip to content

Commit

Permalink
Rename to cacheValuesOnly
Browse files Browse the repository at this point in the history
  • Loading branch information
kajebiii committed Sep 26, 2023
1 parent 1184a17 commit 873e990
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
35 changes: 12 additions & 23 deletions zio-cache/shared/src/main/scala/zio/cache/Cache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ object Cache {
capacity: Int,
timeToLive: Duration,
lookup: Lookup[Key, Environment, Error, Value],
storeOnlyIfValue: Boolean = false
cacheValuesOnly: Boolean = false
)(implicit trace: Trace): URIO[Environment, Cache[Key, Error, Value]] =
makeWith(capacity, lookup, storeOnlyIfValue)(_ => timeToLive)
makeWith(capacity, lookup, cacheValuesOnly)(_ => timeToLive)

/**
* Constructs a new cache with the specified capacity, time to live, and
Expand All @@ -116,11 +116,11 @@ object Cache {
def makeWith[Key, Environment, Error, Value](
capacity: Int,
lookup: Lookup[Key, Environment, Error, Value],
storeOnlyIfValue: Boolean = false
cacheValuesOnly: Boolean = false
)(
timeToLive: Exit[Error, Value] => Duration
)(implicit trace: Trace): URIO[Environment, Cache[Key, Error, Value]] =
makeWithKey(capacity, lookup, storeOnlyIfValue)(timeToLive, identity)
makeWithKey(capacity, lookup, cacheValuesOnly)(timeToLive, identity)

/**
* Constructs a new cache with the specified capacity, time to live, and
Expand All @@ -135,7 +135,7 @@ object Cache {
def makeWithKey[In, Key, Environment, Error, Value](
capacity: Int,
lookup: Lookup[In, Environment, Error, Value],
storeOnlyIfValue: Boolean = false
cacheValuesOnly: Boolean = false
)(
timeToLive: Exit[Error, Value] => Duration,
keyBy: In => Key
Expand Down Expand Up @@ -233,12 +233,7 @@ object Cache {
map.remove(k, value)
get(in)
} else {
exit match {
case Left(exit) =>
ZIO.done(exit)
case Right(value) =>
ZIO.done(Exit.Success(value))
}
ZIO.done(exit)
}
case MapValue.Refreshing(
promiseInProgress,
Expand All @@ -249,12 +244,7 @@ object Cache {
if (hasExpired(ttl)) {
promiseInProgress.await
} else {
currentResult match {
case Left(exit) =>
ZIO.done(exit)
case Right(value) =>
ZIO.done(Exit.Success(value))
}
ZIO.done(currentResult)
}
}
}
Expand All @@ -279,9 +269,8 @@ object Cache {
map.remove(k, value)
get(in)
} else {
val rollbackResultIfError = if (storeOnlyIfValue) Some(completedResult) else None
// Only trigger the lookup if we're still the current value, `completedResult`
lookupValueOf(in, promise, rollbackResultIfError).when {
lookupValueOf(in, promise, Some(completedResult)).when {
map.replace(k, completedResult, MapValue.Refreshing(promise, completedResult))
}
}
Expand Down Expand Up @@ -320,17 +309,17 @@ object Cache {
val now = Unsafe.unsafe(implicit u => clock.unsafe.instant())
val entryStats = EntryStats(now)

if (!storeOnlyIfValue)
if (!cacheValuesOnly)
map.put(
key,
MapValue.Complete(new MapKey(key), Left(exit), entryStats, now.plus(timeToLive(exit)))
MapValue.Complete(new MapKey(key), exit, entryStats, now.plus(timeToLive(exit)))
)
else {
exit match {
case Exit.Success(value) =>
map.put(
key,
MapValue.Complete(new MapKey(key), Right(value), entryStats, now.plus(timeToLive(exit)))
MapValue.Complete(new MapKey(key), exit, entryStats, now.plus(timeToLive(exit)))
)
case Exit.Failure(cause) =>
rollbackResultIfError match {
Expand Down Expand Up @@ -373,7 +362,7 @@ object Cache {

final case class Complete[Key, Error, Value](
key: MapKey[Key],
exit: Either[Exit[Error, Value], Value],
exit: Exit[Error, Value],
entryStats: EntryStats,
timeToLive: Instant
) extends MapValue[Key, Error, Value]
Expand Down
6 changes: 3 additions & 3 deletions zio-cache/shared/src/test/scala/zio/cache/CacheSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ object CacheSpec extends ZIOSpecDefault {
val key = 123
for {
ref <- Ref.make(seed)
cache <- Cache.make(1, Duration.Infinity, Lookup(retrieve(ref)), storeOnlyIfValue = true)
cache <- Cache.make(1, Duration.Infinity, Lookup(retrieve(ref)), cacheValuesOnly = true)
val1 <- cache.get(key)
_ <- cache.refresh(key)
_ <- cache.get(key)
Expand All @@ -178,7 +178,7 @@ object CacheSpec extends ZIOSpecDefault {
val key = 1
for {
ref <- Ref.make(seed)
cache <- Cache.make(1, Duration.Infinity, Lookup(retrieve(ref)), storeOnlyIfValue = true)
cache <- Cache.make(1, Duration.Infinity, Lookup(retrieve(ref)), cacheValuesOnly = true)
failure1 <- cache.get(key).either
_ <- cache.refresh(key)
val1 <- cache.get(key).either
Expand Down Expand Up @@ -216,7 +216,7 @@ object CacheSpec extends ZIOSpecDefault {
val cap = 30
for {
ref <- Ref.make(seed)
cache <- Cache.make(cap, Duration.Infinity, Lookup(retrieve(ref)), storeOnlyIfValue = true)
cache <- Cache.make(cap, Duration.Infinity, Lookup(retrieve(ref)), cacheValuesOnly = true)
count0 <- cache.size
_ <- ZIO.foreachDiscard(1 to cap)(key => cache.refresh(key).either)
count1 <- cache.size
Expand Down

0 comments on commit 873e990

Please sign in to comment.