Replies: 1 comment 4 replies
-
As far as I know, The #[macro_use]
extern crate rocket;
use std::time::Duration;
use cached::{proc_macro::cached, TimedCache};
#[rocket::launch]
fn launch() -> _ {
rocket::build()
.mount("/", routes![long_to_compute])
}
#[get("/")]
#[cached(
type = "TimedCache<(), &'static str>",
create = "{ TimedCache::with_lifespan(100) }",
)]
fn long_to_compute() -> &'static str {
std::thread::sleep(Duration::from_secs(2));
"Long calculation is done"
} It supports various kinds of caches, works with async functions, and you can obviously have the parameters of the cached function (here, it would be the various Of course, Rocket will still have to write the HTTP headers for every response, but if your handler does a database call for instance, the gain will still be noticeable. |
Beta Was this translation helpful? Give feedback.
-
I already made an Issue for this, but couldn't find how to flag it as a "Suggestion" but is OutputCaching currently available in Rocket?
I mean, why focus on speeding up the page generation when you don't need to. This is especially useful on high-traffic websites where most of the pages can be retrieved from the cache.
For example: let's say you have a mostly static page with the time on it, which is obviously generated by some code. But the server only needs to generate a new page every second (the time displayed only shows hours:minutes:seconds) and this could have a huge impact if thousands of users are hitting your site. If a thousand users are hitting your site every second 999 of them will get the page from the cache!
Beta Was this translation helpful? Give feedback.
All reactions