pub struct RefreshTrigger { /* private fields */ }Expand description
A trigger that can be cloned and triggered multiple times Similar to oneshot but reusable
Implementations§
Source§impl RefreshTrigger
impl RefreshTrigger
pub fn new() -> Self
Sourcepub fn trigger(&self)
pub fn trigger(&self)
Trigger a full cache refresh (clears all entries)
Examples found in repository?
examples/library_usage.rs (line 44)
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(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, refresh_trigger): (Router, RefreshTrigger) = create_proxy(proxy_config);
35
36 // You can clone and use the refresh_trigger in your code
37 let trigger_clone = refresh_trigger.clone();
38
39 // Example: Trigger cache refresh from another part of your application
40 tokio::spawn(async move {
41 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
42
43 // Clear all cache entries
44 trigger_clone.trigger();
45 println!("All cache cleared!");
46
47 tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
48
49 // Clear only cache entries matching a pattern (supports wildcards)
50 trigger_clone.trigger_by_key_match("GET:/api/*");
51 println!("Cache cleared for GET:/api/* pattern!");
52 });
53
54 // Start the proxy server
55 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
56
57 println!("Proxy server listening on http://0.0.0.0:3000");
58 println!("Caching paths: /api/*, /public/*, GET /admin/stats");
59 println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
60 println!("Cache strategy: none (proxy-only mode)");
61 println!("Compression strategy: brotli (applies only to cached responses)");
62 println!("Cache storage mode: filesystem (custom cache directory)");
63 println!("Note: Cache reads and writes are disabled in this example");
64 println!("WebSocket support: enabled");
65
66 axum::serve(listener, proxy_app).await.unwrap();
67}Sourcepub fn trigger_by_key_match(&self, pattern: &str)
pub fn trigger_by_key_match(&self, pattern: &str)
Trigger a cache refresh for entries matching a pattern Supports wildcards: “/api/”, “GET:/api/”, etc.
Examples found in repository?
examples/library_usage.rs (line 50)
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(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, refresh_trigger): (Router, RefreshTrigger) = create_proxy(proxy_config);
35
36 // You can clone and use the refresh_trigger in your code
37 let trigger_clone = refresh_trigger.clone();
38
39 // Example: Trigger cache refresh from another part of your application
40 tokio::spawn(async move {
41 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
42
43 // Clear all cache entries
44 trigger_clone.trigger();
45 println!("All cache cleared!");
46
47 tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
48
49 // Clear only cache entries matching a pattern (supports wildcards)
50 trigger_clone.trigger_by_key_match("GET:/api/*");
51 println!("Cache cleared for GET:/api/* pattern!");
52 });
53
54 // Start the proxy server
55 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
56
57 println!("Proxy server listening on http://0.0.0.0:3000");
58 println!("Caching paths: /api/*, /public/*, GET /admin/stats");
59 println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
60 println!("Cache strategy: none (proxy-only mode)");
61 println!("Compression strategy: brotli (applies only to cached responses)");
62 println!("Cache storage mode: filesystem (custom cache directory)");
63 println!("Note: Cache reads and writes are disabled in this example");
64 println!("WebSocket support: enabled");
65
66 axum::serve(listener, proxy_app).await.unwrap();
67}Sourcepub fn subscribe(&self) -> Receiver<RefreshMessage>
pub fn subscribe(&self) -> Receiver<RefreshMessage>
Subscribe to refresh events
Trait Implementations§
Source§impl Clone for RefreshTrigger
impl Clone for RefreshTrigger
Source§fn clone(&self) -> RefreshTrigger
fn clone(&self) -> RefreshTrigger
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for RefreshTrigger
impl !RefUnwindSafe for RefreshTrigger
impl Send for RefreshTrigger
impl Sync for RefreshTrigger
impl Unpin for RefreshTrigger
impl UnsafeUnpin for RefreshTrigger
impl !UnwindSafe for RefreshTrigger
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more