@gvnrdao/dh-sdk 0.0.291 → 0.0.293

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.
@@ -1,236 +1,74 @@
1
1
  /**
2
- * Generic Cache Manager Module
2
+ * Cache Manager Module
3
3
  *
4
- * Provides a type-safe, generic LRU cache with TTL support.
5
- * Can be used for any data type (Bitcoin balances, addresses, query results, etc.)
6
- *
7
- * Features:
8
- * - Generic type support
9
- * - TTL-based expiration
10
- * - LRU eviction policy
11
- * - Hit/miss statistics
12
- * - Memory-efficient
13
- * - Thread-safe operations
14
- */
15
- import { Result } from '../../types/result';
16
- import { SDKError } from '../../utils/error-handler';
17
- /**
18
- * Cache entry with metadata
4
+ * Provides a simple cache factory for SDK modules.
5
+ * Each cache is an independent LRU cache with TTL support.
19
6
  */
20
- interface CacheEntry<T> {
21
- /** Cached value */
22
- value: T;
23
- /** Unix timestamp when entry was created (ms) */
24
- timestamp: number;
25
- /** Number of times this entry was accessed */
26
- hits: number;
27
- /** Unix timestamp of last access (ms) */
28
- lastAccessed: number;
7
+ export interface CacheConfig {
8
+ maxSize: number;
9
+ ttlMs: number;
29
10
  }
30
- /**
31
- * Cache statistics
32
- */
33
11
  export interface CacheStats {
34
- /** Current number of entries in cache */
35
12
  size: number;
36
- /** Total cache hits */
37
- hits: number;
38
- /** Total cache misses */
39
- misses: number;
40
- /** Total evictions performed */
41
- evictions: number;
42
- /** Timestamp of oldest entry (ms) */
43
- oldestEntry: number;
44
- /** Timestamp of newest entry (ms) */
45
- newestEntry: number;
46
- /** Hit rate as percentage (0-100) */
47
- hitRate: number;
13
+ maxSize: number;
14
+ ttlMs: number;
48
15
  }
49
16
  /**
50
- * Cache configuration
51
- */
52
- export interface CacheConfig {
53
- /** Maximum number of entries (default: 1000) */
54
- maxSize?: number;
55
- /** Time-to-live in milliseconds (default: 60000 = 1 minute) */
56
- ttlMs?: number;
57
- /** Enable debug logging (default: false) */
58
- debug?: boolean;
59
- /** Cache name for logging (default: 'Cache') */
60
- name?: string;
61
- }
62
- /**
63
- * Generic LRU Cache with TTL support
64
- *
65
- * @template K - Key type (usually string)
66
- * @template V - Value type
67
- *
68
- * @example
69
- * ```typescript
70
- * // Create a cache for Bitcoin balances
71
- * const balanceCache = new LRUCache<string, BitcoinBalance>({
72
- * maxSize: 500,
73
- * ttlMs: 60000,
74
- * name: 'BitcoinBalance'
75
- * });
76
- *
77
- * // Set value
78
- * balanceCache.set('bc1q...', { balance: Satoshis(50000000n) });
79
- *
80
- * // Get value
81
- * const balance = balanceCache.get('bc1q...');
82
- * if (balance) {
83
- * console.log('Cached balance:', balance.balance);
84
- * }
85
- * ```
17
+ * Simple LRU Cache with TTL support
86
18
  */
87
- export declare class LRUCache<K, V> {
19
+ export declare class Cache<T = any> {
88
20
  private cache;
89
- /**
90
- * Audit M-J: singleflight registry for `getOrCompute` / `getOrComputeResult`.
91
- * Concurrent cache-miss callers for the same key share one inflight promise
92
- * instead of each running `compute()` independently — important when the
93
- * compute spends a paid LIT capacity credit or hits a rate-limited upstream.
94
- */
95
- private inflight;
96
- private inflightResult;
97
21
  private readonly maxSize;
98
22
  private readonly ttlMs;
99
- private readonly debug;
100
- private readonly name;
101
- private stats;
102
- constructor(config?: CacheConfig);
23
+ constructor(config: CacheConfig);
103
24
  /**
104
25
  * Get value from cache
105
- *
106
- * Returns null if:
107
- * - Key not found
108
- * - Entry has expired
109
- *
110
- * @param key - Cache key
111
- * @returns Cached value or null
112
26
  */
113
- get(key: K): V | null;
114
- /**
115
- * Get value from cache with Result wrapper
116
- *
117
- * Useful when you want to distinguish between "not found" and "expired"
118
- */
119
- getResult(key: K): Result<V, SDKError>;
27
+ get(key: string): T | undefined;
120
28
  /**
121
29
  * Set value in cache
122
- *
123
- * If cache is full, evicts the least recently used entry
124
- *
125
- * @param key - Cache key
126
- * @param value - Value to cache
127
- * @param ttl - Optional custom TTL for this entry (ms)
128
- */
129
- set(key: K, value: V, ttl?: number): void;
130
- /**
131
- * Set value in cache with Result wrapper
132
30
  */
133
- setResult(key: K, value: V, ttl?: number): Result<void, SDKError>;
31
+ set(key: string, value: T, ttl?: number): void;
134
32
  /**
135
- * Check if key exists in cache (without affecting stats)
33
+ * Check if key exists in cache
136
34
  */
137
- has(key: K): boolean;
35
+ has(key: string): boolean;
138
36
  /**
139
- * Delete specific key from cache
37
+ * Delete key from cache
140
38
  */
141
- delete(key: K): boolean;
39
+ delete(key: string): boolean;
142
40
  /**
143
- * Clear entire cache
41
+ * Clear all cache entries
144
42
  */
145
43
  clear(): void;
146
44
  /**
147
- * Get current cache size
148
- */
149
- size(): number;
150
- /**
151
- * Get cache statistics
152
- */
153
- getStats(): CacheStats;
154
- /**
155
- * Get hit rate percentage
156
- */
157
- getHitRate(): number;
158
- /**
159
- * Get all cached keys (for debugging)
160
- */
161
- getKeys(): K[];
162
- /**
163
- * Get all cached values (for debugging)
164
- */
165
- getValues(): V[];
166
- /**
167
- * Get all cache entries with metadata (for debugging)
168
- */
169
- getEntries(): Array<{
170
- key: K;
171
- value: V;
172
- metadata: Omit<CacheEntry<V>, 'value'>;
173
- }>;
174
- /**
175
- * Clean up expired entries
176
- *
177
- * Useful for periodic maintenance
178
- *
179
- * @returns Number of entries cleaned
45
+ * Clean expired entries
180
46
  */
181
47
  cleanExpired(): number;
182
48
  /**
183
- * Check if cache entry is expired
184
- */
185
- private isExpired;
186
- /**
187
- * Evict least recently used entry
188
- */
189
- private evictLRU;
190
- /**
191
- * Get or compute value
192
- *
193
- * If key exists in cache, returns cached value.
194
- * Otherwise, computes value using provided function and caches it.
195
- *
196
- * @param key - Cache key
197
- * @param compute - Function to compute value if not in cache
198
- * @param ttl - Optional custom TTL for this entry
199
- * @returns Cached or computed value
200
- */
201
- getOrCompute(key: K, compute: () => Promise<V>, ttl?: number): Promise<V>;
202
- /**
203
- * Get or compute value with Result wrapper
49
+ * Get cache statistics
204
50
  */
205
- getOrComputeResult(key: K, compute: () => Promise<Result<V, SDKError>>, ttl?: number): Promise<Result<V, SDKError>>;
206
- }
207
- /**
208
- * Generic cache interface for type safety
209
- */
210
- export interface Cache<T> {
211
- get(key: string): T | null | undefined;
212
- set(key: string, value: T): void;
213
- delete(key: string): boolean;
214
- clear(): void;
215
- has(key: string): boolean;
216
- size(): number;
217
- getStats(): CacheStats;
51
+ getStats(): {
52
+ size: number;
53
+ maxSize: number;
54
+ ttlMs: number;
55
+ };
218
56
  }
219
57
  /**
220
- * Cache Manager - Factory for creating specialized caches
58
+ * Cache Manager
59
+ *
60
+ * Factory for creating named caches with specific configurations
221
61
  */
222
62
  export declare class CacheManager {
223
63
  private caches;
224
- private readonly globalConfig;
225
- constructor(globalConfig?: CacheConfig);
64
+ private readonly debug;
65
+ constructor(config?: {
66
+ debug?: boolean;
67
+ });
226
68
  /**
227
- * Create or get a named cache
228
- *
229
- * @param name - Unique cache name
230
- * @param config - Optional cache-specific configuration
231
- * @returns LRU cache instance
69
+ * Get or create a cache instance
232
70
  */
233
- getCache<K, V>(name: string, config?: CacheConfig): LRUCache<K, V>;
71
+ getCache<K extends string = string, V = any>(name: K, config: CacheConfig): Cache<V>;
234
72
  /**
235
73
  * Clear all caches
236
74
  */
@@ -242,18 +80,13 @@ export declare class CacheManager {
242
80
  /**
243
81
  * Get statistics for all caches
244
82
  */
245
- getAllStats(): Record<string, CacheStats>;
246
- /**
247
- * Get list of all cache names
248
- */
249
- getCacheNames(): string[];
83
+ getAllStats(): Record<string, ReturnType<Cache["getStats"]>>;
250
84
  /**
251
- * Delete a named cache
85
+ * Destroy cache manager
252
86
  */
253
- deleteCache(name: string): boolean;
87
+ destroy(): void;
254
88
  }
255
- /**
256
- * Factory function to create a CacheManager instance
257
- */
258
- export declare function createCacheManager(config?: CacheConfig): CacheManager;
259
- export {};
89
+ export { Cache as LRUCache };
90
+ export declare function createCacheManager(config?: {
91
+ debug?: boolean;
92
+ }): CacheManager;
@@ -847,7 +847,11 @@ export declare class DiamondHandsSDK {
847
847
  /**
848
848
  * Get cache statistics
849
849
  */
850
- getCacheStats(): Record<string, import("./cache/cache-manager.module").CacheStats>;
850
+ getCacheStats(): Record<string, {
851
+ size: number;
852
+ maxSize: number;
853
+ ttlMs: number;
854
+ }>;
851
855
  /**
852
856
  * Get contract manager (for advanced usage)
853
857
  */
@@ -196,7 +196,11 @@ export declare class LoanQuery {
196
196
  /**
197
197
  * Get cache statistics
198
198
  */
199
- getCacheStats(): import("../cache/cache-manager.module").CacheStats | null;
199
+ getCacheStats(): {
200
+ size: number;
201
+ maxSize: number;
202
+ ttlMs: number;
203
+ } | null;
200
204
  }
201
205
  /**
202
206
  * Factory function to create a LoanQuery instance
@@ -128,7 +128,11 @@ export declare class PKPManager {
128
128
  /**
129
129
  * Get cache statistics
130
130
  */
131
- getCacheStats(): import("../cache/cache-manager.module").CacheStats | null;
131
+ getCacheStats(): {
132
+ size: number;
133
+ maxSize: number;
134
+ ttlMs: number;
135
+ } | null;
132
136
  }
133
137
  /**
134
138
  * Factory function to create a PKPManager instance
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gvnrdao/dh-sdk",
3
- "version": "0.0.291",
3
+ "version": "0.0.293",
4
4
  "description": "TypeScript SDK for Diamond Hands Protocol - Bitcoin-backed lending with LIT Protocol PKPs",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",