pub struct CacheHandle { /* private fields */ }Expand description
A cloneable handle for cache management — invalidating entries and (in PreGenerate mode) managing the list of pre-generated SSG snapshots at runtime.
Implementations§
Source§impl CacheHandle
impl CacheHandle
Sourcepub fn invalidate_all(&self)
pub fn invalidate_all(&self)
Invalidate all cache entries.
Examples found in repository?
9async fn main() {
10 // Initialize tracing (optional but recommended)
11 // tracing_subscriber::fmt::init();
12
13 // Create proxy configuration
14 // You can specify method prefixes to filter by HTTP method
15 let proxy_config = CreateProxyConfig::new("http://localhost:8080".to_string())
16 .with_include_paths(vec![
17 "/api/*".to_string(),
18 "/public/*".to_string(),
19 "GET /admin/stats".to_string(), // Only cache GET requests to this endpoint
20 ])
21 .with_exclude_paths(vec![
22 "/api/admin/*".to_string(),
23 "POST *".to_string(), // Don't cache any POST requests
24 "PUT *".to_string(), // Don't cache any PUT requests
25 "DELETE *".to_string(), // Don't cache any DELETE requests
26 ])
27 .caching_strategy(CacheStrategy::None)
28 .compression_strategy(CompressStrategy::Brotli)
29 .with_cache_storage_mode(phantom_frame::CacheStorageMode::Filesystem)
30 .with_cache_directory(PathBuf::from("./.phantom-frame-cache"))
31 .with_websocket_enabled(true); // Enable WebSocket support (default: true)
32
33 // Create proxy - proxy_url is the backend server to proxy requests to
34 let (proxy_app, handle): (Router, CacheHandle) = create_proxy(proxy_config);
35
36 // You can clone and use the handle in your code
37 let handle_clone = handle.clone();
38
39 // Example: Trigger cache invalidation from another part of your application
40 tokio::spawn(async move {
41 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
42
43 // Invalidate all cache entries
44 handle_clone.invalidate_all();
45 println!("All cache invalidated!");
46
47 tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
48
49 // Invalidate only cache entries matching a pattern (supports wildcards)
50 handle_clone.invalidate("GET:/api/*");
51 println!("Cache invalidated for GET:/api/* pattern!");
52 });
53
54 // Example: PreGenerate (SSG) mode with snapshot management
55 // let ssg_config = CreateProxyConfig::new("http://localhost:8080".to_string())
56 // .with_proxy_mode(ProxyMode::PreGenerate {
57 // paths: vec!["/".to_string(), "/about".to_string(), "/book/1".to_string()],
58 // fallthrough: false, // return 404 on cache miss (default)
59 // });
60 // let (ssg_app, ssg_handle) = create_proxy(ssg_config);
61 // // At runtime, manage snapshots:
62 // ssg_handle.add_snapshot("/book/2").await.unwrap();
63 // ssg_handle.refresh_snapshot("/book/1").await.unwrap();
64 // ssg_handle.remove_snapshot("/about").await.unwrap();
65 // ssg_handle.refresh_all_snapshots().await.unwrap();
66 let _ = ProxyMode::Dynamic; // suppress unused import warning
67
68 // Start the proxy server
69 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
70
71 println!("Proxy server listening on http://0.0.0.0:3000");
72 println!("Caching paths: /api/*, /public/*, GET /admin/stats");
73 println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
74 println!("Cache strategy: none (proxy-only mode)");
75 println!("Compression strategy: brotli (applies only to cached responses)");
76 println!("Cache storage mode: filesystem (custom cache directory)");
77 println!("Note: Cache reads and writes are disabled in this example");
78 println!("WebSocket support: enabled");
79
80 axum::serve(listener, proxy_app).await.unwrap();
81}Sourcepub fn invalidate(&self, pattern: &str)
pub fn invalidate(&self, pattern: &str)
Invalidate cache entries whose key matches pattern.
Supports wildcards: "/api/*", "GET:/api/*", etc.
Examples found in repository?
9async fn main() {
10 // Initialize tracing (optional but recommended)
11 // tracing_subscriber::fmt::init();
12
13 // Create proxy configuration
14 // You can specify method prefixes to filter by HTTP method
15 let proxy_config = CreateProxyConfig::new("http://localhost:8080".to_string())
16 .with_include_paths(vec![
17 "/api/*".to_string(),
18 "/public/*".to_string(),
19 "GET /admin/stats".to_string(), // Only cache GET requests to this endpoint
20 ])
21 .with_exclude_paths(vec![
22 "/api/admin/*".to_string(),
23 "POST *".to_string(), // Don't cache any POST requests
24 "PUT *".to_string(), // Don't cache any PUT requests
25 "DELETE *".to_string(), // Don't cache any DELETE requests
26 ])
27 .caching_strategy(CacheStrategy::None)
28 .compression_strategy(CompressStrategy::Brotli)
29 .with_cache_storage_mode(phantom_frame::CacheStorageMode::Filesystem)
30 .with_cache_directory(PathBuf::from("./.phantom-frame-cache"))
31 .with_websocket_enabled(true); // Enable WebSocket support (default: true)
32
33 // Create proxy - proxy_url is the backend server to proxy requests to
34 let (proxy_app, handle): (Router, CacheHandle) = create_proxy(proxy_config);
35
36 // You can clone and use the handle in your code
37 let handle_clone = handle.clone();
38
39 // Example: Trigger cache invalidation from another part of your application
40 tokio::spawn(async move {
41 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
42
43 // Invalidate all cache entries
44 handle_clone.invalidate_all();
45 println!("All cache invalidated!");
46
47 tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
48
49 // Invalidate only cache entries matching a pattern (supports wildcards)
50 handle_clone.invalidate("GET:/api/*");
51 println!("Cache invalidated for GET:/api/* pattern!");
52 });
53
54 // Example: PreGenerate (SSG) mode with snapshot management
55 // let ssg_config = CreateProxyConfig::new("http://localhost:8080".to_string())
56 // .with_proxy_mode(ProxyMode::PreGenerate {
57 // paths: vec!["/".to_string(), "/about".to_string(), "/book/1".to_string()],
58 // fallthrough: false, // return 404 on cache miss (default)
59 // });
60 // let (ssg_app, ssg_handle) = create_proxy(ssg_config);
61 // // At runtime, manage snapshots:
62 // ssg_handle.add_snapshot("/book/2").await.unwrap();
63 // ssg_handle.refresh_snapshot("/book/1").await.unwrap();
64 // ssg_handle.remove_snapshot("/about").await.unwrap();
65 // ssg_handle.refresh_all_snapshots().await.unwrap();
66 let _ = ProxyMode::Dynamic; // suppress unused import warning
67
68 // Start the proxy server
69 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
70
71 println!("Proxy server listening on http://0.0.0.0:3000");
72 println!("Caching paths: /api/*, /public/*, GET /admin/stats");
73 println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
74 println!("Cache strategy: none (proxy-only mode)");
75 println!("Compression strategy: brotli (applies only to cached responses)");
76 println!("Cache storage mode: filesystem (custom cache directory)");
77 println!("Note: Cache reads and writes are disabled in this example");
78 println!("WebSocket support: enabled");
79
80 axum::serve(listener, proxy_app).await.unwrap();
81}Sourcepub fn subscribe(&self) -> Receiver<InvalidationMessage>
pub fn subscribe(&self) -> Receiver<InvalidationMessage>
Subscribe to invalidation events.
Sourcepub async fn add_snapshot(&self, path: &str) -> Result<()>
pub async fn add_snapshot(&self, path: &str) -> Result<()>
Fetch path from the upstream server, store it in the cache, and add it
to the tracked snapshot list. Only available in PreGenerate mode.
Sourcepub async fn refresh_snapshot(&self, path: &str) -> Result<()>
pub async fn refresh_snapshot(&self, path: &str) -> Result<()>
Re-fetch path from the upstream server and update its cached entry.
Only available in PreGenerate mode.
Sourcepub async fn remove_snapshot(&self, path: &str) -> Result<()>
pub async fn remove_snapshot(&self, path: &str) -> Result<()>
Remove path from the cache and from the tracked snapshot list.
Only available in PreGenerate mode.
Sourcepub async fn refresh_all_snapshots(&self) -> Result<()>
pub async fn refresh_all_snapshots(&self) -> Result<()>
Re-fetch every currently tracked snapshot path from the upstream server. Only available in PreGenerate mode.
Trait Implementations§
Source§impl Clone for CacheHandle
impl Clone for CacheHandle
Source§fn clone(&self) -> CacheHandle
fn clone(&self) -> CacheHandle
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more