Skip to content

Commit 92370c4

Browse files
committed
feat: Upgrade event callbacks into EventEmitter (#16)
1 parent aa369e9 commit 92370c4

File tree

7 files changed

+687
-251
lines changed

7 files changed

+687
-251
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@ jobs:
1919
node-version: "18"
2020
registry-url: "https://registry.npmjs.org"
2121

22+
- name: Install dependencies
23+
run: npm ci
24+
2225
- name: Build
2326
run: npm run build

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"cSpell.words": ["refetchable", "refetched"]
2+
"cSpell.words": ["refetchable", "refetched", "refetches"]
33
}

README.md

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22
[![license](https://img.shields.io/github/license/helloscoopa/run-cache)](https://github.com/helloscoopa/run-cache?tab=MIT-1-ov-file)
33
[![ci-build](https://img.shields.io/github/actions/workflow/status/helloscoopa/run-cache/build.yml?label=build)](https://github.com/helloscoopa/run-cache/actions/workflows/build.yml)
44
[![ci-tests](https://img.shields.io/github/actions/workflow/status/helloscoopa/run-cache/tests.yml?label=tests)](https://github.com/helloscoopa/run-cache/actions/workflows/tests.yml)
5-
[![commits-since](https://img.shields.io/github/commits-since/helloscoopa/run-cache/latest/main?color=yellow)](https://github.com/helloscoopa/run-cache/releases/latest)
65

76
# Run~time~Cache
87

9-
RunCache is a dependency nil, light-weight in-memory caching library for JavaScript and TypeScript that allows you to cache `string` values with optional time-to-live (TTL) settings. It also supports caching values generated from asynchronous functions and provides methods to refetch them on demand.
8+
RunCache is a dependency-free, lightweight runtime caching library for JavaScript and TypeScript that allows you to cache `string` values with optional time-to-live (TTL) settings. It also supports caching values generated from sync/async functions and provide methods to refetch them on expiry or on demand; with a set of events to keep track of the state of the cache.
109

1110
## Features
1211

13-
- **In-memory caching** with optional TTL (time-to-live).
14-
- **Asynchronous source functions** for fetching and caching dynamic data.
15-
- **Refetch functionality** to update cache values using stored source functions.
16-
- **Events** to get know when cache expires or being refetched.
17-
- **Easy interface** for managing cached data: set, get, delete and check existence.
12+
- **Dependency-free:** Does not consume any external dependencies.
13+
- **In-memory caching:** A runtime cache that gives you quick access.
14+
- **Sync/async source functions:** Fetch dynamic data from user-defined functions.
15+
- **Events:** Get to know when cache expires, refetched or refetch fails.
16+
- **Intuitive SDK:** Clean interface to access data.
1817

1918
## Installation
2019

@@ -32,7 +31,7 @@ npm install run-cache
3231
import { RunCache } from "run-cache";
3332
```
3433

35-
#### Set cache:
34+
#### Set cache
3635

3736
```ts
3837
// Set a cache value
@@ -68,38 +67,52 @@ await RunCache.set({
6867
});
6968

7069
/*
71-
Use a callback function to get know when your cache expires
70+
Use a callback function to get to know when your cache expires
7271
or when its being refetched. The expiry is triggered only
7372
on demand, not automatically.
7473
*/
7574
import { EventParam } from "run-cache";
7675

76+
// Event of all expiries
77+
RunCache.onExpiry((cache: EventParam) => {
78+
console.log(`Cache of key '${cache.key}' has been expired`);
79+
})
80+
81+
// Event of a specific key expiry
82+
RunCache.onKeyExpiry('Key', (cache: EventParam) => {
83+
console.log(`Cache of key '${cache.key}' has been expired`);
84+
})
85+
7786
await RunCache.set({
7887
key: "Key",
79-
ttl: 10000,
80-
onExpiry: async (cache: EventParam) => {
81-
console.log(`Cache of key '${cache.key}' has been expired`);
82-
}
88+
ttl: 10000
89+
})
90+
91+
// Event of all refetches
92+
RunCache.onRefetch((cache: EventParam) => {
93+
console.log(`Cache of key '${cache.key}' has been refetched`);
94+
})
95+
96+
// Event of a specific key refetch
97+
RunCache.onKeyRefetch('Key', (cache: EventParam) => {
98+
console.log(`Cache of key '${cache.key}' has been refetched`);
8399
})
84100

85101
await RunCache.set({
86102
key: "Key",
87103
ttl: 10000,
88104
sourceFn: () => { return Promise.resolve("Value") }
89-
onRefetch: async (cache: EventParam) => {
90-
console.log(`Cache of key '${cache.key}' has been refetched`);
91-
}
92105
})
93106
```
94107

95-
#### Refetch cache:
108+
#### Refetch cache
96109

97110
```ts
98111
// Refetch the cache value (Only works if the key is set with a sourceFn)
99112
await RunCache.refetch("Key");
100113
```
101114

102-
#### Get cache:
115+
#### Get cache
103116

104117
```ts
105118
/*
@@ -109,19 +122,37 @@ await RunCache.refetch("Key");
109122
const value = await RunCache.get("Key");
110123
```
111124

112-
#### Delete cache:
125+
#### Delete cache
113126

114127
```ts
115128
// Delete a specific cache key
116129
RunCache.delete("Key");
117130

118131
// Delete all cache keys
119-
RunCache.deleteAll();
132+
RunCache.flush();
120133
```
121134

122-
#### Check existence of a specific cache:
135+
#### Check the existence of a specific cache
123136

124137
```ts
125138
// Returns a boolean, expired cache returns `false` even if they're refetchable
126139
const hasCache = RunCache.has("Key");
127140
```
141+
142+
#### Clear event listeners
143+
144+
```ts
145+
// Clear all listeners
146+
RunCache.clearEventListeners();
147+
148+
// Clear specific event listeners
149+
RunCache.clearEventListeners({
150+
event: "expiry",
151+
});
152+
153+
// Clear specific event key listeners
154+
RunCache.clearEventListeners({
155+
event: "expiry",
156+
key: "Key",
157+
});
158+
```

package-lock.json

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

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "run-cache",
3-
"version": "1.3.2",
4-
"description": "RunCache is a dependency nil, light-weight in-memory caching library for JavaScript and TypeScript that allows you to cache `string` values with optional time-to-live (TTL) settings. It also supports caching values generated from asynchronous functions and provides methods to refetch them on demand.",
3+
"version": "1.4.0",
4+
"description": "RunCache is a dependency-free, light-weight in-memory caching library for JavaScript and TypeScript that allows you to cache `string` values with optional time-to-live (TTL) settings. It also supports caching values generated from asynchronous functions and provides methods to refetch them on demand.",
55
"main": "dist/run-cache.js",
66
"types": "dist/run-cache.d.ts",
77
"scripts": {
@@ -29,8 +29,10 @@
2929
},
3030
"devDependencies": {
3131
"@types/jest": "^29.5.12",
32+
"@types/uuid": "^10.0.0",
3233
"jest": "^29.7.0",
3334
"ts-jest": "^29.2.5",
34-
"tsx": "^4.19.1"
35+
"tsx": "^4.19.1",
36+
"uuid": "^10.0.0"
3537
}
3638
}

0 commit comments

Comments
 (0)