@@ -25,7 +25,9 @@ use url::Url;
25
25
26
26
use crate :: config_json:: { gen_config_json_file, is_config_json_url} ;
27
27
use crate :: crate_info:: CrateInfo ;
28
- use crate :: file_cache:: { cache_fetch_crate, cache_store_crate} ;
28
+ use crate :: file_cache:: {
29
+ cache_fetch_crate, cache_fetch_index_entry, cache_store_crate, cache_store_index_entry,
30
+ } ;
29
31
use crate :: index_entry:: IndexEntry ;
30
32
31
33
/// Default listen address and port
@@ -79,6 +81,9 @@ struct ProxyConfig {
79
81
/// External URL of this proxy server (defaults to [`DEFAULT_PROXY_URL`])
80
82
proxy_url : Url ,
81
83
84
+ /// Registry index cache directory (defaults to [`DEFAULT_CACHE_DIR`])
85
+ index_dir : PathBuf ,
86
+
82
87
/// Crate files cache directory (defaults to [`DEFAULT_CACHE_DIR`])
83
88
crates_dir : PathBuf ,
84
89
}
@@ -295,6 +300,8 @@ fn forward_index_request(request: Request, entry: IndexEntry, config: ProxyConfi
295
300
"fetch: successfully got index entry for {entry}" ,
296
301
entry = response. entry
297
302
) ;
303
+
304
+ cache_store_index_entry ( & config. index_dir , & response. entry , & response. data ) ;
298
305
} else {
299
306
debug ! (
300
307
"fetch: cached index entry for {entry} is up to date" ,
@@ -361,7 +368,20 @@ fn handle_index_request(request: Request, index_url: &str, config: &ProxyConfig)
361
368
}
362
369
}
363
370
364
- forward_index_request ( request, index_entry, config. clone ( ) ) ;
371
+ if let Some ( data) = cache_fetch_index_entry ( & config. index_dir , & index_entry) {
372
+ debug ! ( "proxy: local index cache hit for {index_entry}" ) ;
373
+
374
+ // TODO: Add cache control metadata.
375
+ let response = IndexResponse {
376
+ entry : index_entry,
377
+ status : 200 ,
378
+ data,
379
+ } ;
380
+
381
+ send_index_entry_data_response ( request, response) ;
382
+ } else {
383
+ forward_index_request ( request, index_entry, config. clone ( ) ) ;
384
+ }
365
385
}
366
386
367
387
/// Processes one HTTP GET request.
@@ -509,8 +529,14 @@ fn main() {
509
529
info ! ( "proxy: using proxy server URL: {proxy_url}" ) ;
510
530
511
531
let cache_dir = PathBuf :: from ( cache_dir_string) ;
532
+ let index_dir = cache_dir. join ( "index" ) ;
512
533
let crates_dir = cache_dir. join ( "crates" ) ;
513
534
535
+ info ! (
536
+ "cache: using index directory: {}" ,
537
+ index_dir. to_string_lossy( )
538
+ ) ;
539
+
514
540
info ! (
515
541
"cache: using crates directory: {}" ,
516
542
crates_dir. to_string_lossy( )
@@ -520,6 +546,7 @@ fn main() {
520
546
index_url,
521
547
upstream_url,
522
548
proxy_url,
549
+ index_dir,
523
550
crates_dir,
524
551
} ;
525
552
0 commit comments