Cache System
Potato framework provides two cache systems: OnceCache (single request cache) and SessionCache (session-level cache).
OnceCache
Used to pass data within a single HTTP request lifecycle, shared between preprocess, handler, and postprocess.
Basic Usage
#[potato::preprocess]
async fn auth_preprocess(req: &mut HttpRequest, cache: &mut OnceCache) {
cache.set("user_id", 12345u32);
}
#[potato::http_get("/profile")]
#[potato::preprocess(auth_preprocess)]
async fn get_profile(cache: &mut OnceCache) -> HttpResponse {
let user_id: u32 = cache.get("user_id").copied().unwrap_or(0);
HttpResponse::text(format!("User: {}", user_id))
}
Common Methods
cache.get::<T>(name)- Get value (returnsOption<&T>)cache.get_or_default::<T>(name, default)- Get value or defaultcache.get_mut::<T>(name)- Get mutable referencecache.set::<T>(name, value)- Set valuecache.remove::<T>(name)- Remove and return valuecache.contains_key::<T>(name)- Check if key exists
SessionCache
Used to maintain user session data across requests, based on JWT token for user authentication and data isolation.
Basic Usage
// Login endpoint to issue token
#[potato::http_post("/login")]
async fn login(req: &mut HttpRequest) -> HttpResponse {
let user_id = 12345i64;
let token = SessionCache::generate_token(user_id, Duration::from_secs(3600)).unwrap();
HttpResponse::json(serde_json::json!({ "token": token }))
}
// Use SessionCache directly, macro automatically handles token validation
#[potato::http_get("/profile")]
async fn get_profile(cache: &mut SessionCache) -> HttpResponse {
let count: u32 = cache.get("visits").unwrap_or(0);
cache.set("visits", count + 1);
HttpResponse::text(format!("Visits: {}", count + 1))
}
Cookie Support
SessionCache automatically handles HTTP Cookies with full attribute support:
- Calling
get_cookieautomatically reads the request'sCookieheader - Calling
set_cookie/set_cookie_with_builderautomatically sets the response'sSet-Cookieheader - Supports all standard attributes: path, domain, expires, Secure, HttpOnly, SameSite, etc.
Simple Usage
#[potato::http_get("/cookie/simple")]
async fn cookie_simple(cache: &mut SessionCache) -> HttpResponse {
// Read request Cookie
let session_id = cache.get_cookie("session_id");
// Set simple Cookie (default path is "/")
cache.set_cookie("theme", "dark");
HttpResponse::text("ok")
}
Full Attribute Configuration
use potato::CookieBuilder;
use chrono::Utc;
#[potato::http_get("/cookie/full")]
async fn cookie_full(cache: &mut SessionCache) -> HttpResponse {
let expires = Utc::now().timestamp() + 3600; // Expires in 1 hour
let cookie = CookieBuilder::new("user_token", "abc123")
.path("/api") // Path
.domain(".example.com") // Domain
.expires(expires) // Expiration time (Unix timestamp)
.max_age(3600) // Max age in seconds
.secure(true) // HTTPS only
.http_only(true) // Prevent JavaScript access
.same_site("Strict"); // SameSite policy
cache.set_cookie_with_builder(cookie);
HttpResponse::text("ok")
}
Deleting Cookies
#[potato::http_get("/cookie/delete")]
async fn cookie_delete(cache: &mut SessionCache) -> HttpResponse {
// Simple delete
cache.remove_cookie("session_id");
// Delete with domain
cache.remove_cookie_with_domain("user_token", ".example.com");
HttpResponse::text("cookies deleted")
}
Common Methods
Session Management
SessionCache::set_jwt_secret(secret)- Set JWT secret (global)SessionCache::generate_token(user_id, duration)- Generate tokenSessionCache::invalidate(user_id)- Invalidate session
Data Storage
cache.get::<T>(key)- Get value (requires Clone)cache.set::<T>(key, value)- Set valuecache.with_get::<T>(key, |v| ...)- Read and processcache.with_mut::<T>(key, |v| ...)- Mutable reference processing
Cookie Operations
cache.get_cookie(name)- Read request cookie (returnsOption<String>)cache.set_cookie(name, value)- Set simple cookie (default path "/")cache.set_cookie_with_builder(cookie)- Set full attribute cookie using CookieBuildercache.remove_cookie(name)- Remove cookiecache.remove_cookie_with_domain(name, domain)- Remove cookie with domain
CookieBuilder Methods
CookieBuilder::new(name, value)- Create cookie.path(path)- Set path.domain(domain)- Set domain.expires(timestamp)- Set expiration time (Unix timestamp, seconds).max_age(seconds)- Set max age in seconds.secure(bool)- Set Secure flag (HTTPS only).http_only(bool)- Set HttpOnly flag (prevent JS access).same_site(policy)- Set SameSite policy ("Strict"/"Lax"/"None")
Using OnceCache and SessionCache Together
#[potato::http_get("/data")]
async fn get_data(once: &mut OnceCache, session: &mut SessionCache) -> HttpResponse {
once.set("req_id", "abc"); // Single request
session.set("user", "john"); // Cross-request persistence
HttpResponse::text("ok")
}