Skip to content

Commit 900275d

Browse files
oskardudyczoskardudycz
authored andcommitted
Simplified cache set and eviction
1 parent 93f027d commit 900275d

2 files changed

Lines changed: 27 additions & 27 deletions

File tree

src/packages/pongo/src/core/collection/pongoCollection.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,14 @@ export const pongoCollection = <
271271
};
272272

273273
const cacheSet = (
274-
key: PongoDocumentCacheKey,
275-
value: PongoDocument | null,
274+
value: WithId<T>,
276275
options: CollectionOperationOptions | undefined,
277276
) => {
277+
const key = cacheKey(value._id);
278278
const txCache = txCacheFor(options);
279+
279280
if (txCache) return txCache.set(key, value, { mainCache: cache });
281+
280282
return cache.set(key, value);
281283
};
282284

@@ -290,9 +292,10 @@ export const pongoCollection = <
290292
};
291293

292294
const cacheDelete = (
293-
key: PongoDocumentCacheKey,
295+
id: string,
294296
options: CollectionOperationOptions | undefined,
295297
) => {
298+
const key = cacheKey(id);
296299
const txCache = txCacheFor(options);
297300
if (txCache) return txCache.delete(key, { mainCache: cache });
298301
return cache.delete(key);
@@ -322,15 +325,13 @@ export const pongoCollection = <
322325
): Promise<PongoDeleteResult & { deletedIds: Set<string> }> => {
323326
await ensureCollectionCreated(options);
324327

325-
const result = await command<{ _id: string; deleted?: bigint }>(
328+
const result = await command<{ _id: string; deleted?: number }>(
326329
SqlFor.deleteManyByIds(ids),
327330
options,
328331
);
329332

330333
const deletedIds = new Set(
331-
result.rows
332-
.filter((row) => row.deleted === undefined || Number(row.deleted) > 0)
333-
.map((row) => row._id),
334+
result.rows.filter((row) => (row.deleted ?? 0) > 0).map((row) => row._id),
334335
);
335336

336337
if (!options?.skipCache) {
@@ -381,8 +382,8 @@ export const pongoCollection = <
381382
const successful = (result.rowCount ?? 0) > 0;
382383

383384
if (successful && !options?.skipCache) {
384-
const doc = { ...document, _id, _version } as PongoDocument;
385-
await cacheSet(cacheKey(_id), doc, options);
385+
const doc = { ...document, _id, _version } as WithId<T>;
386+
await cacheSet(doc, options);
386387
}
387388

388389
return operationResult<PongoInsertOneResult>(
@@ -461,9 +462,7 @@ export const pongoCollection = <
461462

462463
if (opResult.successful && !options?.skipCache) {
463464
const id = idFromFilter(filter);
464-
if (id) {
465-
await cacheDelete(cacheKey(id), options);
466-
}
465+
if (id) await cacheDelete(id, options);
467466
}
468467

469468
return opResult;
@@ -493,16 +492,14 @@ export const pongoCollection = <
493492
);
494493

495494
if (opResult.successful && !options?.skipCache) {
496-
const id = idFromFilter(filter);
497-
if (id) {
498-
const newVersion = opResult.nextExpectedVersion;
495+
const _id = idFromFilter(filter);
496+
if (_id) {
499497
await cacheSet(
500-
cacheKey(id),
501498
{
502-
...(downcasted as PongoDocument),
503-
_id: id,
504-
_version: newVersion,
505-
},
499+
...document,
500+
_id,
501+
_version: opResult.nextExpectedVersion,
502+
} as unknown as WithId<T>,
506503
options,
507504
);
508505
}
@@ -517,6 +514,7 @@ export const pongoCollection = <
517514
): Promise<PongoUpdateManyResult> => {
518515
await ensureCollectionCreated(options);
519516

517+
// TODO: add a similar filter checking if filter is not ids only
520518
const result = await command(SqlFor.updateMany(filter, update), options);
521519

522520
return operationResult<PongoUpdateManyResult>(
@@ -550,7 +548,7 @@ export const pongoCollection = <
550548

551549
if (opResult.successful && !options?.skipCache && filter) {
552550
const id = idFromFilter(filter);
553-
if (id) await cacheSet(cacheKey(id), null, options);
551+
if (id) await cacheDelete(id, options);
554552
}
555553

556554
return opResult;
@@ -597,7 +595,8 @@ export const pongoCollection = <
597595
: null;
598596

599597
const doc = await findOneFromDb(filter!, options);
600-
await cacheSet(cacheKey(id), doc as PongoDocument | null, options);
598+
if (doc) await cacheSet(doc, options);
599+
else await cacheDelete(id, options);
601600
return doc;
602601
}
603602

@@ -692,7 +691,7 @@ export const pongoCollection = <
692691
if (cacheEntries.length > 0)
693692
await cacheReplaceMany(cacheEntries, options);
694693

695-
for (const id of conflictIds) await cacheDelete(cacheKey(id), options);
694+
await cacheDeleteMany([...conflictIds], options);
696695
}
697696

698697
return operationResult<PongoReplaceManyResult>(
@@ -769,7 +768,7 @@ export const pongoCollection = <
769768
? { type: 'delete' as const }
770769
: {
771770
type: 'delete' as const,
772-
_version: existing._version as bigint,
771+
_version: existing._version,
773772
};
774773
return op;
775774
}
@@ -779,7 +778,7 @@ export const pongoCollection = <
779778
: {
780779
type: 'replace' as const,
781780
result: result as WithoutId<T>,
782-
_version: existing!._version as bigint,
781+
_version: existing!._version,
783782
};
784783
return op;
785784
}),
@@ -858,7 +857,8 @@ export const pongoCollection = <
858857
if (op.type === 'insert') {
859858
const succeeded = insertedIds.has(docId);
860859
if (!succeeded) {
861-
void cacheDelete(cacheKey(docId), options);
860+
//TODO: this is not acceptable
861+
void cacheDelete(docId, options);
862862
}
863863
return {
864864
...operationResult<PongoInsertOneResult>(

src/packages/pongo/src/core/typing/operations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ export declare type WithVersion<TSchema> = EnhancedOmit<TSchema, '_version'> & {
369369
};
370370
export type WithoutVersion<T> = Omit<T, '_version'>;
371371

372-
export type WithIdAndVersion<T> = WithId<WithVersion<T>>;
372+
export type WithIdAndVersion<T> = WithId<T> & WithVersion<T>;
373373
export type WithoutIdAndVersion<T> = WithoutId<WithoutVersion<T>>;
374374

375375
/** @public */

0 commit comments

Comments
 (0)