Caching Workflow
A step-by-step workflow for adding response caching through cache policy and interceptor registration.
Use this flow when you want predictable response caching:
- enable caching features
- define a cache policy
- register the cache interceptor
- run the app
- verify cache behavior on real routes
Step 1: enable the features
Section titled “Step 1: enable the features”nestforge = { version = "1", features = ["cache", "redis"] }Step 2: define a policy
Section titled “Step 2: define a policy”Start with a small policy:
#[derive(Default, Clone)]struct UsersCachePolicy;
impl nestforge::CachePolicy for UsersCachePolicy { type Store = nestforge::InMemoryRedisStore;}The policy controls what gets cached and how cache keys and TTL rules are derived.
Step 3: register the interceptor
Section titled “Step 3: register the interceptor”Attach the interceptor at bootstrap:
NestForgeFactory::<AppModule>::create()? .use_interceptor::<nestforge::CacheInterceptor<UsersCachePolicy>>();That makes caching part of the request pipeline instead of a hidden behavior inside the controller.
Step 4: verify default behavior
Section titled “Step 4: verify default behavior”The built-in defaults are conservative. First confirm that:
GETroutes are cacheable- non-
GETroutes are not cached - the request URI becomes the cache key
- only
200 OKresponses are stored
Step 5: customize only when needed
Section titled “Step 5: customize only when needed”Once the default behavior is confirmed, customize the policy for:
- custom keys
- custom TTL
- cache invalidation semantics
- route-specific rules
What to read next
Section titled “What to read next”For the reference page, see Caching.