Skip to content

Commit ad27fda

Browse files
committed
refactor: migrate from @cacheable/node-cache to node-cache and update related types
1 parent 30b7fe6 commit ad27fda

File tree

7 files changed

+123
-164
lines changed

7 files changed

+123
-164
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Node Cache Plus
22

3-
`node-cache-plus` is a simple wrapper around [`@cacheable/node-cache`](https://github.com/jaredwray/cacheable/tree/main/packages/node-cache) with additional features such tag based invalidation, factory functions, and other helpers for in-memory caching in Node.js applications.
3+
`node-cache-plus` is a wrapper around the popular library [`node-cache`](https://github.com/node-cache/node-cache/tree/master) with additional features such tag-based invalidation, factory functions, and other helpers for in-memory caching in Node.js applications.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "node-cache-plus",
33
"version": "1.0.1",
44
"type": "module",
5-
"description": "A wrapper around @cacheable/node-cache with additional features such tag based invalidation, factory functions, and more.",
5+
"description": "A wrapper around the popular node-cache library, featuring tag-based invalidation, factory functions, and more.",
66
"main": "dist/index.js",
77
"types": "dist/index.d.ts",
88
"files": [
@@ -73,6 +73,6 @@
7373
"vitest": "~2.1.8"
7474
},
7575
"dependencies": {
76-
"@cacheable/node-cache": "1.5.1"
76+
"node-cache": "^5.1.2"
7777
}
7878
}

pnpm-lock.yaml

Lines changed: 17 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cache.test.ts

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import NodeCache from "@cacheable/node-cache";
21
import { beforeEach, describe, expect, it } from "vitest";
32
import { Cache } from "./Cache";
43

@@ -10,7 +9,7 @@ describe("Cache", () => {
109
});
1110

1211
it("should set, get, and take values correctly", () => {
13-
cache.set("key0", "value0", 60, []);
12+
cache.set("key0", "value0", 60);
1413
cache.set("key1", "value1", 60, ["tag1"]);
1514
cache.set("key2", "value2", 60, ["tag2"]);
1615
cache.set("key3", "value3", 60, ["tag1", "tag2"]);
@@ -28,13 +27,22 @@ describe("Cache", () => {
2827
expect(cache.get("key4")).toBe("value4");
2928
expect(cache.get("key5")).toBe("value5");
3029
expect(cache.get("key6")).toBe("value6");
30+
});
31+
32+
it("should take values correctly", () => {
33+
cache.set("key0", "value0", 60, ["test"]);
34+
35+
// verify tag is set
36+
expect(cache.getTagMap().get("test")).toEqual(new Set(["key0"]));
3137

3238
// take value
3339
expect(cache.take("key0")).toBe("value0");
3440

35-
// check if tags are removed
41+
// check if key was removed
3642
expect(cache.get("key0")).toBeUndefined();
37-
expect(cache.get("key1")).toBe("value1");
43+
44+
// check if tag was removed
45+
expect(cache.getTagMap().get("test")).toBeUndefined();
3846
});
3947

4048
it("should delete a key", () => {
@@ -101,9 +109,9 @@ describe("Cache", () => {
101109

102110
it("should set multiple values using mset", () => {
103111
const items = [
104-
{ key: "key1", value: "value1", ttl: 60, tags: ["tag1"] },
105-
{ key: "key2", value: "value2", ttl: 60, tags: ["tag2"] },
106-
{ key: "key3", value: "value3", ttl: 60, tags: ["tag1", "tag2"] },
112+
{ key: "key1", val: "value1", ttl: 60, tags: ["tag1"] },
113+
{ key: "key2", val: "value2", ttl: 60, tags: ["tag2"] },
114+
{ key: "key3", val: "value3", ttl: 60, tags: ["tag1", "tag2"] },
107115
];
108116
cache.mset(items);
109117

@@ -118,12 +126,20 @@ describe("Cache", () => {
118126
cache.set("key3", "value3", 60, ["tag1", "tag2"]);
119127

120128
const values = cache.mget(["key1", "key2", "key3"]);
121-
expect(values).toEqual(["value1", "value2", "value3"]);
129+
expect(values).toEqual({
130+
key1: "value1",
131+
key2: "value2",
132+
key3: "value3",
133+
});
122134

123135
// invalid tag & check if removed
124136
cache.invalidateTagsIntersection(["tag1", "tag2"]);
125137
const valuesAfterInvalidation = cache.mget(["key1", "key2", "key3"]);
126-
expect(valuesAfterInvalidation).toEqual(["value1", "value2", undefined]);
138+
expect(valuesAfterInvalidation).toEqual({
139+
key1: "value1",
140+
key2: "value2",
141+
key3: undefined,
142+
});
127143
});
128144

129145
it("should delete multiple keys using mdel", () => {
@@ -197,27 +213,43 @@ describe("Cache", () => {
197213
expect(stats.misses).toBe(0);
198214
});
199215

200-
it("should return the cache instance for custom operations", () => {
201-
const cacheInstance = cache.getCacheInstance();
202-
expect(cacheInstance).toBeInstanceOf(NodeCache);
203-
204-
// test a hook
205-
cacheInstance.on("set", (key, value) => {
206-
expect(key).toBe("keyHook");
207-
expect(value).toStrictEqual({
208-
value: "valueHook",
209-
tags: [],
210-
});
211-
});
212-
213-
cache.set("keyHook", "valueHook", 60);
214-
});
215-
216216
it("should return the tag map", () => {
217217
cache.set("key1", "value1", 60, ["tag1"]);
218218
cache.set("key2", "value2", 60, ["tag2"]);
219219
const tagMap = cache.getTagMap();
220220
expect(tagMap.get("tag1")).toEqual(new Set(["key1"]));
221221
expect(tagMap.get("tag2")).toEqual(new Set(["key2"]));
222222
});
223+
224+
it("should handle number keys correctly", () => {
225+
cache.set(1, "value1", 60);
226+
cache.set(2, "value2", 60, ["tag1"]);
227+
cache.set(3, "value3", 60, ["tag2"]);
228+
cache.set(4, "value4", 60, ["tag1", "tag2"]);
229+
cache.set(5, "value5", 60, ["tagA"]);
230+
cache.set(6, "value6", 60, ["tagB"]);
231+
232+
expect(cache.get(1)).toBe("value1");
233+
expect(cache.get(2)).toBe("value2");
234+
expect(cache.get(3)).toBe("value3");
235+
expect(cache.get(4)).toBe("value4");
236+
237+
cache.del(1);
238+
expect(cache.get(1)).toBeUndefined();
239+
240+
cache.invalidateTag("tag1");
241+
expect(cache.get(2)).toBeUndefined();
242+
expect(cache.get(3)).toBe("value3");
243+
expect(cache.get(4)).toBeUndefined();
244+
245+
cache.invalidateTagsIntersection(["tag1", "tag2"]);
246+
247+
expect(cache.get(3)).toBeUndefined();
248+
expect(cache.get(5)).toBe("value5");
249+
expect(cache.get(6)).toBe("value6");
250+
251+
cache.invalidateTagsUnion(["tagA", "tagB"]);
252+
expect(cache.get(5)).toBeUndefined();
253+
expect(cache.get(6)).toBeUndefined();
254+
});
223255
});

0 commit comments

Comments
 (0)