I'm making an API wrapper using Rust. I have a method products::all() which returns a vector of Product structs. This works fine but wouldn't it be best to cache the data once they call the method and if they do it again check whether the data was cached to return the vector? It takes a good while to get the data and certain functions like of_category() calls all() to filter it.
I'm thinking of implementing a mutable static variable that will have all the Product structs, something like
// products.rs
static mut products: Vec<Product> = Vec::new();
pub async fn all() -> Vec<Product> {
if !products.len() > 10 {
products = // load data
}
return products;
}