| 310 | }) |
| 311 | |
| 312 | class CacheImpl<in out Key, in out Value, in out Error> implements Cache.Cache<Key, Value, Error> { |
| 313 | readonly [CacheTypeId] = cacheVariance |
| 314 | readonly [ConsumerCacheTypeId] = consumerCacheVariance |
| 315 | readonly cacheState: CacheState<Key, Value, Error> |
| 316 | constructor( |
| 317 | readonly capacity: number, |
| 318 | readonly context: Context.Context<any>, |
| 319 | readonly fiberId: FiberId.FiberId, |
| 320 | readonly lookup: Cache.Lookup<Key, Value, Error, any>, |
| 321 | readonly timeToLive: (exit: Exit.Exit<Value, Error>) => Duration.DurationInput |
| 322 | ) { |
| 323 | this.cacheState = initialCacheState() |
| 324 | } |
| 325 | |
| 326 | get(key: Key): Effect.Effect<Value, Error> { |
| 327 | return core.map(this.getEither(key), Either.merge) |
| 328 | } |
| 329 | |
| 330 | get cacheStats(): Effect.Effect<Cache.CacheStats> { |
| 331 | return core.sync(() => |
| 332 | makeCacheStats({ |
| 333 | hits: this.cacheState.hits, |
| 334 | misses: this.cacheState.misses, |
| 335 | size: MutableHashMap.size(this.cacheState.map) |
| 336 | }) |
| 337 | ) |
| 338 | } |
| 339 | |
| 340 | getOption(key: Key): Effect.Effect<Option.Option<Value>, Error> { |
| 341 | return core.suspend(() => |
| 342 | Option.match(MutableHashMap.get(this.cacheState.map, key), { |
| 343 | onNone: () => { |
| 344 | const mapKey = makeMapKey(key) |
| 345 | this.trackAccess(mapKey) |
| 346 | this.trackMiss() |
| 347 | return core.succeed(Option.none<Value>()) |
| 348 | }, |
| 349 | onSome: (value) => this.resolveMapValue(value) |
| 350 | }) |
| 351 | ) |
| 352 | } |
| 353 | |
| 354 | getOptionComplete(key: Key): Effect.Effect<Option.Option<Value>> { |
| 355 | return core.suspend(() => |
| 356 | Option.match(MutableHashMap.get(this.cacheState.map, key), { |
| 357 | onNone: () => { |
| 358 | const mapKey = makeMapKey(key) |
| 359 | this.trackAccess(mapKey) |
| 360 | this.trackMiss() |
| 361 | return core.succeed(Option.none<Value>()) |
| 362 | }, |
| 363 | onSome: (value) => this.resolveMapValue(value, true) as Effect.Effect<Option.Option<Value>> |
| 364 | }) |
| 365 | ) |
| 366 | } |
| 367 | |
| 368 | contains(key: Key): Effect.Effect<boolean> { |
| 369 | return core.sync(() => MutableHashMap.has(this.cacheState.map, key)) |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…