@@ -3,18 +3,18 @@ package zio.cache
3
3
import zio .cache .ScopedCache .Finalizer
4
4
import zio .cache .ScopedCacheImplementation .{CacheState , MapValue }
5
5
import zio .internal .MutableConcurrentQueue
6
- import zio .{Exit , IO , Scope , UIO , ZEnvironment , ZIO }
6
+ import zio .{Clock , Exit , IO , Scope , UIO , Unsafe , ZEnvironment , ZIO }
7
7
8
- import java .time .{Clock , Duration , Instant }
8
+ import java .time .{Duration , Instant }
9
9
import java .util
10
10
import java .util .concurrent .atomic .{AtomicBoolean , AtomicInteger , LongAdder }
11
11
import scala .jdk .CollectionConverters ._
12
12
13
13
private [cache] class ScopedCacheImplementation [Key , Environment , Error , Value ](
14
14
capacity : Int ,
15
15
scopedLookup : ScopedLookup [Key , Environment , Error , Value ],
16
- clock : Clock ,
17
16
timeToLive : Exit [Error , Value ] => Duration ,
17
+ clock : Clock ,
18
18
environment : ZEnvironment [Environment ]
19
19
) extends ScopedCache [Key , Error , Value ] {
20
20
private val cacheState = CacheState .initial[Key , Error , Value ]()
@@ -86,7 +86,7 @@ private[cache] class ScopedCacheImplementation[Key, Environment, Error, Value](
86
86
}
87
87
}
88
88
89
- def freeExpired : UIO [Int ] = ZIO .suspendSucceed {
89
+ def freeExpired : UIO [Int ] = ZIO .suspendSucceedUnsafe { implicit unsafe =>
90
90
var expiredKey = List .empty[Key ]
91
91
map.entrySet().forEach { entry =>
92
92
entry.getValue match {
@@ -106,7 +106,7 @@ private[cache] class ScopedCacheImplementation[Key, Environment, Error, Value](
106
106
107
107
override def get (k : Key ): ZIO [Scope , Error , Value ] =
108
108
lookupValueOf(k).memoize.flatMap { lookupValue =>
109
- ZIO .suspendSucceed {
109
+ ZIO .suspendSucceedUnsafe { implicit unsafe =>
110
110
var key : MapKey [Key ] = null
111
111
var value = map.get(k)
112
112
if (value eq null ) {
@@ -155,7 +155,7 @@ private[cache] class ScopedCacheImplementation[Key, Environment, Error, Value](
155
155
case MapValue .Pending (_, scopedEffect) =>
156
156
scopedEffect
157
157
case completeResult @ MapValue .Complete (_, _, _, _, ttl) =>
158
- if (hasExpired(ttl)) {
158
+ if (hasExpired(ttl)( Unsafe .unsafe) ) {
159
159
ZIO .succeed(get(k))
160
160
} else {
161
161
if (map.replace(k, completeResult, MapValue .Refreshing (scoped, completeResult))) {
@@ -200,7 +200,7 @@ private[cache] class ScopedCacheImplementation[Key, Environment, Error, Value](
200
200
} yield (exit, scope.close(_)))
201
201
.onInterrupt(ZIO .succeed(map.remove(key)))
202
202
.flatMap { case (exit, release) =>
203
- val now = Instant .now (clock)
203
+ val now = Unsafe .unsafe (clock.unsafe.instant()(_) )
204
204
val expiredAt = now.plus(timeToLive(exit))
205
205
exit match {
206
206
case Exit .Success (value) =>
@@ -236,8 +236,8 @@ private[cache] class ScopedCacheImplementation[Key, Environment, Error, Value](
236
236
.memoize
237
237
} yield scopedEffect.flatten
238
238
239
- private def hasExpired (timeToLive : Instant ) =
240
- Instant .now(clock ).isAfter(timeToLive)
239
+ private def hasExpired (timeToLive : Instant )( implicit unsafe : Unsafe ) =
240
+ clock.unsafe.instant( ).isAfter(timeToLive)
241
241
}
242
242
243
243
object ScopedCacheImplementation {
0 commit comments