@asaidimu/utils-cache 1.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/index.d.mts ADDED
@@ -0,0 +1,141 @@
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 ADDED
@@ -0,0 +1,141 @@
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 };