Attribute Macro cache

Source
#[cache]
Expand description

#[cache] - Transparent Redis-based caching for async functions.

This macro wraps an async function to automatically cache its result in Redis. It checks for a cached value before executing the function. If a cached result is found, it is deserialized and returned directly. Otherwise, the function runs normally and its result is stored in Redis.

§Syntax

#[cache("key_pattern", expire = 600)]

§Attributes

  • "key_pattern": (required) A cache key expression using standard format!() syntax.
  • expire = <integer>: (optional) Time-to-live in seconds for the cached value.

§Function Requirements

  • Must be an async fn
  • Can return either a Result<T, E> or a plain value T
  • The return type must implement serde::Serialize and serde::Deserialize
  • Generics, attributes, and visibility will be preserved

§Example

use spring_macros::cache;

#[derive(serde::Serialize, serde::Deserialize)]
struct User {
    id: u64,
    name: String,
}

struct MyError;

#[cache("user:{user_id}", expire = 600)]
async fn get_user(user_id: u64) -> Result<User, MyError> {
    // Fetch user from database
    unimplemented!("do something")
}