@@ -22,12 +22,22 @@ const DEFAULT_TTL = process.env.LOCAL_DEV
22
22
? 30 * 60 * 1000 // cache 30 minutes when local dev
23
23
: 1 * 60 * 1000 ; // cache 1 minute in production
24
24
25
- await fs . mkdir ( CACHE_DIR , { recursive : true } ) ;
25
+ async function ensureCacheDir ( ) {
26
+ await fs . mkdir ( CACHE_DIR , { recursive : true } ) ;
27
+ }
26
28
27
- const keyv = new Keyv ( {
28
- store : new KeyvSqlite ( CACHE_FILE ) ,
29
- ttl : DEFAULT_TTL ,
30
- } ) ;
29
+ let keyv : Keyv | null = null ;
30
+
31
+ async function getKeyv ( ) {
32
+ if ( ! keyv ) {
33
+ await ensureCacheDir ( ) ;
34
+ keyv = new Keyv ( {
35
+ store : new KeyvSqlite ( CACHE_FILE ) ,
36
+ ttl : DEFAULT_TTL ,
37
+ } ) ;
38
+ }
39
+ return keyv ;
40
+ }
31
41
32
42
function createCacheKey ( basePath : string [ ] , prop : string | symbol , args : any [ ] ) : string {
33
43
// Create a deterministic key from the path and arguments
@@ -58,9 +68,10 @@ function createCachedProxy(target: any, basePath: string[] = []): any {
58
68
if ( typeof value === "function" ) {
59
69
return async function ( ...args : any [ ] ) {
60
70
const cacheKey = createCacheKey ( basePath , prop , args ) ;
71
+ const keyvInstance = await getKeyv ( ) ;
61
72
62
73
// Try to get from cache first
63
- const cached = await keyv . get ( cacheKey ) ;
74
+ const cached = await keyvInstance . get ( cacheKey ) ;
64
75
if ( cached !== undefined ) {
65
76
// console.log(`HIT|${cacheKey}`); // cache hit info for debug
66
77
return cached ;
@@ -70,7 +81,7 @@ function createCachedProxy(target: any, basePath: string[] = []): any {
70
81
const result = await value . apply ( obj , args ) ;
71
82
72
83
// Cache the result
73
- await keyv . set ( cacheKey , result ) ;
84
+ await keyvInstance . set ( cacheKey , result ) ;
74
85
75
86
return result ;
76
87
} ;
@@ -95,7 +106,8 @@ type DeepAsyncWrapper<T> = {
95
106
} ;
96
107
97
108
export async function clearGhCache ( ) : Promise < void > {
98
- await keyv . clear ( ) ;
109
+ const keyvInstance = await getKeyv ( ) ;
110
+ await keyvInstance . clear ( ) ;
99
111
}
100
112
101
113
export async function getGhCacheStats ( ) : Promise < { size : number ; keys : string [ ] } > {
@@ -108,22 +120,26 @@ export const ghc = createCachedProxy(gh) as DeepAsyncWrapper<typeof gh>;
108
120
109
121
// manual test with real api
110
122
if ( import . meta. main ) {
111
- // Test the cached client
112
- console . log ( "Testing cached GitHub client..." ) ;
113
-
114
- // This should make a real API call
115
- const result1 = await ghc . repos . get ( {
116
- owner : "octocat" ,
117
- repo : "Hello-World" ,
118
- } ) ;
119
- console . log ( "First call result:" , result1 . data . name ) ;
120
-
121
- // This should use cache
122
- const result2 = await ghc . repos . get ( {
123
- owner : "octocat" ,
124
- repo : "Hello-World" ,
125
- } ) ;
126
- console . log ( "Second call result (cached):" , result2 . data . name ) ;
123
+ async function runTest ( ) {
124
+ // Test the cached client
125
+ console . log ( "Testing cached GitHub client..." ) ;
126
+
127
+ // This should make a real API call
128
+ const result1 = await ghc . repos . get ( {
129
+ owner : "octocat" ,
130
+ repo : "Hello-World" ,
131
+ } ) ;
132
+ console . log ( "First call result:" , result1 . data . name ) ;
133
+
134
+ // This should use cache
135
+ const result2 = await ghc . repos . get ( {
136
+ owner : "octocat" ,
137
+ repo : "Hello-World" ,
138
+ } ) ;
139
+ console . log ( "Second call result (cached):" , result2 . data . name ) ;
140
+
141
+ console . log ( "Cache test complete!" ) ;
142
+ }
127
143
128
- console . log ( "Cache test complete!" ) ;
144
+ runTest ( ) . catch ( console . error ) ;
129
145
}
0 commit comments