Skip to content

Commit b0e6f0a

Browse files
perf: replacement of moka with quick_cache
1 parent e174623 commit b0e6f0a

File tree

3 files changed

+20
-110
lines changed

3 files changed

+20
-110
lines changed

frameworks/Rust/axum/Cargo.lock

Lines changed: 13 additions & 100 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frameworks/Rust/axum/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ axum-core = { version = "0.4.5", optional = true }
7777
mime = { version = "0.3.17", optional = true }
7878
bytes = { version = "1.9.0", optional = true }
7979
serde_path_to_error = { version = "0.1.16", optional = true }
80-
moka = { version = "0.12.8", features = ["future"] }
8180
socket2 = "0.5.8"
8281
hyper = { version = "1.5", features = ["server", "http1"] }
8382
hyper-util = { version = "0.1", features = ["tokio", "server-auto", "http1"] }
83+
quick_cache = "0.6.9"
8484

8585

8686
[profile.release]

frameworks/Rust/axum/src/main_sqlx.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use axum::{
1212
Router,
1313
};
1414
use dotenv::dotenv;
15-
use moka::future::Cache;
15+
use quick_cache::sync::Cache;
1616
use rand::{rngs::SmallRng, thread_rng, SeedableRng};
1717
use sqlx::models::World;
1818
use yarte::Template;
@@ -98,10 +98,10 @@ async fn cache(
9898
) -> impl IntoResponse {
9999
let count = parse_params(params);
100100
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
101-
let mut worlds: Vec<Option<Arc<World>>> = Vec::with_capacity(count);
101+
let mut worlds: Vec<Option<World>> = Vec::with_capacity(count);
102102

103103
for id in random_ids(&mut rng, count) {
104-
worlds.push(cache.get(&id).await);
104+
worlds.push(cache.get(&id));
105105
}
106106

107107
(StatusCode::OK, Json(worlds))
@@ -115,15 +115,15 @@ async fn preload_cache(AppState { db, cache }: &AppState) {
115115
.expect("error loading worlds");
116116

117117
for world in worlds {
118-
cache.insert(world.id, Arc::new(world)).await;
118+
cache.insert(world.id, world);
119119
}
120120
}
121121

122122
/// Application state
123123
#[derive(Clone)]
124124
struct AppState {
125125
db: PgPool,
126-
cache: Cache<i32, Arc<World>>,
126+
cache: Arc<Cache<i32, World>>,
127127
}
128128

129129
#[tokio::main]
@@ -136,10 +136,7 @@ async fn main() {
136136

137137
let state = AppState {
138138
db: create_pool(database_url, max_pool_size, min_pool_size).await,
139-
cache: Cache::builder()
140-
.initial_capacity(10000)
141-
.max_capacity(10000)
142-
.build()
139+
cache: Arc::new(Cache::new(10_000))
143140
};
144141

145142
// Prime the cache with CachedWorld objects

0 commit comments

Comments
 (0)