-
-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #673 from loco-rs/cache-get-or-insert
add get_or_insert function to cache layer
- Loading branch information
Showing
16 changed files
with
249 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
use crate::models::users; | ||
use loco_rs::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct CacheResponse { | ||
value: Option<String>, | ||
|
@@ -11,14 +11,31 @@ async fn get_cache(State(ctx): State<AppContext>) -> Result<Response> { | |
value: ctx.cache.get("value").await.unwrap(), | ||
}) | ||
} | ||
|
||
async fn insert(State(ctx): State<AppContext>) -> Result<Response> { | ||
ctx.cache.insert("value", "loco cache value").await.unwrap(); | ||
format::empty() | ||
} | ||
|
||
async fn get_or_insert(State(ctx): State<AppContext>) -> Result<Response> { | ||
let res = ctx | ||
.cache | ||
.get_or_insert("user", async { | ||
let user = users::Model::find_by_email(&ctx.db, "[email protected]").await?; | ||
Ok(user.name) | ||
}) | ||
.await; | ||
|
||
match res { | ||
Ok(username) => format::text(&username), | ||
Err(_e) => format::text("not found"), | ||
} | ||
} | ||
|
||
pub fn routes() -> Routes { | ||
Routes::new() | ||
.prefix("cache") | ||
.add("/", get(get_cache)) | ||
.add("/insert", post(insert)) | ||
.add("/get_or_insert", get(get_or_insert)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
use blo::app::App; | ||
use blo::{app::App, models::users}; | ||
use insta::assert_debug_snapshot; | ||
use loco_rs::testing; | ||
use sea_orm::ModelTrait; | ||
use serial_test::serial; | ||
|
||
// TODO: see how to dedup / extract this to app-local test utils | ||
// not to framework, because that would require a runtime dep on insta | ||
|
@@ -14,6 +16,7 @@ macro_rules! configure_insta { | |
} | ||
|
||
#[tokio::test] | ||
#[serial] | ||
async fn ping() { | ||
configure_insta!(); | ||
|
||
|
@@ -27,3 +30,23 @@ async fn ping() { | |
}) | ||
.await; | ||
} | ||
|
||
#[tokio::test] | ||
#[serial] | ||
async fn can_get_or_insert() { | ||
configure_insta!(); | ||
|
||
testing::request::<App, _, _>(|request, ctx| async move { | ||
testing::seed::<App>(&ctx.db).await.unwrap(); | ||
let response = request.get("/cache/get_or_insert").await; | ||
assert_eq!(response.text(), "user1"); | ||
|
||
let user = users::Model::find_by_email(&ctx.db, "[email protected]") | ||
.await | ||
.unwrap(); | ||
user.delete(&ctx.db).await.unwrap(); | ||
let response = request.get("/cache/get_or_insert").await; | ||
assert_eq!(response.text(), "user1"); | ||
}) | ||
.await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
mod auth; | ||
mod cache; | ||
pub mod mylayer; | ||
mod mylayer; | ||
mod notes; | ||
mod ping; | ||
mod prepare_data; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.