async-graphql/src/registry/cache_control.rs

112 lines
2.4 KiB
Rust
Raw Normal View History

2020-09-14 00:37:15 +00:00
/// Cache control values
///
/// # Examples
///
/// ```rust
/// use async_graphql::*;
///
2021-11-20 03:16:48 +00:00
/// struct Query;
2020-09-14 00:37:15 +00:00
///
/// #[Object(cache_control(max_age = 60))]
2021-11-20 03:16:48 +00:00
/// impl Query {
2020-09-28 09:44:00 +00:00
/// #[graphql(cache_control(max_age = 30))]
2020-09-14 00:37:15 +00:00
/// async fn value1(&self) -> i32 {
/// 0
/// }
///
2020-09-28 09:44:00 +00:00
/// #[graphql(cache_control(private))]
2020-09-14 00:37:15 +00:00
/// async fn value2(&self) -> i32 {
/// 0
/// }
/// }
///
2021-11-20 03:16:48 +00:00
/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
/// let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
2022-04-19 04:25:11 +00:00
/// assert_eq!(
/// schema
/// .execute("{ value1 }")
/// .await
/// .into_result()
/// .unwrap()
/// .cache_control,
/// CacheControl {
/// public: true,
/// max_age: 30
/// }
/// );
/// assert_eq!(
/// schema
/// .execute("{ value2 }")
/// .await
/// .into_result()
/// .unwrap()
/// .cache_control,
/// CacheControl {
/// public: false,
/// max_age: 60
/// }
/// );
/// assert_eq!(
/// schema
/// .execute("{ value1 value2 }")
/// .await
/// .into_result()
/// .unwrap()
/// .cache_control,
/// CacheControl {
/// public: false,
/// max_age: 30
/// }
/// );
2021-11-20 03:16:48 +00:00
/// # });
2020-09-14 00:37:15 +00:00
/// ```
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct CacheControl {
/// Scope is public, default is true.
pub public: bool,
/// Cache max age, default is 0.
pub max_age: usize,
}
impl Default for CacheControl {
fn default() -> Self {
Self {
public: true,
max_age: 0,
}
}
}
impl CacheControl {
/// Get 'Cache-Control' header value.
#[must_use]
pub fn value(&self) -> Option<String> {
if self.max_age > 0 {
Some(format!(
"max-age={}{}",
self.max_age,
if self.public { "" } else { ", private" }
))
} else {
None
}
}
}
impl CacheControl {
2020-09-17 08:39:55 +00:00
#[must_use]
pub(crate) fn merge(self, other: &CacheControl) -> CacheControl {
CacheControl {
public: self.public && other.public,
max_age: if self.max_age == 0 {
other.max_age
} else if other.max_age == 0 {
self.max_age
} else {
self.max_age.min(other.max_age)
},
}
2020-09-14 00:37:15 +00:00
}
}