Skip to content

Commit

Permalink
add async support and examples for async_std and tokio
Browse files Browse the repository at this point in the history
  • Loading branch information
csos95 committed May 30, 2020
1 parent a206c8f commit 8f6689e
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 19 deletions.
15 changes: 13 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@ travis-ci = { repository = "jaemk/cached", branch = "master" }

[features]
default = ["proc_macro"]
proc_macro = []
proc_macro = ["async-std", "cached_proc_macro"]

[dependencies]
once_cell = "1"
cached_proc_macro = { path = "cached_proc_macro"}

[dependencies.async-std]
version = "1.6.0"
optional = true
features = ["attributes"]

[dependencies.cached_proc_macro]
path = "cached_proc_macro"
optional = true

[dev-dependencies]
tokio = { version = "0.2.21", features = ["macros", "time"] }

[workspace]
members = ["cached_proc_macro"]
1 change: 1 addition & 0 deletions cached_proc_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ proc-macro = true
[dependencies]
quote = "1.0.6"
darling = "0.10.2"
async-std = "1.6.0"

[dependencies.syn]
version = "1.0.27"
Expand Down
60 changes: 43 additions & 17 deletions cached_proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
let fn_ident = signature.ident.clone();
let inputs = signature.inputs.clone();
let output = signature.output.clone();
let asyncness = signature.asyncness.clone();

// pull out the names and types of the function inputs
let input_tys = inputs
Expand Down Expand Up @@ -181,28 +182,53 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
};

// put it all together
let expanded = 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 #signature {
use cached::Cached;
let key = #key_convert_block;
{
// check if the result is cached
let mut cache = #cache_ident.lock().unwrap();
if let Some(result) = cache.cache_get(&key) {
return result.clone();
let expanded = if asyncness.is_some() {
quote! {
#visibility static #cache_ident: ::cached::once_cell::sync::Lazy<::cached::async_std::sync::Mutex<#cache_ty>> = ::cached::once_cell::sync::Lazy::new(|| ::cached::async_std::sync::Mutex::new(#cache_create));
#visibility #signature {
use cached::Cached;
let key = #key_convert_block;
{
// check if the result is cached
let mut cache = #cache_ident.lock().await;
if let Some(result) = cache.cache_get(&key) {
return result.clone();
}
}

// run the function and cache the result
async fn inner(#inputs) #output #body;
let result = inner(#(#input_names),*).await;

let mut cache = #cache_ident.lock().await;
#set_cache_block

result
}
}
} 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));
#visibility #signature {
use cached::Cached;
let key = #key_convert_block;
{
// check if the result is cached
let mut cache = #cache_ident.lock().unwrap();
if let Some(result) = cache.cache_get(&key) {
return result.clone();
}
}

// run the function and cache the result
fn inner(#inputs) #output #body;
let result = inner(#(#input_names),*);
// run the function and cache the result
fn inner(#inputs) #output #body;
let result = inner(#(#input_names),*);

let mut cache = #cache_ident.lock().unwrap();
// cache.cache_set(key, result.clone());
#set_cache_block
let mut cache = #cache_ident.lock().unwrap();
#set_cache_block

result
result
}
}
};

Expand Down
24 changes: 24 additions & 0 deletions examples/async_std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use async_std::task::sleep;
use cached::proc_macro::cached;
use std::time::Duration;

async fn sleep_secs(secs: u64) {
sleep(Duration::from_secs(secs)).await;
}

#[cached]
async fn cached_sleep_secs(secs: u64) {
sleep(Duration::from_secs(secs)).await;
}

#[async_std::main]
async fn main() {
println!("sleeping for 4 seconds");
sleep_secs(4).await;
println!("sleeping for 4 seconds");
sleep_secs(4).await;
println!("cached sleeping for 4 seconds");
cached_sleep_secs(4).await;
println!("cached sleeping for 4 seconds");
cached_sleep_secs(4).await;
}
24 changes: 24 additions & 0 deletions examples/tokio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use cached::proc_macro::cached;
use std::time::Duration;
use tokio::time::delay_for;

async fn sleep_secs(secs: u64) {
delay_for(Duration::from_secs(secs)).await;
}

#[cached]
async fn cached_sleep_secs(secs: u64) {
delay_for(Duration::from_secs(secs)).await;
}

#[tokio::main]
async fn main() {
println!("sleeping for 4 seconds");
sleep_secs(4).await;
println!("sleeping for 4 seconds");
sleep_secs(4).await;
println!("cached sleeping for 4 seconds");
cached_sleep_secs(4).await;
println!("cached sleeping for 4 seconds");
cached_sleep_secs(4).await;
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ pub use stores::{SizedCache, TimedCache, UnboundCache};
pub mod proc_macro {
pub use cached_proc_macro::cached;
}
#[cfg(feature = "proc_macro")]
pub use async_std;

/// Cache operations
pub trait Cached<K, V> {
Expand Down

0 comments on commit 8f6689e

Please sign in to comment.