pub struct CacheMiddleware { /* private fields */ }Expand description
Cache Middleware
§Examples
use std::sync::Arc;
use std::time::Duration;
use reinhardt_middleware::cache::{CacheMiddleware, CacheConfig, CacheKeyStrategy};
use reinhardt_http::{Handler, Middleware, Request, Response};
use hyper::{StatusCode, Method, Version, HeaderMap};
use bytes::Bytes;
struct TestHandler;
#[async_trait::async_trait]
impl Handler for TestHandler {
async fn handle(&self, _request: Request) -> reinhardt_core::exception::Result<Response> {
Ok(Response::new(StatusCode::OK).with_body(Bytes::from("OK")))
}
}
let config = CacheConfig::new(Duration::from_secs(60), CacheKeyStrategy::UrlOnly);
let middleware = CacheMiddleware::new(config);
let handler = Arc::new(TestHandler);
let request = Request::builder()
.method(Method::GET)
.uri("/api/data")
.version(Version::HTTP_11)
.headers(HeaderMap::new())
.body(Bytes::new())
.build()
.unwrap();
let response = middleware.process(request, handler).await.unwrap();
assert_eq!(response.status, StatusCode::OK);Implementations§
Source§impl CacheMiddleware
impl CacheMiddleware
Sourcepub fn new(config: CacheConfig) -> Self
pub fn new(config: CacheConfig) -> Self
Create a new cache middleware
§Examples
use std::time::Duration;
use reinhardt_middleware::cache::{CacheMiddleware, CacheConfig, CacheKeyStrategy};
let config = CacheConfig::new(Duration::from_secs(300), CacheKeyStrategy::UrlOnly);
let middleware = CacheMiddleware::new(config);Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Create with default configuration
Sourcepub fn from_arc(config: CacheConfig, store: Arc<CacheStore>) -> Self
pub fn from_arc(config: CacheConfig, store: Arc<CacheStore>) -> Self
Create from an existing Arc-wrapped cache store
This is provided for cases where you already have an Arc<CacheStore>.
In most cases, you should use new() instead, which creates the store internally.
Sourcepub fn store(&self) -> &CacheStore
pub fn store(&self) -> &CacheStore
Get a reference to the cache store
§Examples
use std::time::Duration;
use reinhardt_middleware::cache::{CacheMiddleware, CacheConfig, CacheKeyStrategy};
let middleware = CacheMiddleware::new(
CacheConfig::new(Duration::from_secs(300), CacheKeyStrategy::UrlOnly)
);
// Access the store
let store = middleware.store();
assert_eq!(store.len(), 0);Sourcepub fn store_arc(&self) -> Arc<CacheStore>
pub fn store_arc(&self) -> Arc<CacheStore>
Get a cloned Arc of the store (for cases where you need ownership)
In most cases, you should use store() instead to get a reference.
Trait Implementations§
Source§impl Default for CacheMiddleware
impl Default for CacheMiddleware
Source§impl Middleware for CacheMiddleware
impl Middleware for CacheMiddleware
Auto Trait Implementations§
impl Freeze for CacheMiddleware
impl RefUnwindSafe for CacheMiddleware
impl Send for CacheMiddleware
impl Sync for CacheMiddleware
impl Unpin for CacheMiddleware
impl UnsafeUnpin for CacheMiddleware
impl UnwindSafe for CacheMiddleware
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian().