Delay set cache after response, make cache static
Uses `tokio::spawn` to delay updating the cache while the server responds to the request. Because `tokio::spawn` can run on another thread, references need to be static, so I initialized the cache in `lazy_static`.
This commit is contained in:
parent
0980d01640
commit
a53eeffb0f
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -142,6 +142,7 @@ dependencies = [
|
|||||||
"http-api-problem",
|
"http-api-problem",
|
||||||
"hyper",
|
"hyper",
|
||||||
"ipnetwork",
|
"ipnetwork",
|
||||||
|
"lazy_static",
|
||||||
"listenfd",
|
"listenfd",
|
||||||
"lru",
|
"lru",
|
||||||
"seahash",
|
"seahash",
|
||||||
|
@ -12,6 +12,7 @@ chrono = { version = "0.4", features = ["serde"] }
|
|||||||
dotenv = "0.15"
|
dotenv = "0.15"
|
||||||
http-api-problem = { version = "0.17", features = ["with-warp"] }
|
http-api-problem = { version = "0.17", features = ["with-warp"] }
|
||||||
hyper = "0.13"
|
hyper = "0.13"
|
||||||
|
lazy_static = "1.4"
|
||||||
listenfd = "0.3"
|
listenfd = "0.3"
|
||||||
tokio = { version = "0.2", features = ["macros", "rt-threaded", "sync"] }
|
tokio = { version = "0.2", features = ["macros", "rt-threaded", "sync"] }
|
||||||
sqlx = { version = "0.3", default-features = false, features = [ "runtime-tokio", "macros", "postgres", "chrono", "uuid", "ipnetwork", "json" ] }
|
sqlx = { version = "0.3", default-features = false, features = [ "runtime-tokio", "macros", "postgres", "chrono", "uuid", "ipnetwork", "json" ] }
|
||||||
|
@ -24,8 +24,8 @@ where
|
|||||||
|
|
||||||
impl<K, V> Cache<K, V>
|
impl<K, V> Cache<K, V>
|
||||||
where
|
where
|
||||||
K: Eq + Hash + Debug,
|
K: Eq + Hash + Debug + Send,
|
||||||
V: Clone,
|
V: Clone + Send,
|
||||||
{
|
{
|
||||||
pub fn new(name: &str, capacity: usize) -> Self {
|
pub fn new(name: &str, capacity: usize) -> Self {
|
||||||
Cache {
|
Cache {
|
||||||
@ -48,7 +48,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get<G, F>(&self, key: K, getter: G) -> Result<V>
|
pub async fn get<G, F>(&'static self, key: K, getter: G) -> Result<V>
|
||||||
where
|
where
|
||||||
G: Fn() -> F,
|
G: Fn() -> F,
|
||||||
F: Future<Output = Result<V>>,
|
F: Future<Output = Result<V>>,
|
||||||
@ -62,8 +62,13 @@ where
|
|||||||
|
|
||||||
self.log_with_key(&key, "get: miss");
|
self.log_with_key(&key, "get: miss");
|
||||||
let value = getter().await?;
|
let value = getter().await?;
|
||||||
let mut guard = self.lru_mutex.lock().await;
|
|
||||||
guard.put(key, value.clone());
|
let to_cache = value.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let mut guard = self.lru_mutex.lock().await;
|
||||||
|
self.log_with_key(&key, "get: update cache");
|
||||||
|
guard.put(key, to_cache);
|
||||||
|
});
|
||||||
|
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
@ -85,10 +90,10 @@ where
|
|||||||
|
|
||||||
impl<K> Cache<K, CachedResponse>
|
impl<K> Cache<K, CachedResponse>
|
||||||
where
|
where
|
||||||
K: Eq + Hash + Debug,
|
K: Eq + Hash + Debug + Send,
|
||||||
{
|
{
|
||||||
pub async fn get_response<G, F, R>(
|
pub async fn get_response<G, F, R>(
|
||||||
&self,
|
&'static self,
|
||||||
key: K,
|
key: K,
|
||||||
getter: G,
|
getter: G,
|
||||||
) -> Result<CachedResponse, Rejection>
|
) -> Result<CachedResponse, Rejection>
|
||||||
@ -111,8 +116,12 @@ where
|
|||||||
let cached_response = CachedResponse::from_reply(reply)
|
let cached_response = CachedResponse::from_reply(reply)
|
||||||
.await
|
.await
|
||||||
.map_err(reject_anyhow)?;
|
.map_err(reject_anyhow)?;
|
||||||
let mut guard = self.lru_mutex.lock().await;
|
let to_cache = cached_response.clone();
|
||||||
guard.put(key, cached_response.clone());
|
tokio::spawn(async move {
|
||||||
|
let mut guard = self.lru_mutex.lock().await;
|
||||||
|
self.log_with_key(&key, "get_response: update cache");
|
||||||
|
guard.put(key, to_cache);
|
||||||
|
});
|
||||||
cached_response
|
cached_response
|
||||||
}
|
}
|
||||||
Err(rejection) => {
|
Err(rejection) => {
|
||||||
|
@ -9,6 +9,10 @@ mod cached_response;
|
|||||||
pub use cache::Cache;
|
pub use cache::Cache;
|
||||||
pub use cached_response::CachedResponse;
|
pub use cached_response::CachedResponse;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
pub static ref CACHES: Caches = Caches::initialize();
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Caches {
|
pub struct Caches {
|
||||||
pub owner_ids_by_api_key: Cache<Uuid, i32>,
|
pub owner_ids_by_api_key: Cache<Uuid, i32>,
|
||||||
|
@ -4,6 +4,7 @@ use uuid::Uuid;
|
|||||||
use warp::reply::{with_header, with_status};
|
use warp::reply::{with_header, with_status};
|
||||||
use warp::{Rejection, Reply};
|
use warp::{Rejection, Reply};
|
||||||
|
|
||||||
|
use crate::caches::CACHES;
|
||||||
use crate::models::{InteriorRefList, ListParams};
|
use crate::models::{InteriorRefList, ListParams};
|
||||||
use crate::problem::reject_anyhow;
|
use crate::problem::reject_anyhow;
|
||||||
use crate::Environment;
|
use crate::Environment;
|
||||||
@ -11,8 +12,7 @@ use crate::Environment;
|
|||||||
use super::{authenticate, check_etag, JsonWithETag};
|
use super::{authenticate, check_etag, JsonWithETag};
|
||||||
|
|
||||||
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
|
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
|
||||||
let response = env
|
let response = CACHES
|
||||||
.caches
|
|
||||||
.interior_ref_list
|
.interior_ref_list
|
||||||
.get_response(id, || async {
|
.get_response(id, || async {
|
||||||
let interior_ref_list = InteriorRefList::get(&env.db, id).await?;
|
let interior_ref_list = InteriorRefList::get(&env.db, id).await?;
|
||||||
@ -29,8 +29,7 @@ pub async fn get_by_shop_id(
|
|||||||
etag: Option<String>,
|
etag: Option<String>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
let response = env
|
let response = CACHES
|
||||||
.caches
|
|
||||||
.interior_ref_list_by_shop_id
|
.interior_ref_list_by_shop_id
|
||||||
.get_response(shop_id, || async {
|
.get_response(shop_id, || async {
|
||||||
let interior_ref_list = InteriorRefList::get_by_shop_id(&env.db, shop_id).await?;
|
let interior_ref_list = InteriorRefList::get_by_shop_id(&env.db, shop_id).await?;
|
||||||
@ -47,8 +46,7 @@ pub async fn list(
|
|||||||
etag: Option<String>,
|
etag: Option<String>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
let response = env
|
let response = CACHES
|
||||||
.caches
|
|
||||||
.list_interior_ref_lists
|
.list_interior_ref_lists
|
||||||
.get_response(list_params.clone(), || async {
|
.get_response(list_params.clone(), || async {
|
||||||
let interior_ref_lists = InteriorRefList::list(&env.db, &list_params).await?;
|
let interior_ref_lists = InteriorRefList::list(&env.db, &list_params).await?;
|
||||||
@ -80,11 +78,13 @@ pub async fn create(
|
|||||||
let reply = JsonWithETag::from_serializable(&saved_interior_ref_list).map_err(reject_anyhow)?;
|
let reply = JsonWithETag::from_serializable(&saved_interior_ref_list).map_err(reject_anyhow)?;
|
||||||
let reply = with_header(reply, "Location", url.as_str());
|
let reply = with_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
env.caches.list_interior_ref_lists.clear().await;
|
tokio::spawn(async move {
|
||||||
env.caches
|
CACHES.list_interior_ref_lists.clear().await;
|
||||||
.interior_ref_list_by_shop_id
|
CACHES
|
||||||
.delete_response(saved_interior_ref_list.shop_id)
|
.interior_ref_list_by_shop_id
|
||||||
.await;
|
.delete_response(saved_interior_ref_list.shop_id)
|
||||||
|
.await;
|
||||||
|
});
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,12 +118,14 @@ pub async fn update(
|
|||||||
JsonWithETag::from_serializable(&updated_interior_ref_list).map_err(reject_anyhow)?;
|
JsonWithETag::from_serializable(&updated_interior_ref_list).map_err(reject_anyhow)?;
|
||||||
let reply = with_header(reply, "Location", url.as_str());
|
let reply = with_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
env.caches.interior_ref_list.delete_response(id).await;
|
tokio::spawn(async move {
|
||||||
env.caches
|
CACHES.interior_ref_list.delete_response(id).await;
|
||||||
.interior_ref_list_by_shop_id
|
CACHES
|
||||||
.delete_response(updated_interior_ref_list.shop_id)
|
.interior_ref_list_by_shop_id
|
||||||
.await;
|
.delete_response(updated_interior_ref_list.shop_id)
|
||||||
env.caches.list_interior_ref_lists.clear().await;
|
.await;
|
||||||
|
CACHES.list_interior_ref_lists.clear().await;
|
||||||
|
});
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,19 +151,21 @@ pub async fn update_by_shop_id(
|
|||||||
JsonWithETag::from_serializable(&updated_interior_ref_list).map_err(reject_anyhow)?;
|
JsonWithETag::from_serializable(&updated_interior_ref_list).map_err(reject_anyhow)?;
|
||||||
let reply = with_header(reply, "Location", url.as_str());
|
let reply = with_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
env.caches
|
tokio::spawn(async move {
|
||||||
.interior_ref_list
|
CACHES
|
||||||
.delete_response(
|
.interior_ref_list
|
||||||
updated_interior_ref_list
|
.delete_response(
|
||||||
.id
|
updated_interior_ref_list
|
||||||
.expect("saved interior_ref_list has no id"),
|
.id
|
||||||
)
|
.expect("saved interior_ref_list has no id"),
|
||||||
.await;
|
)
|
||||||
env.caches
|
.await;
|
||||||
.interior_ref_list_by_shop_id
|
CACHES
|
||||||
.delete_response(updated_interior_ref_list.shop_id)
|
.interior_ref_list_by_shop_id
|
||||||
.await;
|
.delete_response(updated_interior_ref_list.shop_id)
|
||||||
env.caches.list_interior_ref_lists.clear().await;
|
.await;
|
||||||
|
CACHES.list_interior_ref_lists.clear().await;
|
||||||
|
});
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,11 +181,13 @@ pub async fn delete(
|
|||||||
InteriorRefList::delete(&env.db, owner_id, id)
|
InteriorRefList::delete(&env.db, owner_id, id)
|
||||||
.await
|
.await
|
||||||
.map_err(reject_anyhow)?;
|
.map_err(reject_anyhow)?;
|
||||||
env.caches.interior_ref_list.delete_response(id).await;
|
tokio::spawn(async move {
|
||||||
env.caches.list_interior_ref_lists.clear().await;
|
CACHES.interior_ref_list.delete_response(id).await;
|
||||||
env.caches
|
CACHES.list_interior_ref_lists.clear().await;
|
||||||
.interior_ref_list_by_shop_id
|
CACHES
|
||||||
.delete_response(interior_ref_list.shop_id)
|
.interior_ref_list_by_shop_id
|
||||||
.await;
|
.delete_response(interior_ref_list.shop_id)
|
||||||
|
.await;
|
||||||
|
});
|
||||||
Ok(StatusCode::NO_CONTENT)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ use uuid::Uuid;
|
|||||||
use warp::reply::{with_header, with_status};
|
use warp::reply::{with_header, with_status};
|
||||||
use warp::{Rejection, Reply};
|
use warp::{Rejection, Reply};
|
||||||
|
|
||||||
|
use crate::caches::CACHES;
|
||||||
use crate::models::{ListParams, MerchandiseList};
|
use crate::models::{ListParams, MerchandiseList};
|
||||||
use crate::problem::reject_anyhow;
|
use crate::problem::reject_anyhow;
|
||||||
use crate::Environment;
|
use crate::Environment;
|
||||||
@ -11,8 +12,7 @@ use crate::Environment;
|
|||||||
use super::{authenticate, check_etag, JsonWithETag};
|
use super::{authenticate, check_etag, JsonWithETag};
|
||||||
|
|
||||||
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
|
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
|
||||||
let response = env
|
let response = CACHES
|
||||||
.caches
|
|
||||||
.merchandise_list
|
.merchandise_list
|
||||||
.get_response(id, || async {
|
.get_response(id, || async {
|
||||||
let merchandise_list = MerchandiseList::get(&env.db, id).await?;
|
let merchandise_list = MerchandiseList::get(&env.db, id).await?;
|
||||||
@ -29,8 +29,7 @@ pub async fn get_by_shop_id(
|
|||||||
etag: Option<String>,
|
etag: Option<String>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
let response = env
|
let response = CACHES
|
||||||
.caches
|
|
||||||
.merchandise_list_by_shop_id
|
.merchandise_list_by_shop_id
|
||||||
.get_response(shop_id, || async {
|
.get_response(shop_id, || async {
|
||||||
let merchandise_list = MerchandiseList::get_by_shop_id(&env.db, shop_id).await?;
|
let merchandise_list = MerchandiseList::get_by_shop_id(&env.db, shop_id).await?;
|
||||||
@ -47,8 +46,7 @@ pub async fn list(
|
|||||||
etag: Option<String>,
|
etag: Option<String>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
let response = env
|
let response = CACHES
|
||||||
.caches
|
|
||||||
.list_merchandise_lists
|
.list_merchandise_lists
|
||||||
.get_response(list_params.clone(), || async {
|
.get_response(list_params.clone(), || async {
|
||||||
let merchandise_lists = MerchandiseList::list(&env.db, &list_params).await?;
|
let merchandise_lists = MerchandiseList::list(&env.db, &list_params).await?;
|
||||||
@ -80,11 +78,13 @@ pub async fn create(
|
|||||||
let reply = JsonWithETag::from_serializable(&saved_merchandise_list).map_err(reject_anyhow)?;
|
let reply = JsonWithETag::from_serializable(&saved_merchandise_list).map_err(reject_anyhow)?;
|
||||||
let reply = with_header(reply, "Location", url.as_str());
|
let reply = with_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
env.caches.list_merchandise_lists.clear().await;
|
tokio::spawn(async move {
|
||||||
env.caches
|
CACHES.list_merchandise_lists.clear().await;
|
||||||
.merchandise_list_by_shop_id
|
CACHES
|
||||||
.delete_response(saved_merchandise_list.shop_id)
|
.merchandise_list_by_shop_id
|
||||||
.await;
|
.delete_response(saved_merchandise_list.shop_id)
|
||||||
|
.await;
|
||||||
|
});
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,12 +118,14 @@ pub async fn update(
|
|||||||
JsonWithETag::from_serializable(&updated_merchandise_list).map_err(reject_anyhow)?;
|
JsonWithETag::from_serializable(&updated_merchandise_list).map_err(reject_anyhow)?;
|
||||||
let reply = with_header(reply, "Location", url.as_str());
|
let reply = with_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
env.caches.merchandise_list.delete_response(id).await;
|
tokio::spawn(async move {
|
||||||
env.caches
|
CACHES.merchandise_list.delete_response(id).await;
|
||||||
.merchandise_list_by_shop_id
|
CACHES
|
||||||
.delete_response(updated_merchandise_list.shop_id)
|
.merchandise_list_by_shop_id
|
||||||
.await;
|
.delete_response(updated_merchandise_list.shop_id)
|
||||||
env.caches.list_merchandise_lists.clear().await;
|
.await;
|
||||||
|
CACHES.list_merchandise_lists.clear().await;
|
||||||
|
});
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,19 +151,21 @@ pub async fn update_by_shop_id(
|
|||||||
JsonWithETag::from_serializable(&updated_merchandise_list).map_err(reject_anyhow)?;
|
JsonWithETag::from_serializable(&updated_merchandise_list).map_err(reject_anyhow)?;
|
||||||
let reply = with_header(reply, "Location", url.as_str());
|
let reply = with_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
env.caches
|
tokio::spawn(async move {
|
||||||
.merchandise_list
|
CACHES
|
||||||
.delete_response(
|
.merchandise_list
|
||||||
updated_merchandise_list
|
.delete_response(
|
||||||
.id
|
updated_merchandise_list
|
||||||
.expect("saved merchandise_list has no id"),
|
.id
|
||||||
)
|
.expect("saved merchandise_list has no id"),
|
||||||
.await;
|
)
|
||||||
env.caches
|
.await;
|
||||||
.merchandise_list_by_shop_id
|
CACHES
|
||||||
.delete_response(updated_merchandise_list.shop_id)
|
.merchandise_list_by_shop_id
|
||||||
.await;
|
.delete_response(updated_merchandise_list.shop_id)
|
||||||
env.caches.list_merchandise_lists.clear().await;
|
.await;
|
||||||
|
CACHES.list_merchandise_lists.clear().await;
|
||||||
|
});
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,11 +181,13 @@ pub async fn delete(
|
|||||||
MerchandiseList::delete(&env.db, owner_id, id)
|
MerchandiseList::delete(&env.db, owner_id, id)
|
||||||
.await
|
.await
|
||||||
.map_err(reject_anyhow)?;
|
.map_err(reject_anyhow)?;
|
||||||
env.caches.merchandise_list.delete_response(id).await;
|
tokio::spawn(async move {
|
||||||
env.caches
|
CACHES.merchandise_list.delete_response(id).await;
|
||||||
.merchandise_list_by_shop_id
|
CACHES
|
||||||
.delete_response(merchandise_list.shop_id)
|
.merchandise_list_by_shop_id
|
||||||
.await;
|
.delete_response(merchandise_list.shop_id)
|
||||||
env.caches.list_merchandise_lists.clear().await;
|
.await;
|
||||||
|
CACHES.list_merchandise_lists.clear().await;
|
||||||
|
});
|
||||||
Ok(StatusCode::NO_CONTENT)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
}
|
}
|
||||||
|
@ -15,14 +15,14 @@ pub mod owner;
|
|||||||
pub mod shop;
|
pub mod shop;
|
||||||
pub mod transaction;
|
pub mod transaction;
|
||||||
|
|
||||||
use super::caches::CachedResponse;
|
use super::caches::{CachedResponse, CACHES};
|
||||||
use super::problem::{unauthorized_no_api_key, unauthorized_no_owner};
|
use super::problem::{unauthorized_no_api_key, unauthorized_no_owner};
|
||||||
use super::Environment;
|
use super::Environment;
|
||||||
|
|
||||||
#[instrument(level = "debug", skip(env, api_key))]
|
#[instrument(level = "debug", skip(env, api_key))]
|
||||||
pub async fn authenticate(env: &Environment, api_key: Option<Uuid>) -> Result<i32> {
|
pub async fn authenticate(env: &Environment, api_key: Option<Uuid>) -> Result<i32> {
|
||||||
if let Some(api_key) = api_key {
|
if let Some(api_key) = api_key {
|
||||||
env.caches
|
CACHES
|
||||||
.owner_ids_by_api_key
|
.owner_ids_by_api_key
|
||||||
.get(api_key, || async {
|
.get(api_key, || async {
|
||||||
Ok(
|
Ok(
|
||||||
|
@ -6,6 +6,7 @@ use uuid::Uuid;
|
|||||||
use warp::reply::{with_header, with_status};
|
use warp::reply::{with_header, with_status};
|
||||||
use warp::{Rejection, Reply};
|
use warp::{Rejection, Reply};
|
||||||
|
|
||||||
|
use crate::caches::CACHES;
|
||||||
use crate::models::{ListParams, Owner};
|
use crate::models::{ListParams, Owner};
|
||||||
use crate::problem::{reject_anyhow, unauthorized_no_api_key};
|
use crate::problem::{reject_anyhow, unauthorized_no_api_key};
|
||||||
use crate::Environment;
|
use crate::Environment;
|
||||||
@ -13,8 +14,7 @@ use crate::Environment;
|
|||||||
use super::{authenticate, check_etag, JsonWithETag};
|
use super::{authenticate, check_etag, JsonWithETag};
|
||||||
|
|
||||||
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
|
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
|
||||||
let response = env
|
let response = CACHES
|
||||||
.caches
|
|
||||||
.owner
|
.owner
|
||||||
.get_response(id, || async {
|
.get_response(id, || async {
|
||||||
let owner = Owner::get(&env.db, id).await?;
|
let owner = Owner::get(&env.db, id).await?;
|
||||||
@ -31,8 +31,7 @@ pub async fn list(
|
|||||||
etag: Option<String>,
|
etag: Option<String>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
let response = env
|
let response = CACHES
|
||||||
.caches
|
|
||||||
.list_owners
|
.list_owners
|
||||||
.get_response(list_params.clone(), || async {
|
.get_response(list_params.clone(), || async {
|
||||||
let owners = Owner::list(&env.db, &list_params).await?;
|
let owners = Owner::list(&env.db, &list_params).await?;
|
||||||
@ -72,7 +71,9 @@ pub async fn create(
|
|||||||
let reply = JsonWithETag::from_serializable(&saved_owner).map_err(reject_anyhow)?;
|
let reply = JsonWithETag::from_serializable(&saved_owner).map_err(reject_anyhow)?;
|
||||||
let reply = with_header(reply, "Location", url.as_str());
|
let reply = with_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
env.caches.list_owners.clear().await;
|
tokio::spawn(async move {
|
||||||
|
CACHES.list_owners.clear().await;
|
||||||
|
});
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
} else {
|
} else {
|
||||||
Err(reject_anyhow(unauthorized_no_api_key()))
|
Err(reject_anyhow(unauthorized_no_api_key()))
|
||||||
@ -98,8 +99,10 @@ pub async fn update(
|
|||||||
let reply = JsonWithETag::from_serializable(&updated_owner).map_err(reject_anyhow)?;
|
let reply = JsonWithETag::from_serializable(&updated_owner).map_err(reject_anyhow)?;
|
||||||
let reply = with_header(reply, "Location", url.as_str());
|
let reply = with_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
env.caches.owner.delete_response(id).await;
|
tokio::spawn(async move {
|
||||||
env.caches.list_owners.clear().await;
|
CACHES.owner.delete_response(id).await;
|
||||||
|
CACHES.list_owners.clear().await;
|
||||||
|
});
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,11 +115,13 @@ pub async fn delete(
|
|||||||
Owner::delete(&env.db, owner_id, id)
|
Owner::delete(&env.db, owner_id, id)
|
||||||
.await
|
.await
|
||||||
.map_err(reject_anyhow)?;
|
.map_err(reject_anyhow)?;
|
||||||
env.caches.owner.delete_response(id).await;
|
tokio::spawn(async move {
|
||||||
env.caches
|
CACHES.owner.delete_response(id).await;
|
||||||
.owner_ids_by_api_key
|
CACHES
|
||||||
.delete(api_key.expect("api-key has been validated during authenticate"))
|
.owner_ids_by_api_key
|
||||||
.await;
|
.delete(api_key.expect("api-key has been validated during authenticate"))
|
||||||
env.caches.list_owners.clear().await;
|
.await;
|
||||||
|
CACHES.list_owners.clear().await;
|
||||||
|
});
|
||||||
Ok(StatusCode::NO_CONTENT)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ use uuid::Uuid;
|
|||||||
use warp::reply::{with_header, with_status};
|
use warp::reply::{with_header, with_status};
|
||||||
use warp::{Rejection, Reply};
|
use warp::{Rejection, Reply};
|
||||||
|
|
||||||
|
use crate::caches::CACHES;
|
||||||
use crate::models::{InteriorRefList, ListParams, MerchandiseList, Shop};
|
use crate::models::{InteriorRefList, ListParams, MerchandiseList, Shop};
|
||||||
use crate::problem::reject_anyhow;
|
use crate::problem::reject_anyhow;
|
||||||
use crate::Environment;
|
use crate::Environment;
|
||||||
@ -12,8 +13,7 @@ use crate::Environment;
|
|||||||
use super::{authenticate, check_etag, JsonWithETag};
|
use super::{authenticate, check_etag, JsonWithETag};
|
||||||
|
|
||||||
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
|
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
|
||||||
let response = env
|
let response = CACHES
|
||||||
.caches
|
|
||||||
.shop
|
.shop
|
||||||
.get_response(id, || async {
|
.get_response(id, || async {
|
||||||
let shop = Shop::get(&env.db, id).await?;
|
let shop = Shop::get(&env.db, id).await?;
|
||||||
@ -30,8 +30,7 @@ pub async fn list(
|
|||||||
etag: Option<String>,
|
etag: Option<String>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
let response = env
|
let response = CACHES
|
||||||
.caches
|
|
||||||
.list_shops
|
.list_shops
|
||||||
.get_response(list_params.clone(), || async {
|
.get_response(list_params.clone(), || async {
|
||||||
let shops = Shop::list(&env.db, &list_params).await?;
|
let shops = Shop::list(&env.db, &list_params).await?;
|
||||||
@ -90,7 +89,9 @@ pub async fn create(
|
|||||||
let reply = JsonWithETag::from_serializable(&saved_shop).map_err(reject_anyhow)?;
|
let reply = JsonWithETag::from_serializable(&saved_shop).map_err(reject_anyhow)?;
|
||||||
let reply = with_header(reply, "Location", url.as_str());
|
let reply = with_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
env.caches.list_shops.clear().await;
|
tokio::spawn(async move {
|
||||||
|
CACHES.list_shops.clear().await;
|
||||||
|
});
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,8 +123,10 @@ pub async fn update(
|
|||||||
let reply = JsonWithETag::from_serializable(&updated_shop).map_err(reject_anyhow)?;
|
let reply = JsonWithETag::from_serializable(&updated_shop).map_err(reject_anyhow)?;
|
||||||
let reply = with_header(reply, "Location", url.as_str());
|
let reply = with_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
env.caches.shop.delete_response(id).await;
|
tokio::spawn(async move {
|
||||||
env.caches.list_shops.clear().await;
|
CACHES.shop.delete_response(id).await;
|
||||||
|
CACHES.list_shops.clear().await;
|
||||||
|
});
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,15 +139,14 @@ pub async fn delete(
|
|||||||
Shop::delete(&env.db, owner_id, id)
|
Shop::delete(&env.db, owner_id, id)
|
||||||
.await
|
.await
|
||||||
.map_err(reject_anyhow)?;
|
.map_err(reject_anyhow)?;
|
||||||
env.caches.shop.delete_response(id).await;
|
tokio::spawn(async move {
|
||||||
env.caches.list_shops.clear().await;
|
CACHES.shop.delete_response(id).await;
|
||||||
env.caches
|
CACHES.list_shops.clear().await;
|
||||||
.interior_ref_list_by_shop_id
|
CACHES
|
||||||
.delete_response(id)
|
.interior_ref_list_by_shop_id
|
||||||
.await;
|
.delete_response(id)
|
||||||
env.caches
|
.await;
|
||||||
.merchandise_list_by_shop_id
|
CACHES.merchandise_list_by_shop_id.delete_response(id).await;
|
||||||
.delete_response(id)
|
});
|
||||||
.await;
|
|
||||||
Ok(StatusCode::NO_CONTENT)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ use uuid::Uuid;
|
|||||||
use warp::reply::{with_header, with_status};
|
use warp::reply::{with_header, with_status};
|
||||||
use warp::{Rejection, Reply};
|
use warp::{Rejection, Reply};
|
||||||
|
|
||||||
|
use crate::caches::CACHES;
|
||||||
use crate::models::{ListParams, MerchandiseList, Transaction};
|
use crate::models::{ListParams, MerchandiseList, Transaction};
|
||||||
use crate::problem::reject_anyhow;
|
use crate::problem::reject_anyhow;
|
||||||
use crate::Environment;
|
use crate::Environment;
|
||||||
@ -11,8 +12,7 @@ use crate::Environment;
|
|||||||
use super::{authenticate, check_etag, JsonWithETag};
|
use super::{authenticate, check_etag, JsonWithETag};
|
||||||
|
|
||||||
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
|
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
|
||||||
let response = env
|
let response = CACHES
|
||||||
.caches
|
|
||||||
.transaction
|
.transaction
|
||||||
.get_response(id, || async {
|
.get_response(id, || async {
|
||||||
let transaction = Transaction::get(&env.db, id).await?;
|
let transaction = Transaction::get(&env.db, id).await?;
|
||||||
@ -29,8 +29,7 @@ pub async fn list(
|
|||||||
etag: Option<String>,
|
etag: Option<String>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
let response = env
|
let response = CACHES
|
||||||
.caches
|
|
||||||
.list_transactions
|
.list_transactions
|
||||||
.get_response(list_params.clone(), || async {
|
.get_response(list_params.clone(), || async {
|
||||||
let transactions = Transaction::list(&env.db, &list_params).await?;
|
let transactions = Transaction::list(&env.db, &list_params).await?;
|
||||||
@ -48,8 +47,7 @@ pub async fn list_by_shop_id(
|
|||||||
etag: Option<String>,
|
etag: Option<String>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
let response = env
|
let response = CACHES
|
||||||
.caches
|
|
||||||
.list_transactions_by_shop_id
|
.list_transactions_by_shop_id
|
||||||
.get_response((shop_id, list_params.clone()), || async {
|
.get_response((shop_id, list_params.clone()), || async {
|
||||||
let transactions = Transaction::list_by_shop_id(&env.db, shop_id, &list_params).await?;
|
let transactions = Transaction::list_by_shop_id(&env.db, shop_id, &list_params).await?;
|
||||||
@ -104,22 +102,24 @@ pub async fn create(
|
|||||||
let reply = JsonWithETag::from_serializable(&saved_transaction).map_err(reject_anyhow)?;
|
let reply = JsonWithETag::from_serializable(&saved_transaction).map_err(reject_anyhow)?;
|
||||||
let reply = with_header(reply, "Location", url.as_str());
|
let reply = with_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
// TODO: will this make these caches effectively useless?
|
tokio::spawn(async move {
|
||||||
env.caches.list_transactions.clear().await;
|
// TODO: will this make these caches effectively useless?
|
||||||
env.caches.list_transactions_by_shop_id.clear().await;
|
CACHES.list_transactions.clear().await;
|
||||||
env.caches
|
CACHES.list_transactions_by_shop_id.clear().await;
|
||||||
.merchandise_list
|
CACHES
|
||||||
.delete_response(
|
.merchandise_list
|
||||||
updated_merchandise_list
|
.delete_response(
|
||||||
.id
|
updated_merchandise_list
|
||||||
.expect("saved merchandise_list has no id"),
|
.id
|
||||||
)
|
.expect("saved merchandise_list has no id"),
|
||||||
.await;
|
)
|
||||||
env.caches
|
.await;
|
||||||
.merchandise_list_by_shop_id
|
CACHES
|
||||||
.delete_response(updated_merchandise_list.shop_id)
|
.merchandise_list_by_shop_id
|
||||||
.await;
|
.delete_response(updated_merchandise_list.shop_id)
|
||||||
env.caches.list_merchandise_lists.clear().await;
|
.await;
|
||||||
|
CACHES.list_merchandise_lists.clear().await;
|
||||||
|
});
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,8 +132,10 @@ pub async fn delete(
|
|||||||
Transaction::delete(&env.db, owner_id, id)
|
Transaction::delete(&env.db, owner_id, id)
|
||||||
.await
|
.await
|
||||||
.map_err(reject_anyhow)?;
|
.map_err(reject_anyhow)?;
|
||||||
env.caches.transaction.delete_response(id).await;
|
tokio::spawn(async move {
|
||||||
env.caches.list_transactions.clear().await;
|
CACHES.transaction.delete_response(id).await;
|
||||||
env.caches.list_transactions_by_shop_id.clear().await;
|
CACHES.list_transactions.clear().await;
|
||||||
|
CACHES.list_transactions_by_shop_id.clear().await;
|
||||||
|
});
|
||||||
Ok(StatusCode::NO_CONTENT)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
#[macro_use]
|
||||||
|
extern crate lazy_static;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
@ -18,13 +21,11 @@ mod macros;
|
|||||||
mod models;
|
mod models;
|
||||||
mod problem;
|
mod problem;
|
||||||
|
|
||||||
use caches::Caches;
|
|
||||||
use models::{InteriorRefList, ListParams, MerchandiseList, Owner, Shop, Transaction};
|
use models::{InteriorRefList, ListParams, MerchandiseList, Owner, Shop, Transaction};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Environment {
|
pub struct Environment {
|
||||||
pub db: PgPool,
|
pub db: PgPool,
|
||||||
pub caches: Caches,
|
|
||||||
pub api_url: Url,
|
pub api_url: Url,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +36,6 @@ impl Environment {
|
|||||||
.max_size(5)
|
.max_size(5)
|
||||||
.build(&env::var("DATABASE_URL")?)
|
.build(&env::var("DATABASE_URL")?)
|
||||||
.await?,
|
.await?,
|
||||||
caches: Caches::initialize(),
|
|
||||||
api_url,
|
api_url,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user