1
1
mod common;
2
2
mod sqlx;
3
3
4
- use std:: sync:: Arc ;
5
-
6
4
use :: sqlx:: PgPool ;
7
5
use axum:: {
8
6
extract:: { Query , State } ,
@@ -98,7 +96,7 @@ async fn cache(
98
96
) -> impl IntoResponse {
99
97
let count = parse_params ( params) ;
100
98
let mut rng = SmallRng :: from_rng ( & mut thread_rng ( ) ) . unwrap ( ) ;
101
- let mut worlds: Vec < Option < Arc < World > > > = Vec :: with_capacity ( count) ;
99
+ let mut worlds: Vec < Option < World > > = Vec :: with_capacity ( count) ;
102
100
103
101
for id in random_ids ( & mut rng, count) {
104
102
worlds. push ( cache. get ( & id) . await ) ;
@@ -115,14 +113,15 @@ async fn preload_cache(AppState { db, cache }: &AppState) {
115
113
. expect ( "error loading worlds" ) ;
116
114
117
115
for world in worlds {
118
- cache. insert ( world. id , Arc :: new ( world) ) . await ;
116
+ cache. insert ( world. id , world) . await ;
119
117
}
120
118
}
121
119
120
+ /// Application state
122
121
#[ derive( Clone ) ]
123
122
struct AppState {
124
123
db : PgPool ,
125
- cache : Cache < i32 , Arc < World > > ,
124
+ cache : Cache < i32 , World > ,
126
125
}
127
126
128
127
#[ tokio:: main]
@@ -135,7 +134,10 @@ async fn main() {
135
134
136
135
let state = AppState {
137
136
db : create_pool ( database_url, max_pool_size, min_pool_size) . await ,
138
- cache : Cache :: new ( 10000 ) ,
137
+ cache : Cache :: builder ( )
138
+ . initial_capacity ( 10000 )
139
+ . max_capacity ( 10000 )
140
+ . build ( )
139
141
} ;
140
142
141
143
// Prime the cache with CachedWorld objects
0 commit comments