From 03dfb2ecb818dd7b41bd82b9ef434c396258600f Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Mon, 6 Feb 2023 15:27:57 +0100 Subject: [PATCH] feat: `lru-cache` driver (#146) --- README.md | 15 ++++++++++++++ package.json | 1 + pnpm-lock.yaml | 7 +++++++ src/drivers/lru-cache.ts | 42 ++++++++++++++++++++++++++++++++++++++++ src/index.ts | 1 + 5 files changed, 66 insertions(+) create mode 100644 src/drivers/lru-cache.ts diff --git a/README.md b/README.md index 1af1884b..4c87537c 100644 --- a/README.md +++ b/README.md @@ -360,6 +360,21 @@ const storage = createStorage({ }); ``` +### `lru-cache` (universal) + +Keeps cached data in memory using [LRU Cache](https://www.npmjs.com/package/lru-cache). + +See [`lru-cache`](https://www.npmjs.com/package/lru-cache) for supported options. By default `{ maxSize: 500 }` is used. + +```js +import { createStorage } from "unstorage"; +import lruCacheDriver from "unstorage/drivers/lru-cache"; + +const storage = createStorage({ + driver: lruCacheDriver(), +}); +``` + ### `overlay` (universal) This is a special driver that creates a multi-layer overlay driver. diff --git a/package.json b/package.json index b643b348..633794d2 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "h3": "^1.1.0", "ioredis": "^5.3.0", "listhen": "^1.0.2", + "lru-cache": "^7.14.1", "mkdir": "^0.0.2", "mri": "^1.2.0", "node-fetch-native": "^1.0.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5ed73e68..50e0c32a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -22,6 +22,7 @@ specifiers: jiti: ^1.16.2 jsdom: ^21.1.0 listhen: ^1.0.2 + lru-cache: ^7.14.1 mkdir: ^0.0.2 monaco-editor: ^0.34.1 mri: ^1.2.0 @@ -45,6 +46,7 @@ dependencies: h3: 1.1.0 ioredis: 5.3.0 listhen: 1.0.2 + lru-cache: 7.14.1 mkdir: 0.0.2 mri: 1.2.0 node-fetch-native: 1.0.1 @@ -3302,6 +3304,11 @@ packages: yallist: 4.0.0 dev: true + /lru-cache/7.14.1: + resolution: {integrity: sha512-ysxwsnTKdAx96aTRdhDOCQfDgbHnt8SK0KY8SEjO0wHinhWOFTESbjVCMPbU1uGXg/ch4lifqx0wfjOawU2+WA==} + engines: {node: '>=12'} + dev: false + /magic-string/0.25.9: resolution: {integrity: sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ==} dependencies: diff --git a/src/drivers/lru-cache.ts b/src/drivers/lru-cache.ts new file mode 100644 index 00000000..37752671 --- /dev/null +++ b/src/drivers/lru-cache.ts @@ -0,0 +1,42 @@ +import { defineDriver } from "./utils"; +import LRU, { SharedOptions as _LRUOptions } from "lru-cache"; + +type LRUOptions = _LRUOptions; +export interface LRUDriverOptions extends LRUOptions {} + +export default defineDriver((opts: LRUDriverOptions) => { + const cache = new LRU({ + ...opts, + maxSize: 500, + }); + + return { + hasItem(key) { + return cache.has(key); + }, + getItem(key) { + return cache.get(key) || null; + }, + getItemRaw(key) { + return cache.get(key) || null; + }, + setItem(key, value) { + cache.set(key, value); + }, + setItemRaw(key, value) { + cache.set(key, value); + }, + removeItem(key) { + cache.delete(key); + }, + getKeys() { + return Array.from(cache.keys()); + }, + clear() { + cache.clear(); + }, + dispose() { + cache.clear(); + }, + }; +}); diff --git a/src/index.ts b/src/index.ts index dbb12ce4..0c533fcd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,7 @@ export const builtinDrivers = { github: "unstorage/drivers/github", http: "unstorage/drivers/http", localStorage: "unstorage/drivers/localstorage", + lruCache: "unstorage/drivers/lru-cache", localstorage: "unstorage/drivers/localstorage", memory: "unstorage/drivers/memory", overlay: "unstorage/drivers/overlay",