@darco2903/expiry-cache 2.0.0-beta.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -70
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
# Expiry Cache
|
|
2
|
-
|
|
3
|
-
## Description
|
|
4
|
-
|
|
5
|
-
A simple in-memory cache with expiry functionality. It allows you to store data that automatically expires after a specified duration, with support for synchronous and asynchronous data fetching.
|
|
6
|
-
|
|
7
|
-
## Installation
|
|
8
|
-
|
|
9
|
-
You can find the package here: [**@darco2903/expiry-cache**](https://github.com/users/Darco2903/packages/npm/package/expiry-cache)
|
|
10
|
-
|
|
11
|
-
## Example Usage
|
|
12
|
-
|
|
13
|
-
### Basic Example
|
|
14
|
-
|
|
15
|
-
```ts
|
|
16
|
-
import { ExpiryCache } from "@darco2903/expiry-cache";
|
|
17
|
-
|
|
18
|
-
const cache = new ExpiryCache("initial", () => "string", 1000); // infering to ExpiryCache<string, () => string>
|
|
19
|
-
console.log(cache.getData()); // Outputs: initial
|
|
20
|
-
|
|
21
|
-
await new Promise((resolve) => setTimeout(resolve, 1100)); // wait for cache to expire
|
|
22
|
-
console.log(cache.isExpired); // Outputs: true
|
|
23
|
-
console.log(cache.getData()); // Outputs: null
|
|
24
|
-
|
|
25
|
-
cache.refresh();
|
|
26
|
-
console.log(cache.getData()); // Outputs: string
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
### With Async Fetcher
|
|
30
|
-
|
|
31
|
-
```ts
|
|
32
|
-
import { ExpiryCache } from "@darco2903/expiry-cache";
|
|
33
|
-
|
|
34
|
-
async function getApiData(): Promise<string> {
|
|
35
|
-
// Simulating an API call
|
|
36
|
-
return new Promise((resolve) => {
|
|
37
|
-
setTimeout(() => {
|
|
38
|
-
resolve("fetched data from API");
|
|
39
|
-
}, 500);
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const cache = new ExpiryCache("initial data", getApiData, 5000);
|
|
44
|
-
|
|
45
|
-
await cache.refresh();
|
|
46
|
-
console.log(cache.getData()); // Outputs: fetched data from API
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
### With Parameters
|
|
50
|
-
|
|
51
|
-
```ts
|
|
52
|
-
import { ExpiryCache } from "@darco2903/expiry-cache";
|
|
53
|
-
|
|
54
|
-
const cache = new ExpiryCache(10, (a: number, b: number) => a + b, 200);
|
|
55
|
-
|
|
56
|
-
cache.refresh(5, 7); // typed: refresh(a: number, b: number)
|
|
57
|
-
console.log(cache.getData()); // Outputs: 12
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
### Setting Expiry Manually
|
|
61
|
-
|
|
62
|
-
```ts
|
|
63
|
-
import { ExpiryCache } from "@darco2903/expiry-cache";
|
|
64
|
-
|
|
65
|
-
const cache = new ExpiryCache(0, () => 0);
|
|
66
|
-
|
|
67
|
-
cache.refresh();
|
|
68
|
-
cache.refreshExpiresAt(Date.now());
|
|
69
|
-
cache.refreshExpiresIn(1000);
|
|
70
|
-
```
|
|
1
|
+
# Expiry Cache
|
|
2
|
+
|
|
3
|
+
## Description
|
|
4
|
+
|
|
5
|
+
A simple in-memory cache with expiry functionality. It allows you to store data that automatically expires after a specified duration, with support for synchronous and asynchronous data fetching.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
You can find the package here: [**@darco2903/expiry-cache**](https://github.com/users/Darco2903/packages/npm/package/expiry-cache)
|
|
10
|
+
|
|
11
|
+
## Example Usage
|
|
12
|
+
|
|
13
|
+
### Basic Example
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { ExpiryCache } from "@darco2903/expiry-cache";
|
|
17
|
+
|
|
18
|
+
const cache = new ExpiryCache("initial", () => "string", 1000); // infering to ExpiryCache<string, () => string>
|
|
19
|
+
console.log(cache.getData()); // Outputs: initial
|
|
20
|
+
|
|
21
|
+
await new Promise((resolve) => setTimeout(resolve, 1100)); // wait for cache to expire
|
|
22
|
+
console.log(cache.isExpired); // Outputs: true
|
|
23
|
+
console.log(cache.getData()); // Outputs: null
|
|
24
|
+
|
|
25
|
+
cache.refresh();
|
|
26
|
+
console.log(cache.getData()); // Outputs: string
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### With Async Fetcher
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { ExpiryCache } from "@darco2903/expiry-cache";
|
|
33
|
+
|
|
34
|
+
async function getApiData(): Promise<string> {
|
|
35
|
+
// Simulating an API call
|
|
36
|
+
return new Promise((resolve) => {
|
|
37
|
+
setTimeout(() => {
|
|
38
|
+
resolve("fetched data from API");
|
|
39
|
+
}, 500);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const cache = new ExpiryCache("initial data", getApiData, 5000);
|
|
44
|
+
|
|
45
|
+
await cache.refresh();
|
|
46
|
+
console.log(cache.getData()); // Outputs: fetched data from API
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### With Parameters
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { ExpiryCache } from "@darco2903/expiry-cache";
|
|
53
|
+
|
|
54
|
+
const cache = new ExpiryCache(10, (a: number, b: number) => a + b, 200);
|
|
55
|
+
|
|
56
|
+
cache.refresh(5, 7); // typed: refresh(a: number, b: number)
|
|
57
|
+
console.log(cache.getData()); // Outputs: 12
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Setting Expiry Manually
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { ExpiryCache } from "@darco2903/expiry-cache";
|
|
64
|
+
|
|
65
|
+
const cache = new ExpiryCache(0, () => 0);
|
|
66
|
+
|
|
67
|
+
cache.refresh();
|
|
68
|
+
cache.refreshExpiresAt(Date.now());
|
|
69
|
+
cache.refreshExpiresIn(1000);
|
|
70
|
+
```
|