|
1 | | -# Node Cache Plus |
| 1 | +# node-cache-plus |
2 | 2 |
|
3 | 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. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +To install `node-cache-plus`, use your preferred package manager: |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install node-cache-plus |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +`node-cache-plus` is a drop-in replacement for `node-cache`. It provides the same API as `node-cache` with additional features. Refer to the `node-cache` [documentation](https://github.com/node-cache/node-cache#options) for more information on the basic usage and initialisation options. |
| 16 | + |
| 17 | +### Basic Usage |
| 18 | + |
| 19 | +```typescript |
| 20 | +import { Cache } from "node-cache-plus"; |
| 21 | + |
| 22 | +const cache = new Cache(); |
| 23 | + |
| 24 | +// Set a value with a TTL of 60 seconds |
| 25 | +cache.set("key", "value", 60); |
| 26 | + |
| 27 | +// Get the value |
| 28 | +const value = cache.get("key"); |
| 29 | +console.log(value); // Output: "value" |
| 30 | + |
| 31 | +// Delete the value |
| 32 | +cache.del("key"); |
| 33 | +``` |
| 34 | + |
| 35 | +### Tag-based Invalidation |
| 36 | + |
| 37 | +```typescript |
| 38 | +// Set a value with tags |
| 39 | +cache.set("key1", "value1", 60, ["tag1"]); |
| 40 | +cache.set("key2", "value2", 60, ["tag2"]); |
| 41 | +cache.set("key3", "value3", 60, ["tag1", "tag2"]); |
| 42 | + |
| 43 | +// Invalidate all keys with a specific tag |
| 44 | +cache.invalidateTag("tag1"); // i.e. Invalidates keys "key1" and "key3" |
| 45 | + |
| 46 | +// Invalidate keys that have all specified tags |
| 47 | +cache.invalidateTagsIntersection(["tag1", "tag2"]); // i.e. Invalidates key "key3" |
| 48 | + |
| 49 | +// Invalidate keys that have at least one of the specified tags |
| 50 | +cache.invalidateTagsUnion(["tag1", "tag2"]); // i.e. Invalidates keys "key1" and "key2" |
| 51 | +``` |
| 52 | + |
| 53 | +### `withCache` Helper Function |
| 54 | + |
| 55 | +```typescript |
| 56 | +import { withCache } from "node-cache-plus"; |
| 57 | + |
| 58 | +async function fetchData(param: string): Promise<string> { |
| 59 | + return new Promise((resolve) => { |
| 60 | + setTimeout(() => { |
| 61 | + resolve(`data_${param}`); |
| 62 | + }, 500); |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +const cachedFetchData = withCache(fetchData, { ttl: 600, tags: ["data"] }); |
| 67 | + |
| 68 | +const data = await cachedFetchData("param1"); |
| 69 | +console.log(data); // Output: "data_param1" |
| 70 | +``` |
| 71 | + |
| 72 | +_Note: This function uses the default cache, but you can pass a custom cache instance as an option. See details in the [Configuring Default Cache](#configuring-default-cache) section below._ |
| 73 | + |
| 74 | +### `cachedCall` Helper Function |
| 75 | + |
| 76 | +```typescript |
| 77 | +import { cachedCall } from "node-cache-plus"; |
| 78 | + |
| 79 | +async function fetchData(param: string): Promise<string> { |
| 80 | + return new Promise((resolve) => { |
| 81 | + setTimeout(() => { |
| 82 | + resolve(`data_${param}`); |
| 83 | + }, 500); |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +const data = await cachedCall( |
| 88 | + fetchData, |
| 89 | + { ttl: 600, tags: ["data"] }, |
| 90 | + "param1" |
| 91 | +); |
| 92 | +console.log(data); // Output: "data_param1" |
| 93 | +``` |
| 94 | + |
| 95 | +_Note: This function uses the default cache, but you can pass a custom cache instance as an option. See details in the [Configuring Default Cache](#configuring-default-cache) section below._ |
| 96 | + |
| 97 | +### Configuring Default Cache |
| 98 | + |
| 99 | +The helper functions `withCache` and `cachedCall` use a default cache instance. You can configure the default cache instance by using the`configureDefaultCache` function. |
| 100 | + |
| 101 | +```typescript |
| 102 | +import { configureDefaultCache, getDefaultCache } from "node-cache-plus"; |
| 103 | + |
| 104 | +// Configure the default cache with custom options |
| 105 | +configureDefaultCache({ stdTTL: 100, checkperiod: 120 }); |
| 106 | + |
| 107 | +// Get the default cache instance |
| 108 | +const defaultCache = getDefaultCache(); |
| 109 | + |
| 110 | +// Use the default cache instance |
| 111 | +defaultCache.set("key", "value", 60); |
| 112 | +const value = defaultCache.get("key"); |
| 113 | +console.log(value); // Output: "value" |
| 114 | +``` |
0 commit comments