Skip to content

Commit

Permalink
feat: pass lru options through
Browse files Browse the repository at this point in the history
Fixes unjs#466

This changes the `lru-cache` such that the following methods accept LRU
options (and pass them through to the underlying methods):

- `setItem`
- `setItemRaw`
- `hasItem`
  • Loading branch information
43081j committed Dec 29, 2024
1 parent 990e776 commit 84259f6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
31 changes: 21 additions & 10 deletions src/drivers/lru-cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineDriver } from "./utils";
import { LRUCache } from "lru-cache";
import { type TransactionOptions, type StorageValue } from "../types";

type LRUCacheOptions = LRUCache.OptionsBase<string, any, any> &
Partial<LRUCache.OptionsMaxLimit<string, any, any>> &
Expand All @@ -10,6 +11,13 @@ export interface LRUDriverOptions extends LRUCacheOptions {}

const DRIVER_NAME = "lru-cache";

export type HasItemOptions = LRUCache.HasOptions<string, any, any> &
TransactionOptions;
export type GetItemOptions = LRUCache.GetOptions<string, any, any> &
TransactionOptions;
export type SetItemOptions = LRUCache.SetOptions<string, any, any> &
TransactionOptions;

export default defineDriver((opts: LRUDriverOptions = {}) => {
const cache = new LRUCache({
max: 1000,
Expand All @@ -26,20 +34,23 @@ export default defineDriver((opts: LRUDriverOptions = {}) => {
name: DRIVER_NAME,
options: opts,
getInstance: () => cache,
hasItem(key) {
return cache.has(key);
hasItem(key, opts?: HasItemOptions) {
return cache.has(key, opts);
},
getItem(key) {
return cache.get(key) ?? null;
// Note: its important we specify the return types here, since
// otherwise we will end up adding an overload of `getItem` which
// returns `any`
getItem(key, opts?: GetItemOptions): StorageValue {
return cache.get(key, opts) ?? null;
},
getItemRaw(key) {
return cache.get(key) ?? null;
getItemRaw(key, opts?: GetItemOptions): StorageValue {
return cache.get(key, opts) ?? null;
},
setItem(key, value) {
cache.set(key, value);
setItem(key, value, opts?: SetItemOptions) {
cache.set(key, value, opts);
},
setItemRaw(key, value) {
cache.set(key, value);
setItemRaw(key, value, opts?: SetItemOptions) {
cache.set(key, value, opts);
},
removeItem(key) {
cache.delete(key);
Expand Down
40 changes: 39 additions & 1 deletion test/drivers/lru-cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { it, describe, expect } from "vitest";
import { it, describe, expect, vi, afterEach } from "vitest";
import driver from "../../src/drivers/lru-cache";
import { testDriver } from "./utils";
import { LRUCache } from "lru-cache";

describe("drivers: lru-cache", () => {
testDriver({
Expand All @@ -9,6 +10,10 @@ describe("drivers: lru-cache", () => {
});

describe("drivers: lru-cache with size", () => {
afterEach(() => {
vi.restoreAllMocks();
});

testDriver({
driver: driver({
maxEntrySize: 50,
Expand All @@ -24,6 +29,39 @@ describe("drivers: lru-cache with size", () => {
await ctx.storage.setItemRaw("bigBuff", Buffer.alloc(100));
expect(await ctx.storage.getItemRaw("bigBuff")).toBe(null);
});

it("should pass `setItem` options through", async () => {
const spy = vi.spyOn(LRUCache.prototype, "set");
await ctx.storage.setItem("foo", "test_data", {
noUpdateTTL: true,
});

expect(spy).toHaveBeenCalledWith("foo", "test_data", {
noUpdateTTL: true,
});
});

it("should pass `setItemRaw` options through", async () => {
const spy = vi.spyOn(LRUCache.prototype, "set");
await ctx.storage.setItemRaw("foo", "test_data", {
noUpdateTTL: true,
});

expect(spy).toHaveBeenCalledWith("foo", "test_data", {
noUpdateTTL: true,
});
});

it("should pass `hasItem` options through", async () => {
const spy = vi.spyOn(LRUCache.prototype, "has");
await ctx.storage.hasItem("foo", {
updateAgeOnHas: true,
});

expect(spy).toHaveBeenCalledWith("foo", {
updateAgeOnHas: true,
});
});
},
});
});

0 comments on commit 84259f6

Please sign in to comment.