diff --git a/README.md b/README.md index c07f757..47fb63e 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,27 @@ Documentation and more detailed examples are hosted on [Github Pages](https://jo ## Usage -To implement a cache we need to divert the prisma client's internals so that we +```ts +client.user.create({ data: { name: "Joel" } }); + +// This populates the cache +client.user.findFirst({ where: { name: "Joel" } }); + +// This is retrieved from the cache +client.user.findFirst({ where: { name: "Joel" } }); +``` + +To control the object used for cache storage you can extend the Prisma class: + +```ts +import { LruCache } from "cached-prisma"; + +class CustomPrisma extends Prisma { + static override cacheFactory = () => new LruCache(100); +} +``` + +To implement the cache we need to divert the prisma client's internals so that we can return cached values without hitting the database. To do this we can use a singleton instance for the client and cache objects. @@ -42,17 +62,6 @@ const cache2 = new Prisma().cache; cache1 === cache2; ``` -The caching mechanism should be configurable. To control the object used for -cache storage you can extend the Prisma class: - -```ts -import { LruCache } from "cached-prisma"; - -class CustomPrisma extends Prisma { - static override cacheFactory = () => new LruCache(100); -} -``` - ## Minimal example Create a prisma schema. diff --git a/src/Memcached.spec.ts b/src/Memcached.spec.ts index e7faeb2..48d415e 100644 --- a/src/Memcached.spec.ts +++ b/src/Memcached.spec.ts @@ -7,6 +7,7 @@ describe("Memcached", () => { expect(await cache.read("a")).toBe("1"); expect(await cache.read("b")).toBeNull(); + cache.close(); }); it("should update entries.", async () => { @@ -15,16 +16,16 @@ describe("Memcached", () => { await cache.write("a", "2"); expect(await cache.read("a")).toBe("2"); + cache.close(); }); it("should not cache for longer than its set lifetime.", async () => { const cache = new Memcached("127.0.0.1:11211", 1); + await cache.write("a", "1"); + await new Promise((resolve) => setTimeout(resolve, 1000)); - setTimeout(() => { - cache.read("a").then((read) => { - expect(read).toBeNull(); - }); - }, 1000); + expect(await cache.read("a")).toBeNull(); + cache.close(); }); }); diff --git a/src/Memcached.ts b/src/Memcached.ts index 375dbed..f965429 100644 --- a/src/Memcached.ts +++ b/src/Memcached.ts @@ -41,4 +41,8 @@ export class Memcached implements Cache { }); }); } + + close(): void { + this.client.end(); + } }