jaemk / cached

Rust cache structures and easy function memoization
MIT License
1.58k stars 95 forks source link

Ignore some arguments #67

Closed omid closed 3 years ago

omid commented 3 years ago

Is there any way of ignoring some of the arguments?

Imagine I have a method to get one user from the database. And I would like to cache the user. So this method may need two params, DB connection pool and the user ID. In my case, I do not care about connection pool uniqueness and would like to ignore it!

pub fn get_user(conn: &PgConnection, user_id: &Uuid) -> MyResult<User>

My suggestion us to have something like:

#[cached(time = 600, ignore = "conn,another_param")]
pub fn get_user(conn: &PgConnection, user_id: &Uuid, another_param: String) -> MyResult<User>
jaemk commented 3 years ago

You can use the type, create, and convert options to get what you're looking for (also result = true to cache only Ok results):

#[cached(
    result = true,
    type = "TimedCache<uuid::Uuid, String>",
    create = "{ TimedCache::with_lifespan(600) }",
    convert = r#"{ user_id.clone() }"#
)]
pub fn get_user(conn: &PgConnection, user_id: &uuid::Uuid) -> MyResult<User> {
    ...
    Ok(user)
}
omid commented 3 years ago

Wow, thanks. It is more complex than I thought.