Middleware that provides caching functionality.

Cache middleware can provide caching function for StatusCode, Headers, Body in Response. For the content that has been cached, when processing the request next time, Cache middleware will directly send the content cached in memory to the client.

Note that this plugin will not cache Response whose Body is a ResBody::Stream. If applied to a Response of this type, the Cache will not process these requests and will not cause error.

Main Features

  • CacheIssuer provides an abstraction over the assigned cache keys. RequestIssuer is an implementation of it that defines which parts of the requested URL and the requested Method to generate a cache key. You can also define your own The logic of cache key generation. The cache key does not have to be a string type, any type that satisfies the constraints of Hash + Eq + Send + Sync + 'static can be used as a key.

  • CacheStore provides access to data. MemoryStore is a built-in moka-based memory cache implementation. You can also define your own implementation.

  • Cache is a structure that implements Handler, and there is a skipper field inside, which can be specified to skip certain requests that do not need to be cached. By default, MethodSkipper will be used to skip all request except Method::GET.

    Internal implementation sample code:

    impl<S, I> Cache<S, I> {
      pub fn new(store: S, issuer: I) -> Self {
          let skipper = MethodSkipper::new().skip_all().skip_get(false);
          Cache {
              skipper: Box::new(skipper),

Config Cargo.toml

salvo = { version = "*", features = ["cache"] }

Sample Code

use std::time::Duration;

use salvo::prelude::*;
use salvo::writer::Text;
use salvo::cache::{Cache, MemoryStore, RequestIssuer};
use time::OffsetDateTime;

async fn home() -> Text<&'static str> {
async fn short() -> String {
    format!("Hello World, my birth time is {}", OffsetDateTime::now_utc())
async fn long() -> String {
    format!("Hello World, my birth time is {}", OffsetDateTime::now_utc())

async fn main() {

    tracing::info!("Listening on");
    let short_cache = Cache::new(
    let long_cache = Cache::new(
    let router = Router::new()

static HOME_HTML: &str = r#"
<!DOCTYPE html>
        <title>Cache Example</title>
        <h2>Cache Example</h2>
            This examples shows how to use cache middleware. 
            <a href="/short" target="_blank">Cache 5 seconds</a>
            <a href="/long" target="_blank">Cache 1 minute</a>