Skip to content

Commit

Permalink
feat: caching
Browse files Browse the repository at this point in the history
  • Loading branch information
SegaraRai committed Jan 13, 2025
1 parent 082ff42 commit 367b8d6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-files-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fraci": minor
---

Add caching feature.
58 changes: 49 additions & 9 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,63 @@ export interface FraciOptions<D extends string, L extends string> {
maxRetries?: number;
}

export type FraciCache = Map<string, unknown> & { __fraci__: never };

/**
* Create cache.
*
* @returns Cache
*/
export function createFraciCache(): FraciCache {
return new Map() as FraciCache;
}

function withCache<T>(
cache: FraciCache | undefined,
key: string,
fn: () => T
): T {
if (!cache) {
return fn();
}

let value = cache.get(key) as T | undefined;
if (value === undefined) {
value = fn();
cache.set(key, value);
}

return value;
}

/**
* Create fractional indexing utility.
*
* @param options Options
* @param cache Cache (optional)
* @returns Fractional indexing utility
*/
export function fraci<D extends string, L extends string, X>({
digitBase,
lengthBase,
maxLength = DEFAULT_MAX_LENGTH,
maxRetries = DEFAULT_MAX_RETRIES,
}: FraciOptions<D, L>): Fraci<D, L, X> {
export function fraci<D extends string, L extends string, X>(
{
digitBase,
lengthBase,
maxLength = DEFAULT_MAX_LENGTH,
maxRetries = DEFAULT_MAX_RETRIES,
}: FraciOptions<D, L>,
cache?: FraciCache
): Fraci<D, L, X> {
type F = FractionalIndex<D, L, X>;

const [digBaseForward, digBaseReverse] = createDigitBaseMap(digitBase);
const [lenBaseForward, lenBaseReverse] =
createIntegerLengthBaseMap(lengthBase);
const [digBaseForward, digBaseReverse] = withCache(
cache,
`D${digitBase}`,
createDigitBaseMap.bind(null, digitBase)
);
const [lenBaseForward, lenBaseReverse] = withCache(
cache,
`L${lengthBase}`,
createIntegerLengthBaseMap.bind(null, lengthBase)
);
const smallestInteger = getSmallestInteger(digBaseForward, lenBaseForward);

return {
Expand Down
18 changes: 12 additions & 6 deletions src/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Prisma, PrismaClient } from "@prisma/client/extension.js";
import type { PrismaClientKnownRequestError } from "@prisma/client/runtime/library.js";
import {
createFraciCache,
DEFAULT_MAX_LENGTH,
DEFAULT_MAX_RETRIES,
fraci,
Expand Down Expand Up @@ -360,6 +361,8 @@ export function fraciExtension<Options extends FraciExtensionOptions>({
maxRetries = DEFAULT_MAX_RETRIES,
}: Options) {
return Prisma.defineExtension((client) => {
const cache = createFraciCache();

type HelperValue = FraciForPrisma<any, any, any, any, any>;

const helperMap = new Map<string, HelperValue>();
Expand All @@ -376,12 +379,15 @@ export function fraciExtension<Options extends FraciExtensionOptions>({
);
}

const helper = fraci({
digitBase,
lengthBase,
maxLength,
maxRetries,
});
const helper = fraci(
{
digitBase,
lengthBase,
maxLength,
maxRetries,
},
cache
);

const indicesForAfter = async (
cursor: any,
Expand Down

0 comments on commit 367b8d6

Please sign in to comment.