@asaidimu/utils-cache 1.0.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 +400 -240
- package/package.json +4 -4
- package/LICENSE.md +0 -21
- package/index.d.mts +0 -141
- package/index.d.ts +0 -141
- package/index.js +0 -677
- package/index.mjs +0 -650
package/package.json
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
{
|
2
2
|
"name": "@asaidimu/utils-cache",
|
3
|
-
"version": "
|
3
|
+
"version": "2.0.0",
|
4
4
|
"description": "Caching utilities for @asaidimu applications.",
|
5
5
|
"main": "index.js",
|
6
6
|
"module": "index.mjs",
|
7
7
|
"types": "index.d.ts",
|
8
8
|
"files": [
|
9
|
-
"
|
9
|
+
"./dist/*"
|
10
10
|
],
|
11
11
|
"keywords": [
|
12
12
|
"typescript",
|
@@ -29,7 +29,7 @@
|
|
29
29
|
"access": "public"
|
30
30
|
},
|
31
31
|
"dependencies": {
|
32
|
-
"@asaidimu/utils-persistence": "
|
32
|
+
"@asaidimu/utils-persistence": "2.0.0",
|
33
33
|
"uuid": "^11.1.0"
|
34
34
|
},
|
35
35
|
"exports": {
|
@@ -49,7 +49,7 @@
|
|
49
49
|
[
|
50
50
|
"@semantic-release/npm",
|
51
51
|
{
|
52
|
-
"pkgRoot": "
|
52
|
+
"pkgRoot": "."
|
53
53
|
}
|
54
54
|
],
|
55
55
|
[
|
package/LICENSE.md
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2025 Saidimu
|
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/index.d.mts
DELETED
@@ -1,141 +0,0 @@
|
|
1
|
-
import { S as SimplePersistence } from '../types-D26luDbE.js';
|
2
|
-
|
3
|
-
interface CacheOptions {
|
4
|
-
staleTime?: number;
|
5
|
-
cacheTime?: number;
|
6
|
-
retryAttempts?: number;
|
7
|
-
retryDelay?: number;
|
8
|
-
maxSize?: number;
|
9
|
-
enableMetrics?: boolean;
|
10
|
-
persistence?: SimplePersistence<SerializableCacheState>;
|
11
|
-
persistenceId?: string;
|
12
|
-
serializeValue?: (value: any) => any;
|
13
|
-
deserializeValue?: (value: any) => any;
|
14
|
-
persistenceDebounceTime?: number;
|
15
|
-
}
|
16
|
-
interface CacheMetrics {
|
17
|
-
hits: number;
|
18
|
-
misses: number;
|
19
|
-
fetches: number;
|
20
|
-
errors: number;
|
21
|
-
evictions: number;
|
22
|
-
staleHits: number;
|
23
|
-
}
|
24
|
-
interface SerializableCacheEntry {
|
25
|
-
data: any;
|
26
|
-
lastUpdated: number;
|
27
|
-
lastAccessed: number;
|
28
|
-
accessCount: number;
|
29
|
-
error?: {
|
30
|
-
name: string;
|
31
|
-
message: string;
|
32
|
-
stack?: string;
|
33
|
-
};
|
34
|
-
}
|
35
|
-
type SerializableCacheState = Array<[string, SerializableCacheEntry]>;
|
36
|
-
type CacheEventBase<Type extends string, Payload = {}> = {
|
37
|
-
type: Type;
|
38
|
-
key: string;
|
39
|
-
timestamp: number;
|
40
|
-
} & Payload;
|
41
|
-
type CacheHitEvent<T = any> = CacheEventBase<'hit', {
|
42
|
-
data: T;
|
43
|
-
isStale: boolean;
|
44
|
-
}>;
|
45
|
-
type CacheMissEvent = CacheEventBase<'miss'>;
|
46
|
-
type CacheFetchEvent = CacheEventBase<'fetch', {
|
47
|
-
attempt: number;
|
48
|
-
}>;
|
49
|
-
type CacheErrorEvent = CacheEventBase<'error', {
|
50
|
-
error: Error;
|
51
|
-
attempt: number;
|
52
|
-
}>;
|
53
|
-
type CacheEvictionEvent = CacheEventBase<'eviction', {
|
54
|
-
reason?: string;
|
55
|
-
}>;
|
56
|
-
type CacheInvalidationEvent = CacheEventBase<'invalidation'>;
|
57
|
-
type CacheSetDataEvent<T = any> = CacheEventBase<'set_data', {
|
58
|
-
newData: T;
|
59
|
-
oldData?: T;
|
60
|
-
}>;
|
61
|
-
type CachePersistenceEventPayload = {
|
62
|
-
event: 'load_success' | 'remote_update';
|
63
|
-
message?: string;
|
64
|
-
} | {
|
65
|
-
event: 'load_fail' | 'save_fail' | 'clear_fail';
|
66
|
-
message?: string;
|
67
|
-
error?: any;
|
68
|
-
} | {
|
69
|
-
event: 'save_success' | 'clear_success';
|
70
|
-
};
|
71
|
-
type CachePersistenceEvent = CacheEventBase<'persistence', CachePersistenceEventPayload>;
|
72
|
-
type CacheEvent = CacheHitEvent | CacheMissEvent | CacheFetchEvent | CacheErrorEvent | CacheEvictionEvent | CacheInvalidationEvent | CacheSetDataEvent | CachePersistenceEvent;
|
73
|
-
type CacheEventType = CacheEvent['type'];
|
74
|
-
|
75
|
-
declare class Cache {
|
76
|
-
private cache;
|
77
|
-
private queries;
|
78
|
-
private fetching;
|
79
|
-
private readonly defaultOptions;
|
80
|
-
private metrics;
|
81
|
-
private eventListeners;
|
82
|
-
private gcTimer?;
|
83
|
-
private readonly persistenceId;
|
84
|
-
private persistenceUnsubscribe?;
|
85
|
-
private persistenceDebounceTimer?;
|
86
|
-
private isHandlingRemoteUpdate;
|
87
|
-
constructor(defaultOptions?: CacheOptions);
|
88
|
-
private initializePersistence;
|
89
|
-
private serializeCache;
|
90
|
-
private deserializeAndLoadCache;
|
91
|
-
private schedulePersistState;
|
92
|
-
private handleRemoteStateChange;
|
93
|
-
registerQuery<T>(key: string, fetchFunction: () => Promise<T>, options?: CacheOptions): void;
|
94
|
-
get<T>(key: string, options?: {
|
95
|
-
waitForFresh?: boolean;
|
96
|
-
throwOnError?: boolean;
|
97
|
-
}): Promise<T | undefined>;
|
98
|
-
peek<T>(key: string): T | undefined;
|
99
|
-
has(key: string): boolean;
|
100
|
-
private fetch;
|
101
|
-
private fetchAndWait;
|
102
|
-
private performFetchWithRetry;
|
103
|
-
private isStale;
|
104
|
-
invalidate(key: string, refetch?: boolean): Promise<void>;
|
105
|
-
invalidatePattern(pattern: RegExp, refetch?: boolean): Promise<void>;
|
106
|
-
prefetch(key: string): Promise<void>;
|
107
|
-
refresh<T>(key: string): Promise<T | undefined>;
|
108
|
-
setData<T>(key: string, data: T): void;
|
109
|
-
remove(key: string): boolean;
|
110
|
-
private enforceSizeLimit;
|
111
|
-
private startGarbageCollection;
|
112
|
-
garbageCollect(): number;
|
113
|
-
getStats(): {
|
114
|
-
size: number;
|
115
|
-
metrics: CacheMetrics;
|
116
|
-
hitRate: number;
|
117
|
-
staleHitRate: number;
|
118
|
-
entries: Array<{
|
119
|
-
key: string;
|
120
|
-
lastAccessed: number;
|
121
|
-
lastUpdated: number;
|
122
|
-
accessCount: number;
|
123
|
-
isStale: boolean;
|
124
|
-
isLoading?: boolean;
|
125
|
-
error?: boolean;
|
126
|
-
}>;
|
127
|
-
};
|
128
|
-
on<EType extends CacheEventType>(event: EType, listener: (ev: Extract<CacheEvent, {
|
129
|
-
type: EType;
|
130
|
-
}>) => void): void;
|
131
|
-
off<EType extends CacheEventType>(event: EType, listener: (ev: Extract<CacheEvent, {
|
132
|
-
type: EType;
|
133
|
-
}>) => void): void;
|
134
|
-
private emitEvent;
|
135
|
-
private updateMetrics;
|
136
|
-
private delay;
|
137
|
-
clear(): Promise<void>;
|
138
|
-
destroy(): void;
|
139
|
-
}
|
140
|
-
|
141
|
-
export { Cache };
|
package/index.d.ts
DELETED
@@ -1,141 +0,0 @@
|
|
1
|
-
import { S as SimplePersistence } from '../types-D26luDbE.js';
|
2
|
-
|
3
|
-
interface CacheOptions {
|
4
|
-
staleTime?: number;
|
5
|
-
cacheTime?: number;
|
6
|
-
retryAttempts?: number;
|
7
|
-
retryDelay?: number;
|
8
|
-
maxSize?: number;
|
9
|
-
enableMetrics?: boolean;
|
10
|
-
persistence?: SimplePersistence<SerializableCacheState>;
|
11
|
-
persistenceId?: string;
|
12
|
-
serializeValue?: (value: any) => any;
|
13
|
-
deserializeValue?: (value: any) => any;
|
14
|
-
persistenceDebounceTime?: number;
|
15
|
-
}
|
16
|
-
interface CacheMetrics {
|
17
|
-
hits: number;
|
18
|
-
misses: number;
|
19
|
-
fetches: number;
|
20
|
-
errors: number;
|
21
|
-
evictions: number;
|
22
|
-
staleHits: number;
|
23
|
-
}
|
24
|
-
interface SerializableCacheEntry {
|
25
|
-
data: any;
|
26
|
-
lastUpdated: number;
|
27
|
-
lastAccessed: number;
|
28
|
-
accessCount: number;
|
29
|
-
error?: {
|
30
|
-
name: string;
|
31
|
-
message: string;
|
32
|
-
stack?: string;
|
33
|
-
};
|
34
|
-
}
|
35
|
-
type SerializableCacheState = Array<[string, SerializableCacheEntry]>;
|
36
|
-
type CacheEventBase<Type extends string, Payload = {}> = {
|
37
|
-
type: Type;
|
38
|
-
key: string;
|
39
|
-
timestamp: number;
|
40
|
-
} & Payload;
|
41
|
-
type CacheHitEvent<T = any> = CacheEventBase<'hit', {
|
42
|
-
data: T;
|
43
|
-
isStale: boolean;
|
44
|
-
}>;
|
45
|
-
type CacheMissEvent = CacheEventBase<'miss'>;
|
46
|
-
type CacheFetchEvent = CacheEventBase<'fetch', {
|
47
|
-
attempt: number;
|
48
|
-
}>;
|
49
|
-
type CacheErrorEvent = CacheEventBase<'error', {
|
50
|
-
error: Error;
|
51
|
-
attempt: number;
|
52
|
-
}>;
|
53
|
-
type CacheEvictionEvent = CacheEventBase<'eviction', {
|
54
|
-
reason?: string;
|
55
|
-
}>;
|
56
|
-
type CacheInvalidationEvent = CacheEventBase<'invalidation'>;
|
57
|
-
type CacheSetDataEvent<T = any> = CacheEventBase<'set_data', {
|
58
|
-
newData: T;
|
59
|
-
oldData?: T;
|
60
|
-
}>;
|
61
|
-
type CachePersistenceEventPayload = {
|
62
|
-
event: 'load_success' | 'remote_update';
|
63
|
-
message?: string;
|
64
|
-
} | {
|
65
|
-
event: 'load_fail' | 'save_fail' | 'clear_fail';
|
66
|
-
message?: string;
|
67
|
-
error?: any;
|
68
|
-
} | {
|
69
|
-
event: 'save_success' | 'clear_success';
|
70
|
-
};
|
71
|
-
type CachePersistenceEvent = CacheEventBase<'persistence', CachePersistenceEventPayload>;
|
72
|
-
type CacheEvent = CacheHitEvent | CacheMissEvent | CacheFetchEvent | CacheErrorEvent | CacheEvictionEvent | CacheInvalidationEvent | CacheSetDataEvent | CachePersistenceEvent;
|
73
|
-
type CacheEventType = CacheEvent['type'];
|
74
|
-
|
75
|
-
declare class Cache {
|
76
|
-
private cache;
|
77
|
-
private queries;
|
78
|
-
private fetching;
|
79
|
-
private readonly defaultOptions;
|
80
|
-
private metrics;
|
81
|
-
private eventListeners;
|
82
|
-
private gcTimer?;
|
83
|
-
private readonly persistenceId;
|
84
|
-
private persistenceUnsubscribe?;
|
85
|
-
private persistenceDebounceTimer?;
|
86
|
-
private isHandlingRemoteUpdate;
|
87
|
-
constructor(defaultOptions?: CacheOptions);
|
88
|
-
private initializePersistence;
|
89
|
-
private serializeCache;
|
90
|
-
private deserializeAndLoadCache;
|
91
|
-
private schedulePersistState;
|
92
|
-
private handleRemoteStateChange;
|
93
|
-
registerQuery<T>(key: string, fetchFunction: () => Promise<T>, options?: CacheOptions): void;
|
94
|
-
get<T>(key: string, options?: {
|
95
|
-
waitForFresh?: boolean;
|
96
|
-
throwOnError?: boolean;
|
97
|
-
}): Promise<T | undefined>;
|
98
|
-
peek<T>(key: string): T | undefined;
|
99
|
-
has(key: string): boolean;
|
100
|
-
private fetch;
|
101
|
-
private fetchAndWait;
|
102
|
-
private performFetchWithRetry;
|
103
|
-
private isStale;
|
104
|
-
invalidate(key: string, refetch?: boolean): Promise<void>;
|
105
|
-
invalidatePattern(pattern: RegExp, refetch?: boolean): Promise<void>;
|
106
|
-
prefetch(key: string): Promise<void>;
|
107
|
-
refresh<T>(key: string): Promise<T | undefined>;
|
108
|
-
setData<T>(key: string, data: T): void;
|
109
|
-
remove(key: string): boolean;
|
110
|
-
private enforceSizeLimit;
|
111
|
-
private startGarbageCollection;
|
112
|
-
garbageCollect(): number;
|
113
|
-
getStats(): {
|
114
|
-
size: number;
|
115
|
-
metrics: CacheMetrics;
|
116
|
-
hitRate: number;
|
117
|
-
staleHitRate: number;
|
118
|
-
entries: Array<{
|
119
|
-
key: string;
|
120
|
-
lastAccessed: number;
|
121
|
-
lastUpdated: number;
|
122
|
-
accessCount: number;
|
123
|
-
isStale: boolean;
|
124
|
-
isLoading?: boolean;
|
125
|
-
error?: boolean;
|
126
|
-
}>;
|
127
|
-
};
|
128
|
-
on<EType extends CacheEventType>(event: EType, listener: (ev: Extract<CacheEvent, {
|
129
|
-
type: EType;
|
130
|
-
}>) => void): void;
|
131
|
-
off<EType extends CacheEventType>(event: EType, listener: (ev: Extract<CacheEvent, {
|
132
|
-
type: EType;
|
133
|
-
}>) => void): void;
|
134
|
-
private emitEvent;
|
135
|
-
private updateMetrics;
|
136
|
-
private delay;
|
137
|
-
clear(): Promise<void>;
|
138
|
-
destroy(): void;
|
139
|
-
}
|
140
|
-
|
141
|
-
export { Cache };
|