Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions frameworks/Rust/axum/src/main_sqlx.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mod common;
mod sqlx;

use std::sync::Arc;

use ::sqlx::PgPool;
use axum::{
extract::{Query, State},
Expand Down Expand Up @@ -98,7 +96,7 @@ async fn cache(
) -> impl IntoResponse {
let count = parse_params(params);
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
let mut worlds: Vec<Option<Arc<World>>> = Vec::with_capacity(count);
let mut worlds: Vec<Option<World>> = Vec::with_capacity(count);

for id in random_ids(&mut rng, count) {
worlds.push(cache.get(&id).await);
Expand All @@ -115,14 +113,15 @@ async fn preload_cache(AppState { db, cache }: &AppState) {
.expect("error loading worlds");

for world in worlds {
cache.insert(world.id, Arc::new(world)).await;
cache.insert(world.id, world).await;
}
}

/// Application state
#[derive(Clone)]
struct AppState {
db: PgPool,
cache: Cache<i32, Arc<World>>,
cache: Cache<i32, World>,
}

#[tokio::main]
Expand All @@ -135,7 +134,10 @@ async fn main() {

let state = AppState {
db: create_pool(database_url, max_pool_size, min_pool_size).await,
cache: Cache::new(10000),
cache: Cache::builder()
.initial_capacity(10000)
.max_capacity(10000)
.build()
};

// Prime the cache with CachedWorld objects
Expand Down
Loading