#[macro_export]
macro_rules! cached {
($cachename:ident;
fn $name:ident ($($arg:ident : $argtype:ty),*) -> $ret:ty = $body:expr) => {
cached!(
$cachename : $crate::UnboundCache<($($argtype),*), $ret> = $crate::UnboundCache::new();
fn $name($($arg : $argtype),*) -> $ret = $body
);
};
($cachename:ident : $cachetype:ty = $cacheinstance:expr ;
fn $name:ident ($($arg:ident : $argtype:ty),*) -> $ret:ty = $body:expr) => {
static $cachename: $crate::once_cell::sync::Lazy<::std::sync::Mutex<$cachetype>>
= $crate::once_cell::sync::Lazy::new(|| ::std::sync::Mutex::new($cacheinstance));
#[allow(unused_parens)]
pub fn $name($($arg: $argtype),*) -> $ret {
let key = ($($arg.clone()),*);
{
let mut cache = $cachename.lock().unwrap();
let res = $crate::Cached::cache_get(&mut *cache, &key);
if let Some(res) = res { return res.clone(); }
}
let val = (||$body)();
let mut cache = $cachename.lock().unwrap();
$crate::Cached::cache_set(&mut *cache, key, val.clone());
val
}
};
($cachename:ident : $cachetype:ty = $cacheinstance:expr ;
async fn $name:ident ($($arg:ident : $argtype:ty),*) -> $ret:ty = $body:block) => {
static $cachename: $crate::once_cell::sync::Lazy<::std::sync::Mutex<$cachetype>>
= $crate::once_cell::sync::Lazy::new(|| ::std::sync::Mutex::new($cacheinstance));
#[allow(unused_parens)]
pub async fn $name($($arg: $argtype),*) -> $ret {
let key = ($($arg.clone()),*);
{
let mut cache = $cachename.lock().unwrap();
let res = $crate::Cached::cache_get(&mut *cache, &key);
if let Some(res) = res { return res.clone(); }
}
async fn inner($($arg: $argtype),*) -> $ret $body
let val = inner($($arg),*).await;
let mut cache = $cachename.lock().unwrap();
$crate::Cached::cache_set(&mut *cache, key, val.clone());
val
}
};
}
#[macro_export]
macro_rules! cached_key {
($cachename:ident : $cachetype:ty = $cacheinstance:expr ;
Key = $key:expr;
fn $name:ident ($($arg:ident : $argtype:ty),*) -> $ret:ty = $body:expr) => {
static $cachename: $crate::once_cell::sync::Lazy<::std::sync::Mutex<$cachetype>>
= $crate::once_cell::sync::Lazy::new(|| ::std::sync::Mutex::new($cacheinstance));
#[allow(unused_parens)]
pub fn $name($($arg: $argtype),*) -> $ret {
let key = $key;
{
let mut cache = $cachename.lock().unwrap();
let res = $crate::Cached::cache_get(&mut *cache, &key);
if let Some(res) = res { return res.clone(); }
}
let val = (||$body)();
let mut cache = $cachename.lock().unwrap();
$crate::Cached::cache_set(&mut *cache, key, val.clone());
val
}
};
($cachename:ident : $cachetype:ty = $cacheinstance:expr ;
Key = $key:expr;
async fn $name:ident ($($arg:ident : $argtype:ty),*) -> $ret:ty = $body:expr) => {
static $cachename: $crate::once_cell::sync::Lazy<::std::sync::Mutex<$cachetype>>
= $crate::once_cell::sync::Lazy::new(|| ::std::sync::Mutex::new($cacheinstance));
#[allow(unused_parens)]
pub async fn $name($($arg: $argtype),*) -> $ret {
let key = $key;
{
let mut cache = $cachename.lock().unwrap();
let res = $crate::Cached::cache_get(&mut *cache, &key);
if let Some(res) = res { return res.clone(); }
}
async fn inner($($arg: $argtype),*) -> $ret $body
let val = inner($($arg),*).await;
let mut cache = $cachename.lock().unwrap();
$crate::Cached::cache_set(&mut *cache, key, val.clone());
val
}
};
}
#[macro_export]
macro_rules! cached_result {
($cachename:ident : $cachetype:ty = $cacheinstance:expr ;
fn $name:ident ($($arg:ident : $argtype:ty),*) -> $ret:ty = $body:expr) => {
static $cachename: $crate::once_cell::sync::Lazy<::std::sync::Mutex<$cachetype>>
= $crate::once_cell::sync::Lazy::new(|| ::std::sync::Mutex::new($cacheinstance));
#[allow(unused_parens)]
pub fn $name($($arg: $argtype),*) -> $ret {
let key = ($($arg.clone()),*);
{
let mut cache = $cachename.lock().unwrap();
let res = $crate::Cached::cache_get(&mut *cache, &key);
if let Some(res) = res { return Ok(res.clone()); }
}
let ret : $ret = (||$body)();
let val = ret?;
let mut cache = $cachename.lock().unwrap();
$crate::Cached::cache_set(&mut *cache, key, val.clone());
Ok(val)
}
};
($cachename:ident : $cachetype:ty = $cacheinstance:expr ;
async fn $name:ident ($($arg:ident : $argtype:ty),*) -> $ret:ty = $body:expr) => {
static $cachename: $crate::once_cell::sync::Lazy<::std::sync::Mutex<$cachetype>>
= $crate::once_cell::sync::Lazy::new(|| ::std::sync::Mutex::new($cacheinstance));
#[allow(unused_parens)]
pub async fn $name($($arg: $argtype),*) -> $ret {
let key = ($($arg.clone()),*);
{
let mut cache = $cachename.lock().unwrap();
let res = $crate::Cached::cache_get(&mut *cache, &key);
if let Some(res) = res { return Ok(res.clone()); }
}
async fn inner($($arg: $argtype),*) -> $ret $body
let val = inner($($arg),*).await?;
let mut cache = $cachename.lock().unwrap();
$crate::Cached::cache_set(&mut *cache, key, val.clone());
Ok(val)
}
};
}
#[macro_export]
macro_rules! cached_key_result {
($cachename:ident : $cachetype:ty = $cacheinstance:expr ;
Key = $key:expr;
fn $name:ident ($($arg:ident : $argtype:ty),*) -> $ret:ty = $body:expr) => {
static $cachename: $crate::once_cell::sync::Lazy<::std::sync::Mutex<$cachetype>>
= $crate::once_cell::sync::Lazy::new(|| ::std::sync::Mutex::new($cacheinstance));
#[allow(unused_parens)]
pub fn $name($($arg: $argtype),*) -> $ret {
let key = $key;
{
let mut cache = $cachename.lock().unwrap();
let res = $crate::Cached::cache_get(&mut *cache, &key);
if let Some(res) = res { return Ok(res.clone()); }
}
let ret : $ret = (||$body)();
let val = ret?;
let mut cache = $cachename.lock().unwrap();
$crate::Cached::cache_set(&mut *cache, key, val.clone());
Ok(val)
}
};
($cachename:ident : $cachetype:ty = $cacheinstance:expr ;
Key = $key:expr;
async fn $name:ident ($($arg:ident : $argtype:ty),*) -> $ret:ty = $body:expr) => {
static $cachename: $crate::once_cell::sync::Lazy<::std::sync::Mutex<$cachetype>>
= $crate::once_cell::sync::Lazy::new(|| ::std::sync::Mutex::new($cacheinstance));
#[allow(unused_parens)]
pub async fn $name($($arg: $argtype),*) -> $ret {
let key = $key;
{
let mut cache = $cachename.lock().unwrap();
let res = $crate::Cached::cache_get(&mut *cache, &key);
if let Some(res) = res { return Ok(res.clone()); }
}
async fn inner($($arg: $argtype),*) -> $ret $body
let val = inner($($arg),*).await?;
let mut cache = $cachename.lock().unwrap();
$crate::Cached::cache_set(&mut *cache, key, val.clone());
Ok(val)
}
};
}
#[macro_export]
macro_rules! cached_control {
($cachename:ident : $cachetype:ty = $cacheinstance:expr ;
Key = $key:expr;
PostGet($cached_value:ident) = $post_get:expr;
PostExec($body_value:ident) = $post_exec:expr;
Set($set_value:ident) = $pre_set:expr;
Return($ret_value:ident) = $return:expr;
fn $name:ident ($($arg:ident : $argtype:ty),*) -> $ret:ty = $body:expr) => {
static $cachename: $crate::once_cell::sync::Lazy<::std::sync::Mutex<$cachetype>>
= $crate::once_cell::sync::Lazy::new(|| ::std::sync::Mutex::new($cacheinstance));
#[allow(unused_parens)]
pub fn $name($($arg: $argtype),*) -> $ret {
let key = $key;
{
let mut cache = $cachename.lock().unwrap();
let res = $crate::Cached::cache_get(&mut *cache, &key);
if let Some($cached_value) = res {
$post_get
}
}
let $body_value = (||$body)();
let $set_value = $post_exec;
let mut cache = $cachename.lock().unwrap();
$crate::Cached::cache_set(&mut *cache, key, $pre_set);
let $ret_value = $set_value;
$return
}
};
($cachename:ident : $cachetype:ty = $cacheinstance:expr ;
Key = $key:expr;
PostGet($cached_value:ident) = $post_get:expr;
PostExec($body_value:ident) = $post_exec:expr;
Set($set_value:ident) = $pre_set:expr;
Return($ret_value:ident) = $return:expr;
async fn $name:ident ($($arg:ident : $argtype:ty),*) -> $ret:ty = $body:expr) => {
static $cachename: $crate::once_cell::sync::Lazy<::std::sync::Mutex<$cachetype>>
= $crate::once_cell::sync::Lazy::new(|| ::std::sync::Mutex::new($cacheinstance));
#[allow(unused_parens)]
pub async fn $name($($arg: $argtype),*) -> $ret {
let key = $key;
{
let mut cache = $cachename.lock().unwrap();
let res = $crate::Cached::cache_get(&mut *cache, &key);
if let Some($cached_value) = res {
$post_get
}
}
async fn inner($($arg: $argtype),*) -> $ret $body
let $body_value = inner($($arg),*).await?;
let $set_value = $post_exec;
let mut cache = $cachename.lock().unwrap();
$crate::Cached::cache_set(&mut *cache, key, $pre_set);
let $ret_value = $set_value;
$return
}
};
}