@darco2903/expiry-cache 2.0.2 → 2.1.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/dist/ExpiryCacheBase.d.ts +22 -3
- package/dist/ExpiryCacheBase.js +31 -4
- package/package.json +1 -1
|
@@ -1,15 +1,30 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Time, Millisecond } from "@darco2903/secondthought";
|
|
2
2
|
import type { RefreshFunction, RefreshFunctionAsync } from "./types.js";
|
|
3
3
|
export declare abstract class ExpiryCacheBase<T, U extends RefreshFunction<T> | RefreshFunctionAsync<T>> {
|
|
4
|
+
/** The cached data. */
|
|
4
5
|
protected data: T;
|
|
6
|
+
/** The function to refresh the cached data. */
|
|
5
7
|
protected callback: U;
|
|
6
|
-
|
|
8
|
+
/** The time in milliseconds after which the cache expires. 0 means never expires. */
|
|
9
|
+
protected _expirationTime: Millisecond;
|
|
7
10
|
/** The expiration timestamp in milliseconds. 0 means never expires, -1 means already expired or no data. */
|
|
8
11
|
protected _expiresAt: Millisecond;
|
|
12
|
+
/**
|
|
13
|
+
* Gets the expiration time in milliseconds. This is the duration after which the cache expires, not the absolute expiration timestamp.
|
|
14
|
+
*/
|
|
15
|
+
get expirationTime(): number;
|
|
16
|
+
/**
|
|
17
|
+
* Gets the expiration time as a Millisecond object. This is the duration after which the cache expires, not the absolute expiration timestamp.
|
|
18
|
+
*/
|
|
19
|
+
get expirationTimeAsTime(): Millisecond;
|
|
9
20
|
/**
|
|
10
21
|
* Gets the expiration timestamp in milliseconds.
|
|
11
22
|
*/
|
|
12
23
|
get expiresAt(): number;
|
|
24
|
+
/**
|
|
25
|
+
* Gets the expiration timestamp as a Millisecond object.
|
|
26
|
+
*/
|
|
27
|
+
get expiresAtAsTime(): Millisecond;
|
|
13
28
|
/**
|
|
14
29
|
* Checks if the cache is set to expire.
|
|
15
30
|
*/
|
|
@@ -19,9 +34,13 @@ export declare abstract class ExpiryCacheBase<T, U extends RefreshFunction<T> |
|
|
|
19
34
|
*/
|
|
20
35
|
get isExpired(): boolean;
|
|
21
36
|
/**
|
|
22
|
-
* Gets the time to live (TTL) in milliseconds.
|
|
37
|
+
* Gets the time to live (TTL) in milliseconds. Returns null if the cache does not expire.
|
|
23
38
|
*/
|
|
24
39
|
get timeToLive(): number | null;
|
|
40
|
+
/**
|
|
41
|
+
* Gets the time to live (TTL) as a Millisecond object. Returns null if the cache does not expire.
|
|
42
|
+
*/
|
|
43
|
+
get timeToLiveAsTime(): Millisecond | null;
|
|
25
44
|
/**
|
|
26
45
|
* Converts a number or Time object to milliseconds.
|
|
27
46
|
*/
|
package/dist/ExpiryCacheBase.js
CHANGED
|
@@ -1,11 +1,29 @@
|
|
|
1
|
-
import { Millisecond, Minute } from "@darco2903/secondthought";
|
|
1
|
+
import { Time, Millisecond, Minute } from "@darco2903/secondthought";
|
|
2
2
|
export class ExpiryCacheBase {
|
|
3
|
+
/**
|
|
4
|
+
* Gets the expiration time in milliseconds. This is the duration after which the cache expires, not the absolute expiration timestamp.
|
|
5
|
+
*/
|
|
6
|
+
get expirationTime() {
|
|
7
|
+
return this._expirationTime.time;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Gets the expiration time as a Millisecond object. This is the duration after which the cache expires, not the absolute expiration timestamp.
|
|
11
|
+
*/
|
|
12
|
+
get expirationTimeAsTime() {
|
|
13
|
+
return this._expirationTime;
|
|
14
|
+
}
|
|
3
15
|
/**
|
|
4
16
|
* Gets the expiration timestamp in milliseconds.
|
|
5
17
|
*/
|
|
6
18
|
get expiresAt() {
|
|
7
19
|
return this._expiresAt.time;
|
|
8
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Gets the expiration timestamp as a Millisecond object.
|
|
23
|
+
*/
|
|
24
|
+
get expiresAtAsTime() {
|
|
25
|
+
return this._expiresAt;
|
|
26
|
+
}
|
|
9
27
|
/**
|
|
10
28
|
* Checks if the cache is set to expire.
|
|
11
29
|
*/
|
|
@@ -19,7 +37,7 @@ export class ExpiryCacheBase {
|
|
|
19
37
|
return this.doesExpire && Millisecond.now().greaterThanOrEqual(this._expiresAt);
|
|
20
38
|
}
|
|
21
39
|
/**
|
|
22
|
-
* Gets the time to live (TTL) in milliseconds.
|
|
40
|
+
* Gets the time to live (TTL) in milliseconds. Returns null if the cache does not expire.
|
|
23
41
|
*/
|
|
24
42
|
get timeToLive() {
|
|
25
43
|
if (this.doesExpire) {
|
|
@@ -27,6 +45,15 @@ export class ExpiryCacheBase {
|
|
|
27
45
|
}
|
|
28
46
|
return null;
|
|
29
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Gets the time to live (TTL) as a Millisecond object. Returns null if the cache does not expire.
|
|
50
|
+
*/
|
|
51
|
+
get timeToLiveAsTime() {
|
|
52
|
+
if (this.doesExpire) {
|
|
53
|
+
return Time.max(new Millisecond(0), this._expiresAt.clone().sub(Millisecond.now())).toMillisecond();
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
30
57
|
/**
|
|
31
58
|
* Converts a number or Time object to milliseconds.
|
|
32
59
|
*/
|
|
@@ -43,7 +70,7 @@ export class ExpiryCacheBase {
|
|
|
43
70
|
const expTime = this.argToMs(expirationTime);
|
|
44
71
|
this.data = data;
|
|
45
72
|
this._expiresAt = expTime.time === 0 ? expTime : Millisecond.now().add(expTime);
|
|
46
|
-
this.
|
|
73
|
+
this._expirationTime = expTime;
|
|
47
74
|
this.callback = callback;
|
|
48
75
|
}
|
|
49
76
|
/**
|
|
@@ -80,7 +107,7 @@ export class ExpiryCacheBase {
|
|
|
80
107
|
* Sets the cached data and resets the expiration timestamp based on the expiration time.
|
|
81
108
|
*/
|
|
82
109
|
setData(data) {
|
|
83
|
-
this.setDataExpiresIn(data,
|
|
110
|
+
this.setDataExpiresIn(data, this._expirationTime);
|
|
84
111
|
}
|
|
85
112
|
/**
|
|
86
113
|
* Sets the cached data and sets a new expiration timestamp.
|