@darco2903/expiry-cache 2.0.2 → 3.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 +133 -25
- package/dist/ExpiryCache.d.ts +9 -22
- package/dist/ExpiryCache.js +8 -29
- package/dist/ExpiryCacheAsync.d.ts +11 -24
- package/dist/ExpiryCacheAsync.js +10 -34
- package/dist/ExpiryCacheSafe.d.ts +18 -0
- package/dist/ExpiryCacheSafe.js +31 -0
- package/dist/ExpiryCacheSafeAsync.d.ts +20 -0
- package/dist/ExpiryCacheSafeAsync.js +40 -0
- package/dist/ReturnOptions.d.ts +15 -0
- package/dist/ReturnOptions.js +24 -0
- package/dist/base/ExpiryCacheAsyncBase.d.ts +13 -0
- package/dist/base/ExpiryCacheAsyncBase.js +11 -0
- package/dist/base/ExpiryCacheBase.d.ts +112 -0
- package/dist/base/ExpiryCacheBase.js +177 -0
- package/dist/base/ExpiryCacheSyncBase.d.ts +5 -0
- package/dist/base/ExpiryCacheSyncBase.js +3 -0
- package/dist/base/index.d.ts +2 -0
- package/dist/base/index.js +2 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/interface/ExpiryCacheAsync.d.ts +6 -0
- package/dist/interface/ExpiryCacheAsync.js +2 -0
- package/dist/interface/ExpiryCacheSafe.d.ts +6 -0
- package/dist/interface/ExpiryCacheSafe.js +2 -0
- package/dist/interface/ExpiryCacheSync.d.ts +6 -0
- package/dist/interface/ExpiryCacheSync.js +2 -0
- package/dist/interface/ExpiryCacheUnsafe.d.ts +6 -0
- package/dist/interface/ExpiryCacheUnsafe.js +2 -0
- package/dist/interface/index.d.ts +4 -0
- package/dist/types/RefreshFunction.d.ts +11 -0
- package/dist/types/RefreshFunction.js +1 -0
- package/dist/types/ReturnType.d.ts +10 -0
- package/dist/types/ReturnType.js +1 -0
- package/dist/types/events.d.ts +8 -0
- package/dist/types/events.js +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +1 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.js +17 -0
- package/package.json +8 -5
- package/dist/ExpiryCacheBase.d.ts +0 -95
- package/dist/ExpiryCacheBase.js +0 -115
- package/dist/types.d.ts +0 -2
- /package/dist/{types.js → interface/index.js} +0 -0
package/dist/ExpiryCacheBase.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { Millisecond, Minute } from "@darco2903/secondthought";
|
|
2
|
-
export class ExpiryCacheBase {
|
|
3
|
-
/**
|
|
4
|
-
* Gets the expiration timestamp in milliseconds.
|
|
5
|
-
*/
|
|
6
|
-
get expiresAt() {
|
|
7
|
-
return this._expiresAt.time;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Checks if the cache is set to expire.
|
|
11
|
-
*/
|
|
12
|
-
get doesExpire() {
|
|
13
|
-
return this._expiresAt.time !== 0;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Checks if the cache is expired.
|
|
17
|
-
*/
|
|
18
|
-
get isExpired() {
|
|
19
|
-
return this.doesExpire && Millisecond.now().greaterThanOrEqual(this._expiresAt);
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Gets the time to live (TTL) in milliseconds.
|
|
23
|
-
*/
|
|
24
|
-
get timeToLive() {
|
|
25
|
-
if (this.doesExpire) {
|
|
26
|
-
return Math.max(0, this._expiresAt.clone().sub(Millisecond.now()).time);
|
|
27
|
-
}
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Converts a number or Time object to milliseconds.
|
|
32
|
-
*/
|
|
33
|
-
argToMs(arg) {
|
|
34
|
-
return typeof arg === "number" ? new Millisecond(arg) : arg.toMillisecond();
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Creates an instance of ExpiryCache.
|
|
38
|
-
* @param data The initial data to be cached.
|
|
39
|
-
* @param callback The function to refresh the cached data.
|
|
40
|
-
* @param expirationTime The time in milliseconds after which the cache expires. Defaults to 60_000 (1 minute). If set to 0, the cache will never expire.
|
|
41
|
-
*/
|
|
42
|
-
constructor(data, callback, expirationTime = new Minute(1)) {
|
|
43
|
-
const expTime = this.argToMs(expirationTime);
|
|
44
|
-
this.data = data;
|
|
45
|
-
this._expiresAt = expTime.time === 0 ? expTime : Millisecond.now().add(expTime);
|
|
46
|
-
this.expirationTime = expTime.time;
|
|
47
|
-
this.callback = callback;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Expires the cache immediately.
|
|
51
|
-
*/
|
|
52
|
-
expire() {
|
|
53
|
-
this._expiresAt = new Millisecond(-1);
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Sets the cache to never expire.
|
|
57
|
-
*/
|
|
58
|
-
neverExpire() {
|
|
59
|
-
this._expiresAt = new Millisecond(0);
|
|
60
|
-
}
|
|
61
|
-
expIn(ms) {
|
|
62
|
-
return ms.time === 0 ? ms : Millisecond.now().add(ms);
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Sets the expiration timestamp based on the current time plus the given milliseconds.
|
|
66
|
-
* @param ms The time in milliseconds after which the cache should expire. If set to 0, the cache will never expire.
|
|
67
|
-
*/
|
|
68
|
-
setExpiresIn(ms) {
|
|
69
|
-
const t = this.argToMs(ms);
|
|
70
|
-
this._expiresAt = this.expIn(t);
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Sets the expiration timestamp to a specific time.
|
|
74
|
-
* @param timestamp The expiration timestamp in milliseconds.
|
|
75
|
-
*/
|
|
76
|
-
setExpiresAt(timestamp) {
|
|
77
|
-
this._expiresAt = this.argToMs(timestamp);
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Sets the cached data and resets the expiration timestamp based on the expiration time.
|
|
81
|
-
*/
|
|
82
|
-
setData(data) {
|
|
83
|
-
this.setDataExpiresIn(data, new Millisecond(this.expirationTime));
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Sets the cached data and sets a new expiration timestamp.
|
|
87
|
-
* @param expiresAt The new expiration timestamp in milliseconds.
|
|
88
|
-
*/
|
|
89
|
-
setDataExpiresAt(data, expiresAt) {
|
|
90
|
-
this.data = data;
|
|
91
|
-
this._expiresAt = expiresAt;
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Sets the cached data and sets a new expiration time.
|
|
95
|
-
* @param expirationTime The new expiration time in milliseconds.
|
|
96
|
-
*/
|
|
97
|
-
setDataExpiresIn(data, expirationTime) {
|
|
98
|
-
this.setDataExpiresAt(data, this.expIn(expirationTime));
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Gets the raw cached data without checking expiration.
|
|
102
|
-
*/
|
|
103
|
-
getRawData() {
|
|
104
|
-
return this.data;
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Gets the cached data if not expired, otherwise returns null.
|
|
108
|
-
*/
|
|
109
|
-
getData() {
|
|
110
|
-
if (this.isExpired) {
|
|
111
|
-
return null;
|
|
112
|
-
}
|
|
113
|
-
return this.data;
|
|
114
|
-
}
|
|
115
|
-
}
|
package/dist/types.d.ts
DELETED
|
File without changes
|