11mod common;
22mod sqlx;
33
4- use std:: sync:: Arc ;
5-
64use :: sqlx:: PgPool ;
75use axum:: {
86 extract:: { Query , State } ,
@@ -98,7 +96,7 @@ async fn cache(
9896) -> impl IntoResponse {
9997 let count = parse_params ( params) ;
10098 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) ;
102100
103101 for id in random_ids ( & mut rng, count) {
104102 worlds. push ( cache. get ( & id) . await ) ;
@@ -115,14 +113,15 @@ async fn preload_cache(AppState { db, cache }: &AppState) {
115113 . expect ( "error loading worlds" ) ;
116114
117115 for world in worlds {
118- cache. insert ( world. id , Arc :: new ( world) ) . await ;
116+ cache. insert ( world. id , world) . await ;
119117 }
120118}
121119
120+ /// Application state
122121#[ derive( Clone ) ]
123122struct AppState {
124123 db : PgPool ,
125- cache : Cache < i32 , Arc < World > > ,
124+ cache : Cache < i32 , World > ,
126125}
127126
128127#[ tokio:: main]
@@ -135,7 +134,10 @@ async fn main() {
135134
136135 let state = AppState {
137136 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 ( )
139141 } ;
140142
141143 // Prime the cache with CachedWorld objects
0 commit comments