RefreshTrigger

Struct RefreshTrigger 

Source
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

Source

pub fn new() -> Self

Source

pub fn trigger(&self)

Trigger a full cache refresh (clears all entries)

Examples found in repository?
examples/library_usage.rs (line 36)
5async fn main() {
6    // Initialize tracing (optional but recommended)
7    // tracing_subscriber::fmt::init();
8
9    // Create proxy configuration
10    // You can specify method prefixes to filter by HTTP method
11    let proxy_config = CreateProxyConfig::new("http://localhost:8080".to_string())
12        .with_include_paths(vec![
13            "/api/*".to_string(),
14            "/public/*".to_string(),
15            "GET /admin/stats".to_string(), // Only cache GET requests to this endpoint
16        ])
17        .with_exclude_paths(vec![
18            "/api/admin/*".to_string(),
19            "POST *".to_string(),  // Don't cache any POST requests
20            "PUT *".to_string(),   // Don't cache any PUT requests
21            "DELETE *".to_string(), // Don't cache any DELETE requests
22        ])
23        .with_websocket_enabled(true); // Enable WebSocket support (default: true)
24
25    // Create proxy - proxy_url is the backend server to proxy requests to
26    let (proxy_app, refresh_trigger): (Router, RefreshTrigger) = create_proxy(proxy_config);
27
28    // You can clone and use the refresh_trigger in your code
29    let trigger_clone = refresh_trigger.clone();
30
31    // Example: Trigger cache refresh from another part of your application
32    tokio::spawn(async move {
33        tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
34        
35        // Clear all cache entries
36        trigger_clone.trigger();
37        println!("All cache cleared!");
38        
39        tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
40        
41        // Clear only cache entries matching a pattern (supports wildcards)
42        trigger_clone.trigger_by_key_match("GET:/api/*");
43        println!("Cache cleared for GET:/api/* pattern!");
44    });
45
46    // Start the proxy server
47    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
48
49    println!("Proxy server listening on http://0.0.0.0:3000");
50    println!("Caching paths: /api/*, /public/*, GET /admin/stats");
51    println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
52    println!("Note: Only GET requests will be cached (POST/PUT/DELETE are excluded)");
53    println!("WebSocket support: enabled");
54
55    axum::serve(listener, proxy_app).await.unwrap();
56}
Source

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 42)
5async fn main() {
6    // Initialize tracing (optional but recommended)
7    // tracing_subscriber::fmt::init();
8
9    // Create proxy configuration
10    // You can specify method prefixes to filter by HTTP method
11    let proxy_config = CreateProxyConfig::new("http://localhost:8080".to_string())
12        .with_include_paths(vec![
13            "/api/*".to_string(),
14            "/public/*".to_string(),
15            "GET /admin/stats".to_string(), // Only cache GET requests to this endpoint
16        ])
17        .with_exclude_paths(vec![
18            "/api/admin/*".to_string(),
19            "POST *".to_string(),  // Don't cache any POST requests
20            "PUT *".to_string(),   // Don't cache any PUT requests
21            "DELETE *".to_string(), // Don't cache any DELETE requests
22        ])
23        .with_websocket_enabled(true); // Enable WebSocket support (default: true)
24
25    // Create proxy - proxy_url is the backend server to proxy requests to
26    let (proxy_app, refresh_trigger): (Router, RefreshTrigger) = create_proxy(proxy_config);
27
28    // You can clone and use the refresh_trigger in your code
29    let trigger_clone = refresh_trigger.clone();
30
31    // Example: Trigger cache refresh from another part of your application
32    tokio::spawn(async move {
33        tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
34        
35        // Clear all cache entries
36        trigger_clone.trigger();
37        println!("All cache cleared!");
38        
39        tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
40        
41        // Clear only cache entries matching a pattern (supports wildcards)
42        trigger_clone.trigger_by_key_match("GET:/api/*");
43        println!("Cache cleared for GET:/api/* pattern!");
44    });
45
46    // Start the proxy server
47    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
48
49    println!("Proxy server listening on http://0.0.0.0:3000");
50    println!("Caching paths: /api/*, /public/*, GET /admin/stats");
51    println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
52    println!("Note: Only GET requests will be cached (POST/PUT/DELETE are excluded)");
53    println!("WebSocket support: enabled");
54
55    axum::serve(listener, proxy_app).await.unwrap();
56}
Source

pub fn subscribe(&self) -> Receiver<RefreshMessage>

Subscribe to refresh events

Trait Implementations§

Source§

impl Clone for RefreshTrigger

Source§

fn clone(&self) -> RefreshTrigger

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for RefreshTrigger

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more