Skip to content

Commit

Permalink
cached: Merge lock_key into lock
Browse files Browse the repository at this point in the history
  • Loading branch information
raimannma committed Sep 19, 2024
1 parent 32d1bd0 commit f8dce37
Showing 1 changed file with 43 additions and 46 deletions.
89 changes: 43 additions & 46 deletions cached_proc_macro/src/cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,6 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
Some(ref name) => Ident::new(name, fn_ident.span()),
None => Ident::new(&fn_ident.to_string().to_uppercase(), fn_ident.span()),
};
let cache_ident_key = match args.name {
Some(ref name) => Ident::new(&format!("{}_key", name), fn_ident.span()),
None => Ident::new(
&format!("{}_key", fn_ident.to_string().to_uppercase()),
fn_ident.span(),
),
};

let (cache_key_ty, key_convert_block) =
make_cache_key_type(&args.key, &args.convert, &args.ty, input_tys, &input_names);
Expand Down Expand Up @@ -219,23 +212,24 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
let no_cache_fn_ident = Ident::new(&format!("{}_no_cache", &fn_ident), fn_ident.span());

let lock;
let lock_key;
let function_no_cache;
let function_call;
let ty;
if asyncness.is_some() {
lock = quote! {
let mut cache = #cache_ident.lock().await;
};

lock_key = quote! {
let mut locks = #cache_ident_key.lock().await;
let lock = locks
.entry(key.clone())
.or_insert_with(|| std::sync::Arc::new(::cached::async_sync::Mutex::new(#cache_create)))
.clone();
drop(locks);
let mut cache = lock.lock().await;
lock = if args.sync_writes_by_key {
quote! {
let mut locks = #cache_ident.lock().await;
let lock = locks
.entry(key.clone())
.or_insert_with(|| std::sync::Arc::new(::cached::async_sync::Mutex::new(#cache_create)))
.clone();
drop(locks);
let mut cache = lock.lock().await;
}
} else {
quote! {
let mut cache = #cache_ident.lock().await;
}
};

function_no_cache = quote! {
Expand All @@ -246,20 +240,27 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
let result = #no_cache_fn_ident(#(#input_names),*).await;
};

ty = quote! {
#visibility static #cache_ident: ::cached::once_cell::sync::Lazy<::cached::async_sync::Mutex<#cache_ty>> = ::cached::once_cell::sync::Lazy::new(|| ::cached::async_sync::Mutex::new(#cache_create));
#visibility static #cache_ident_key: ::cached::once_cell::sync::Lazy<::cached::async_sync::Mutex<std::collections::HashMap<#cache_key_ty, std::sync::Arc<::cached::async_sync::Mutex<#cache_ty>>>>> = ::cached::once_cell::sync::Lazy::new(|| ::cached::async_sync::Mutex::new(std::collections::HashMap::new()));
ty = if args.sync_writes_by_key {
quote! {
#visibility static #cache_ident: ::cached::once_cell::sync::Lazy<::cached::async_sync::Mutex<std::collections::HashMap<#cache_key_ty, std::sync::Arc<::cached::async_sync::Mutex<#cache_ty>>>>> = ::cached::once_cell::sync::Lazy::new(|| ::cached::async_sync::Mutex::new(std::collections::HashMap::new()));
}
} else {
quote! {
#visibility static #cache_ident: ::cached::once_cell::sync::Lazy<::cached::async_sync::Mutex<#cache_ty>> = ::cached::once_cell::sync::Lazy::new(|| ::cached::async_sync::Mutex::new(#cache_create));
}
};
} else {
lock = quote! {
let mut cache = #cache_ident.lock().unwrap();
};

lock_key = quote! {
let mut locks = #cache_ident_key.lock().unwrap();
let lock = locks.entry(key.clone()).or_insert_with(|| std::sync::Arc::new(std::sync::Mutex::new(#cache_create))).clone();
drop(locks);
let mut cache = lock.lock().unwrap();
lock = if args.sync_writes_by_key {
quote! {
let mut locks = #cache_ident.lock().unwrap();
let lock = locks.entry(key.clone()).or_insert_with(|| std::sync::Arc::new(std::sync::Mutex::new(#cache_create))).clone();
drop(locks);
let mut cache = lock.lock().unwrap();
}
} else {
quote! {
let mut cache = #cache_ident.lock().unwrap();
}
};

function_no_cache = quote! {
Expand All @@ -270,10 +271,15 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
let result = #no_cache_fn_ident(#(#input_names),*);
};

ty = quote! {
#visibility static #cache_ident: ::cached::once_cell::sync::Lazy<std::sync::Mutex<#cache_ty>> = ::cached::once_cell::sync::Lazy::new(|| std::sync::Mutex::new(#cache_create));
#visibility static #cache_ident_key: ::cached::once_cell::sync::Lazy<std::sync::Mutex<std::collections::HashMap<#cache_key_ty, std::sync::Arc<std::sync::Mutex<#cache_ty>>>>> = ::cached::once_cell::sync::Lazy::new(|| std::sync::Mutex::new(std::collections::HashMap::new()));
};
ty = if args.sync_writes_by_key {
quote! {
#visibility static #cache_ident: ::cached::once_cell::sync::Lazy<std::sync::Mutex<std::collections::HashMap<#cache_key_ty, std::sync::Arc<std::sync::Mutex<#cache_ty>>>>> = ::cached::once_cell::sync::Lazy::new(|| std::sync::Mutex::new(std::collections::HashMap::new()));
}
} else {
quote! {
#visibility static #cache_ident: ::cached::once_cell::sync::Lazy<std::sync::Mutex<#cache_ty>> = ::cached::once_cell::sync::Lazy::new(|| std::sync::Mutex::new(#cache_create));
}
}
}

let prime_do_set_return_block = quote! {
Expand All @@ -284,16 +290,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
#set_cache_and_return
};

let do_set_return_block = if args.sync_writes_by_key {
quote! {
#lock_key
if let Some(result) = cache.cache_get(&key) {
#return_cache_block
}
#function_call
#set_cache_and_return
}
} else if args.sync_writes {
let do_set_return_block = if args.sync_writes_by_key || args.sync_writes {
quote! {
#lock
if let Some(result) = cache.cache_get(&key) {
Expand Down

0 comments on commit f8dce37

Please sign in to comment.