| 45 | * @category constructors |
| 46 | */ |
| 47 | export const make = <K extends Persistence.ResultPersistence.KeyAny, R>(options: { |
| 48 | readonly storeId: string |
| 49 | readonly lookup: (key: K) => Effect.Effect<Schema.WithResult.Success<K>, Schema.WithResult.Failure<K>, R> |
| 50 | readonly timeToLive: (...args: Persistence.ResultPersistence.TimeToLiveArgs<K>) => Duration.DurationInput |
| 51 | readonly inMemoryCapacity?: number | undefined |
| 52 | readonly inMemoryTTL?: Duration.DurationInput | undefined |
| 53 | }): Effect.Effect< |
| 54 | PersistedCache<K>, |
| 55 | never, |
| 56 | Schema.SerializableWithResult.Context<K> | R | Persistence.ResultPersistence | Scope.Scope |
| 57 | > => |
| 58 | Persistence.ResultPersistence.pipe( |
| 59 | Effect.flatMap((_) => |
| 60 | _.make({ |
| 61 | storeId: options.storeId, |
| 62 | timeToLive: options.timeToLive as any |
| 63 | }) |
| 64 | ), |
| 65 | Effect.bindTo("store"), |
| 66 | Effect.bind("inMemoryCache", ({ store }) => |
| 67 | Cache.make({ |
| 68 | lookup: (request: CacheRequest<K>) => { |
| 69 | const effect: Effect.Effect< |
| 70 | Schema.WithResult.Success<K>, |
| 71 | Schema.WithResult.Failure<K> | Persistence.PersistenceError, |
| 72 | Schema.SerializableWithResult.Context<K> | R |
| 73 | > = pipe( |
| 74 | store.get(request.key as any), |
| 75 | Effect.flatMap(Option.match({ |
| 76 | onNone: () => |
| 77 | options.lookup(request.key).pipe( |
| 78 | Effect.exit, |
| 79 | Effect.tap((exit) => store.set(request.key as any, exit)), |
| 80 | Effect.flatten |
| 81 | ), |
| 82 | onSome: identity |
| 83 | })) |
| 84 | ) as any |
| 85 | return request.span._tag === "Some" ? Effect.withParentSpan(effect, request.span.value) : effect |
| 86 | }, |
| 87 | capacity: options.inMemoryCapacity ?? 64, |
| 88 | timeToLive: options.inMemoryTTL ?? 10_000 |
| 89 | })), |
| 90 | Effect.map(({ inMemoryCache, store }) => |
| 91 | identity<PersistedCache<K>>({ |
| 92 | get: (key) => |
| 93 | Effect.serviceOption(Tracer.ParentSpan).pipe( |
| 94 | Effect.flatMap((span) => inMemoryCache.get(new CacheRequest({ key, span }))) |
| 95 | ), |
| 96 | invalidate: (key) => |
| 97 | store.remove(key as any).pipe( |
| 98 | Effect.zipRight(inMemoryCache.invalidate(new CacheRequest({ key, span: Option.none() }))) |
| 99 | ) |
| 100 | }) |
| 101 | ) |
| 102 | ) |