Curriculum
Caching in ASP.NET Core is one of the most effective techniques for improving application performance, reducing database load, decreasing response times, and enhancing user experience. Modern applications often receive thousands or even millions of requests. Without proper caching strategies, servers and databases can become overloaded, leading to slow response times and increased infrastructure costs.
Understanding Caching in ASP.NET Core is essential because caching is widely used in enterprise applications, e-commerce platforms, banking systems, social media platforms, SaaS products, cloud applications, and microservices architectures.
Caching is the process of storing frequently accessed data in a temporary storage location for faster retrieval.
Workflow:
Request Data
↓
Store In Cache
↓
Future Requests
↓
Retrieve From Cache
Cached data can be accessed much faster than querying a database repeatedly.
Without caching:
Database Query
↓
Data Retrieval
↓
Response
This process repeats for every request.
With caching:
Cache Lookup
↓
Data Found
↓
Response
Response times improve significantly.
Library Example:
Without Cache:
Search Book
↓
Storage Room
↓
Retrieve Book
With Cache:
Popular Books
↓
Nearby Shelf
↓
Quick Access
Caching works similarly in software systems.
Faster Responses
Reduced Database Load
Better Scalability
Improved User Experience
Lower Infrastructure Costs
These benefits make caching essential.
ASP.NET Core supports:
In-Memory Cache
Distributed Cache
Response Cache
Output Cache
Each type serves different use cases.
Definition:
Data Stored In Application Memory
Characteristics:
Fast
Simple
Single Server
Ideal for small to medium applications.
Application
↓
Memory Cache
↓
Data
Data is stored directly in server memory.
Program.cs:
builder.Services
.AddMemoryCache();
This enables in-memory caching.
Example:
private readonly
IMemoryCache cache;
The cache service is injected through dependency injection.
Example:
cache.Set(
"Students",
students);
Data is stored using a key.
Example:
cache.TryGetValue(
"Students",
out var students);
The application checks whether data already exists.
Example:
Data Found In Cache
Result:
Fast Response
No database query is required.
Example:
Data Not Found
Result:
Query Database
The cache is then updated.
Request
↓
Cache Check
↓
Cache Hit?
↓
Yes → Return Data
No → Database → Cache → Return Data
This is the most common caching pattern.
Cached data should not live forever.
Types:
Absolute Expiration
Sliding Expiration
Expiration keeps data fresh.
Example:
Expire After
30 Minutes
The item expires regardless of usage.
Example:
Expire After
30 Minutes Of Inactivity
Frequently used items remain cached longer.
Example:
cache.Set(
"Students",
students,
TimeSpan
.FromMinutes(30));
The cache automatically expires after 30 minutes.
Definition:
Cache Shared Across Multiple Servers
Useful in cloud and load-balanced environments.
Problem:
Server A Cache
≠
Server B Cache
Distributed caching solves this issue.
Application 1
↓
Shared Cache
↑
Application 2
All servers access the same cache.
Examples:
Redis
SQL Server
NCache
Redis is the most popular choice.
Redis is:
In-Memory Data Store
Benefits:
Fast
Scalable
Distributed
Widely used in enterprise systems.
Purpose:
Cache Entire HTTP Responses
Useful for APIs and public content.
Request
↓
Generate Response
↓
Store Response
↓
Future Requests
↓
Return Cached Response
Reduces server workload.
Program.cs:
builder.Services
.AddResponseCaching();
Registers response caching services.
Example:
app.UseResponseCaching();
Enables response caching in the pipeline.
Example:
[ResponseCache(
Duration = 60)]
The response is cached for 60 seconds.
Output Caching is a modern caching solution introduced in newer ASP.NET Core versions.
Purpose:
Cache Generated Output
Benefits:
Better Control
Improved Performance
Suitable for high-performance applications.
Examples:
Product Catalogs
Course Lists
Reference Data
Configuration Data
Frequently Accessed Records
Ideal candidates change infrequently.
Examples:
Sensitive Information
Real-Time Financial Data
User Passwords
Frequently Changing Data
Caching unsuitable data can create issues.
Cache invalidation means:
Remove Old Data
Purpose:
Maintain Data Accuracy
Stale data should be refreshed.
Update Product
↓
Remove Cache
↓
Reload Data
Ensures users see current information.
E-Commerce Website:
Cached Data:
Products
Categories
Offers
Frequently accessed content loads quickly.
Cached Data:
Branch Information
Currency Rates
Reference Data
Sensitive account information should not be cached improperly.
Cached Data:
Doctor Schedules
Departments
Reference Records
Frequently accessed information benefits from caching.
Cache Usage:
API Responses
Configuration Data
Shared Reference Data
Improves service communication efficiency.
Users receive data more quickly.
Fewer database queries are executed.
Supports larger workloads.
Applications feel more responsive.
Reduces resource consumption.
These benefits make caching essential in modern systems.
Can create security risks.
May produce stale data.
Causes outdated information.
Wastes memory resources.
Can affect application performance.
Caching stores frequently accessed data for faster retrieval.
IMemoryCache provides in-memory caching within an ASP.NET Core application.
A cache shared across multiple application servers.
Redis is a high-performance distributed in-memory data store.
Response Caching stores entire HTTP responses for reuse.
Caching improves performance, scalability, and user experience.
Caching stores data temporarily to improve performance and reduce resource usage.
IMemoryCache is ASP.NET Core’s built-in in-memory caching solution.
Redis is a distributed in-memory data store commonly used for caching.
Response Caching stores generated HTTP responses and serves them to future requests.
Expiration ensures outdated data is removed and refreshed.
It improves performance, scalability, user experience, and infrastructure efficiency.
WhatsApp us