@darco2903/expiry-cache 2.0.0 → 2.0.2
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/LICENSE +21 -0
- package/README.md +41 -7
- package/dist/ExpiryCacheBase.js +1 -1
- package/package.json +2 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Darco2903
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -4,9 +4,22 @@
|
|
|
4
4
|
|
|
5
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
6
|
|
|
7
|
+
> Note: This package utilizes the [**@darco2903/secondthought**](https://www.npmjs.com/package/@darco2903/secondthought) package for time-related operations, providing enhanced capabilities for handling and manipulating time-related data. While its use is not mandatory, it is recommended for optimal safety and reliability when working with time values.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Type Safety**: Strongly typed cache entries and fetcher functions.
|
|
12
|
+
- **Automatic Expiry**: Cache entries automatically expire after a specified duration.
|
|
13
|
+
- **Manual Expiry**: Manually expire cache entries when needed.
|
|
14
|
+
- **Synchronous and Asynchronous Fetching**: Support for both synchronous and asynchronous data fetching.
|
|
15
|
+
- **Raw Data Access**: Access the raw cached data even if it has expired.
|
|
16
|
+
- **Parameterized Fetching**: Support for fetcher functions that require parameters.
|
|
17
|
+
|
|
7
18
|
## Installation
|
|
8
19
|
|
|
9
|
-
|
|
20
|
+
```bash
|
|
21
|
+
npm install @darco2903/expiry-cache
|
|
22
|
+
```
|
|
10
23
|
|
|
11
24
|
## Example Usage
|
|
12
25
|
|
|
@@ -15,24 +28,36 @@ You can find the package here: [**@darco2903/expiry-cache**](https://github.com/
|
|
|
15
28
|
```ts
|
|
16
29
|
import { ExpiryCache } from "@darco2903/expiry-cache";
|
|
17
30
|
|
|
18
|
-
const cache = new ExpiryCache("initial", () => "
|
|
31
|
+
const cache = new ExpiryCache("initial", () => "refreshed", 1000); // inferring to ExpiryCache<string, () => string>
|
|
19
32
|
console.log(cache.getData()); // Outputs: initial
|
|
20
33
|
|
|
21
34
|
await new Promise((resolve) => setTimeout(resolve, 1100)); // wait for cache to expire
|
|
22
35
|
console.log(cache.isExpired); // Outputs: true
|
|
23
36
|
console.log(cache.getData()); // Outputs: null
|
|
37
|
+
console.log(cache.getRawData()); // Outputs: initial (returns the raw data even if expired)
|
|
24
38
|
|
|
25
39
|
cache.refresh();
|
|
26
|
-
console.log(cache.getData()); // Outputs:
|
|
40
|
+
console.log(cache.getData()); // Outputs: refreshed
|
|
41
|
+
|
|
42
|
+
await new Promise((resolve) => setTimeout(resolve, 1100)); // wait for cache to expire
|
|
43
|
+
console.log(cache.isExpired); // Outputs: true
|
|
44
|
+
console.log(cache.getDataOrRefresh()); // Outputs: refreshed (refreshes the cache and returns the new value)
|
|
45
|
+
|
|
46
|
+
console.log(cache.expirationTime); // Outputs: 1000
|
|
47
|
+
console.log(cache.expiresAt); // Outputs: current timestamp + 1000 milliseconds
|
|
48
|
+
console.log(cache.timeToLive); // Outputs: time remaining until expiration in milliseconds
|
|
49
|
+
|
|
50
|
+
console.log(cache.expire()); // Manually expire the cache
|
|
51
|
+
console.log(cache.isExpired); // Outputs: true
|
|
27
52
|
```
|
|
28
53
|
|
|
29
54
|
### With Async Fetcher
|
|
30
55
|
|
|
31
56
|
```ts
|
|
32
|
-
import {
|
|
57
|
+
import { ExpiryCacheAsync } from "@darco2903/expiry-cache";
|
|
33
58
|
|
|
34
59
|
async function getApiData(): Promise<string> {
|
|
35
|
-
//
|
|
60
|
+
// Simulate an API call
|
|
36
61
|
return new Promise((resolve) => {
|
|
37
62
|
setTimeout(() => {
|
|
38
63
|
resolve("fetched data from API");
|
|
@@ -40,7 +65,7 @@ async function getApiData(): Promise<string> {
|
|
|
40
65
|
});
|
|
41
66
|
}
|
|
42
67
|
|
|
43
|
-
const cache = new
|
|
68
|
+
const cache = new ExpiryCacheAsync("initial data", getApiData, 5000);
|
|
44
69
|
|
|
45
70
|
await cache.refresh();
|
|
46
71
|
console.log(cache.getData()); // Outputs: fetched data from API
|
|
@@ -62,9 +87,18 @@ console.log(cache.getData()); // Outputs: 12
|
|
|
62
87
|
```ts
|
|
63
88
|
import { ExpiryCache } from "@darco2903/expiry-cache";
|
|
64
89
|
|
|
65
|
-
const cache = new ExpiryCache(0, () => 0);
|
|
90
|
+
const cache = new ExpiryCache(0, () => 0); // Cache with no expiry by default
|
|
66
91
|
|
|
67
92
|
cache.refresh();
|
|
68
93
|
cache.refreshExpiresAt(Date.now());
|
|
69
94
|
cache.refreshExpiresIn(1000);
|
|
70
95
|
```
|
|
96
|
+
|
|
97
|
+
### SecondThought Integration
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import { ExpiryCache } from "@darco2903/expiry-cache";
|
|
101
|
+
import { Minute } from "@darco2903/secondthought";
|
|
102
|
+
|
|
103
|
+
const cache = new ExpiryCache("data", () => "data", new Minute(5)); // Cache expires in 5 minutes
|
|
104
|
+
```
|
package/dist/ExpiryCacheBase.js
CHANGED
|
@@ -23,7 +23,7 @@ export class ExpiryCacheBase {
|
|
|
23
23
|
*/
|
|
24
24
|
get timeToLive() {
|
|
25
25
|
if (this.doesExpire) {
|
|
26
|
-
return Math.max(0, this._expiresAt.clone().
|
|
26
|
+
return Math.max(0, this._expiresAt.clone().sub(Millisecond.now()).time);
|
|
27
27
|
}
|
|
28
28
|
return null;
|
|
29
29
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darco2903/expiry-cache",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"author": "Darco2903",
|
|
5
5
|
"description": "",
|
|
6
6
|
"keywords": [],
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"access": "public"
|
|
16
16
|
},
|
|
17
17
|
"peerDependencies": {
|
|
18
|
-
"@darco2903/secondthought": "^1.
|
|
18
|
+
"@darco2903/secondthought": "^1.3.0"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@darco2903/web-common": "^0.7.0",
|