import * as Context from "@effect/data/Context" import * as Data from "@effect/data/Data" import * as Duration from "@effect/data/Duration" import * as Equal from "@effect/data/Equal" import { pipe } from "@effect/data/Function" import * as HashSet from "@effect/data/HashSet" import * as MutableHashMap from "@effect/data/MutableHashMap" import * as MutableQueue from "@effect/data/MutableQueue" import * as MutableRef from "@effect/data/MutableRef" import * as Option from "@effect/data/Option" import { pipeArguments } from "@effect/data/Pipeable" import type * as Cache from "@effect/io/Cache" import type * as Clock from "@effect/io/Clock" import type * as Effect from "@effect/io/Effect" import * as Exit from "@effect/io/Exit" import * as _cache from "@effect/io/internal/cache" import * as core from "@effect/io/internal/core" import * as effect from "@effect/io/internal/effect" import * as fiberRuntime from "@effect/io/internal/fiberRuntime" import * as Scope from "@effect/io/Scope" import type * as ScopedCache from "@effect/io/ScopedCache" /** * The `CacheState` represents the mutable state underlying the cache. * * @internal */ export interface CacheState { map: MutableHashMap.MutableHashMap> keys: _cache.KeySet accesses: MutableQueue.MutableQueue<_cache.MapKey> updating: MutableRef.MutableRef hits: number misses: number } /** @internal */ export const makeCacheState = ( map: MutableHashMap.MutableHashMap>, keys: _cache.KeySet, accesses: MutableQueue.MutableQueue<_cache.MapKey>, updating: MutableRef.MutableRef, hits: number, misses: number ): CacheState => ({ map, keys, accesses, updating, hits, misses }) /** * Constructs an initial cache state. * * @internal */ export const initialCacheState = (): CacheState => makeCacheState( MutableHashMap.empty(), _cache.makeKeySet(), MutableQueue.unbounded(), MutableRef.make(false), 0, 0 ) /** * A `MapValue` represents a value in the cache. A value may either be * `Pending` with a `Promise` that will contain the result of computing the * lookup function, when it is available, or `Complete` with an `Exit` value * that contains the result of computing the lookup function. * * @internal */ export type MapValue = | Complete | Pending | Refreshing /** @internal */ export interface Complete { readonly _tag: "Complete" readonly key: _cache.MapKey readonly exit: Exit.Exit readonly ownerCount: MutableRef.MutableRef readonly entryStats: Cache.EntryStats readonly timeToLive: number } /** @internal */ export interface Pending { readonly _tag: "Pending" readonly key: _cache.MapKey readonly scoped: Effect.Effect> } /** @internal */ export interface Refreshing { readonly _tag: "Refreshing" readonly scoped: Effect.Effect> readonly complete: Complete } /** @internal */ export const complete = ( key: _cache.MapKey, exit: Exit.Exit, ownerCount: MutableRef.MutableRef, entryStats: Cache.EntryStats, timeToLive: number ): Complete => Data.struct({ _tag: "Complete", key, exit, ownerCount, entryStats, timeToLive }) /** @internal */ export const pending = ( key: _cache.MapKey, scoped: Effect.Effect> ): Pending => Data.struct({ _tag: "Pending", key, scoped }) /** @internal */ export const refreshing = ( scoped: Effect.Effect>, complete: Complete ): Refreshing => Data.struct({ _tag: "Refreshing", scoped, complete }) /** @internal */ export const toScoped = ( self: Complete ): Effect.Effect => Exit.matchEffect(self.exit, { onFailure: (cause) => core.failCause(cause), onSuccess: ([value]) => fiberRuntime.acquireRelease( core.as(core.sync(() => MutableRef.incrementAndGet(self.ownerCount)), value), () => releaseOwner(self) ) }) /** @internal */ export const releaseOwner = ( self: Complete ): Effect.Effect => Exit.matchEffect(self.exit, { onFailure: () => core.unit, onSuccess: ([, finalizer]) => core.flatMap( core.sync(() => MutableRef.decrementAndGet(self.ownerCount)), (numOwner) => effect.when(finalizer(Exit.unit), () => numOwner === 0) ) }) /** @internal */ const ScopedCacheSymbolKey = "@effect/io/ScopedCache" /** @internal */ export const ScopedCacheTypeId: ScopedCache.ScopedCacheTypeId = Symbol.for( ScopedCacheSymbolKey ) as ScopedCache.ScopedCacheTypeId const scopedCacheVariance = { _Key: (_: unknown) => _, _Error: (_: never) => _, _Value: (_: never) => _ } class ScopedCacheImpl implements ScopedCache.ScopedCache { readonly [ScopedCacheTypeId] = scopedCacheVariance readonly cacheState: CacheState constructor( readonly capacity: number, readonly scopedLookup: ScopedCache.Lookup, readonly clock: Clock.Clock, readonly timeToLive: (exit: Exit.Exit) => Duration.Duration, readonly context: Context.Context ) { this.cacheState = initialCacheState() } pipe() { return pipeArguments(this, arguments) } cacheStats(): Effect.Effect { return core.sync(() => _cache.makeCacheStats({ hits: this.cacheState.hits, misses: this.cacheState.misses, size: MutableHashMap.size(this.cacheState.map) }) ) } getOption(key: Key): Effect.Effect> { return core.suspend(() => Option.match(MutableHashMap.get(this.cacheState.map, key), { onNone: () => effect.succeedNone, onSome: (value) => core.flatten(this.resolveMapValue(value)) }) ) } getOptionComplete(key: Key): Effect.Effect> { return core.suspend(() => Option.match(MutableHashMap.get(this.cacheState.map, key), { onNone: () => effect.succeedNone, onSome: (value) => core.flatten(this.resolveMapValue(value, true)) as Effect.Effect> }) ) } contains(key: Key): Effect.Effect { return core.sync(() => MutableHashMap.has(this.cacheState.map, key)) } entryStats(key: Key): Effect.Effect> { return core.sync(() => { const value = Option.getOrUndefined(MutableHashMap.get(this.cacheState.map, key)) if (value === undefined) { return Option.none() } switch (value._tag) { case "Complete": { return Option.some(_cache.makeEntryStats(value.entryStats.loadedMillis)) } case "Pending": { return Option.none() } case "Refreshing": { return Option.some(_cache.makeEntryStats(value.complete.entryStats.loadedMillis)) } } }) } get(key: Key): Effect.Effect { return pipe( this.lookupValueOf(key), effect.memoize, core.flatMap((lookupValue) => core.suspend(() => { let k: _cache.MapKey | undefined = undefined let value = Option.getOrUndefined(MutableHashMap.get(this.cacheState.map, key)) if (value === undefined) { k = _cache.makeMapKey(key) if (MutableHashMap.has(this.cacheState.map, key)) { value = Option.getOrUndefined(MutableHashMap.get(this.cacheState.map, key)) } else { MutableHashMap.set(this.cacheState.map, key, pending(k, lookupValue)) } } if (value === undefined) { this.trackMiss() return core.zipRight( this.ensureMapSizeNotExceeded(k!), lookupValue ) } return core.map( this.resolveMapValue(value), core.flatMap(Option.match({ onNone: () => { const val = value as Complete const current = Option.getOrUndefined(MutableHashMap.get(this.cacheState.map, key)) if (Equal.equals(current, value)) { MutableHashMap.remove(this.cacheState.map, key) } return pipe( this.ensureMapSizeNotExceeded(val.key), core.zipRight(releaseOwner(val)), core.zipRight(this.get(key)) ) }, onSome: core.succeed })) ) }) ), core.flatten ) } invalidate(key: Key): Effect.Effect { return core.suspend(() => { if (MutableHashMap.has(this.cacheState.map, key)) { const mapValue = Option.getOrUndefined(MutableHashMap.get(this.cacheState.map, key))! MutableHashMap.remove(this.cacheState.map, key) switch (mapValue._tag) { case "Complete": { return releaseOwner(mapValue) } case "Pending": { return core.unit } case "Refreshing": { return releaseOwner(mapValue.complete) } } } return core.unit }) } invalidateAll(): Effect.Effect { return fiberRuntime.forEachParUnboundedDiscard( HashSet.fromIterable(Array.from(this.cacheState.map).map(([key]) => key)), (key) => this.invalidate(key), false ) } refresh(key: Key): Effect.Effect { return pipe( this.lookupValueOf(key), effect.memoize, core.flatMap((scoped) => { let value = Option.getOrUndefined(MutableHashMap.get(this.cacheState.map, key)) let newKey: _cache.MapKey | undefined = undefined if (value === undefined) { newKey = _cache.makeMapKey(key) if (MutableHashMap.has(this.cacheState.map, key)) { value = Option.getOrUndefined(MutableHashMap.get(this.cacheState.map, key)) } else { MutableHashMap.set(this.cacheState.map, key, pending(newKey, scoped)) } } let finalScoped: Effect.Effect> if (value === undefined) { finalScoped = core.zipRight( this.ensureMapSizeNotExceeded(newKey!), scoped ) } else { switch (value._tag) { case "Complete": { if (this.hasExpired(value.timeToLive)) { finalScoped = core.succeed(this.get(key)) } else { const current = Option.getOrUndefined(MutableHashMap.get(this.cacheState.map, key)) if (Equal.equals(current, value)) { const mapValue = refreshing(scoped, value) MutableHashMap.set(this.cacheState.map, key, mapValue) finalScoped = scoped } else { finalScoped = core.succeed(this.get(key)) } } break } case "Pending": { finalScoped = value.scoped break } case "Refreshing": { finalScoped = value.scoped break } } } return core.flatMap(finalScoped, (s) => fiberRuntime.scopedEffect(core.asUnit(s))) }) ) } size(): Effect.Effect { return core.sync(() => MutableHashMap.size(this.cacheState.map)) } resolveMapValue(value: MapValue, ignorePending = false) { switch (value._tag) { case "Complete": { this.trackHit() if (this.hasExpired(value.timeToLive)) { return core.succeed(effect.succeedNone) } return core.as( this.ensureMapSizeNotExceeded(value.key), effect.asSome(toScoped(value)) ) } case "Pending": { this.trackHit() if (ignorePending) { return core.succeed(effect.succeedNone) } return core.zipRight( this.ensureMapSizeNotExceeded(value.key), core.map(value.scoped, effect.asSome) ) } case "Refreshing": { this.trackHit() if (this.hasExpired(value.complete.timeToLive)) { if (ignorePending) { return core.succeed(effect.succeedNone) } return core.zipRight( this.ensureMapSizeNotExceeded(value.complete.key), core.map(value.scoped, effect.asSome) ) } return core.as( this.ensureMapSizeNotExceeded(value.complete.key), effect.asSome(toScoped(value.complete)) ) } } } lookupValueOf(key: Key): Effect.Effect> { return pipe( core.onInterrupt( core.flatMap(Scope.make(), (scope) => pipe( this.scopedLookup(key), core.provideContext(pipe(this.context, Context.add(Scope.Scope, scope))), core.exit, core.map((exit) => [exit, ((exit) => Scope.close(scope, exit)) as Scope.Scope.Finalizer] as const) )), () => core.sync(() => MutableHashMap.remove(this.cacheState.map, key)) ), core.flatMap(([exit, release]) => { const now = this.clock.unsafeCurrentTimeMillis() const expiredAt = now + Duration.toMillis(this.timeToLive(exit)) switch (exit._tag) { case "Success": { const exitWithFinalizer: Exit.Exit = Exit.succeed([ exit.value, release ]) const completedResult = complete( _cache.makeMapKey(key), exitWithFinalizer, MutableRef.make(1), _cache.makeEntryStats(now), expiredAt ) let previousValue: MapValue | undefined = undefined if (MutableHashMap.has(this.cacheState.map, key)) { previousValue = Option.getOrUndefined(MutableHashMap.get(this.cacheState.map, key)) } MutableHashMap.set(this.cacheState.map, key, completedResult) return core.sync(() => core.flatten( core.as( this.cleanMapValue(previousValue), toScoped(completedResult) ) ) ) } case "Failure": { const completedResult = complete( _cache.makeMapKey(key), exit as Exit.Exit, MutableRef.make(0), _cache.makeEntryStats(now), expiredAt ) let previousValue: MapValue | undefined = undefined if (MutableHashMap.has(this.cacheState.map, key)) { previousValue = Option.getOrUndefined(MutableHashMap.get(this.cacheState.map, key)) } MutableHashMap.set(this.cacheState.map, key, completedResult) return core.zipRight( release(exit), core.sync(() => core.flatten( core.as( this.cleanMapValue(previousValue), toScoped(completedResult) ) ) ) ) } } }), effect.memoize, core.flatten ) } hasExpired(timeToLive: number): boolean { return this.clock.unsafeCurrentTimeMillis() > timeToLive } trackHit(): void { this.cacheState.hits = this.cacheState.hits + 1 } trackMiss(): void { this.cacheState.misses = this.cacheState.misses + 1 } trackAccess(key: _cache.MapKey): Array> { const cleanedKeys: Array> = [] MutableQueue.offer(this.cacheState.accesses, key) if (MutableRef.compareAndSet(this.cacheState.updating, false, true)) { let loop = true while (loop) { const key = MutableQueue.poll(this.cacheState.accesses, MutableQueue.EmptyMutableQueue) if (key === MutableQueue.EmptyMutableQueue) { loop = false } else { this.cacheState.keys.add(key) } } let size = MutableHashMap.size(this.cacheState.map) loop = size > this.capacity while (loop) { const key = this.cacheState.keys.remove() if (key === undefined) { loop = false } else { if (MutableHashMap.has(this.cacheState.map, key.current)) { const removed = Option.getOrUndefined(MutableHashMap.get(this.cacheState.map, key.current))! MutableHashMap.remove(this.cacheState.map, key.current) size = size - 1 cleanedKeys.push(removed) loop = size > this.capacity } } } MutableRef.set(this.cacheState.updating, false) } return cleanedKeys } cleanMapValue(mapValue: MapValue | undefined): Effect.Effect { if (mapValue === undefined) { return core.unit } switch (mapValue._tag) { case "Complete": { return releaseOwner(mapValue) } case "Pending": { return core.unit } case "Refreshing": { return releaseOwner(mapValue.complete) } } } ensureMapSizeNotExceeded(key: _cache.MapKey): Effect.Effect { return fiberRuntime.forEachParUnboundedDiscard( this.trackAccess(key), (cleanedMapValue) => this.cleanMapValue(cleanedMapValue), false ) } } /** @internal */ export const make = ( options: { readonly lookup: ScopedCache.Lookup readonly capacity: number readonly timeToLive: Duration.DurationInput } ): Effect.Effect> => { const timeToLive = Duration.decode(options.timeToLive) return makeWith({ capacity: options.capacity, lookup: options.lookup, timeToLive: () => timeToLive }) } /** @internal */ export const makeWith = ( options: { readonly capacity: number readonly lookup: ScopedCache.Lookup readonly timeToLive: (exit: Exit.Exit) => Duration.DurationInput } ): Effect.Effect> => core.flatMap( effect.clock, (clock) => buildWith( options.capacity, options.lookup, clock, (exit) => Duration.decode(options.timeToLive(exit)) ) ) const buildWith = ( capacity: number, scopedLookup: ScopedCache.Lookup, clock: Clock.Clock, timeToLive: (exit: Exit.Exit) => Duration.Duration ): Effect.Effect> => fiberRuntime.acquireRelease( core.flatMap( core.context(), (context) => core.sync(() => new ScopedCacheImpl( capacity, scopedLookup, clock, timeToLive, context ) ) ), (cache) => cache.invalidateAll() )