Skip to content

Commit 8d08224

Browse files
committed
feat: extend cache durations for improved performance across various endpoints and pages
1 parent 6f64147 commit 8d08224

12 files changed

Lines changed: 38 additions & 48 deletions

Web/Extensions/EndpointExtensions.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ public static class EndpointExtensions
1212
{ public static void MapSitemapEndpoint(this WebApplication app)
1313
{
1414
app.MapGet("/sitemap.xml", async (HttpContext context, IContentService contentService) =>
15-
{
16-
// Enable output caching for sitemap
15+
{ // Enable output caching for sitemap - extended to 6 hours
1716
context.Response.GetTypedHeaders().CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue
1817
{
1918
Public = true,
20-
MaxAge = TimeSpan.FromHours(1)
19+
MaxAge = TimeSpan.FromHours(6)
2120
};
2221
// Define the XML namespace for the sitemap
2322
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
@@ -111,21 +110,19 @@ public static class EndpointExtensions
111110
context.Response.ContentType = MediaTypeNames.Application.Xml;
112111

113112
// Return the XML document as a string
114-
return sitemap.ToString();
115-
})
113+
return sitemap.ToString(); })
116114
.CacheOutput(policy => policy
117-
.Expire(TimeSpan.FromHours(1))
115+
.Expire(TimeSpan.FromHours(6))
118116
.SetVaryByHost(true)
119117
.Tag("sitemap"));
120118
} public static void MapRssFeedEndpoint(this WebApplication app)
121119
{
122120
app.MapGet("/feed.rss", async (HttpContext context, IContentService contentService) =>
123-
{
124-
// Enable output caching for RSS feed
121+
{ // Enable output caching for RSS feed - extended to 2 hours for better performance
125122
context.Response.GetTypedHeaders().CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue
126123
{
127124
Public = true,
128-
MaxAge = TimeSpan.FromMinutes(30)
125+
MaxAge = TimeSpan.FromHours(2)
129126
};// Create the RSS feed XML document
130127
var rss = new XDocument(
131128
new XDeclaration("1.0", "utf-8", null),
@@ -186,10 +183,9 @@ public static class EndpointExtensions
186183
}
187184

188185
// Return the XML document as a string
189-
return rss.ToString();
190-
})
186+
return rss.ToString(); })
191187
.CacheOutput(policy => policy
192-
.Expire(TimeSpan.FromMinutes(30))
188+
.Expire(TimeSpan.FromHours(2))
193189
.SetVaryByHost(true)
194190
.Tag("rss"));
195191
}

Web/Pages/About.cshtml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ namespace Web.Pages;
44

55
public class AboutModel : BasePageModel
66
{
7-
// Override cache duration for static pages - cache for 1 hour
8-
protected override int CacheDurationSeconds => 3600;
7+
// Override cache duration for static pages - cache for 3 days
8+
protected override int CacheDurationSeconds => 259200; // 3 days
99

1010
public void OnGet()
1111
{

Web/Pages/BasePageModel.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ namespace Web.Pages;
77

88
[OutputCache]
99
public abstract class BasePageModel : PageModel
10-
{
11-
/// <summary>
12-
/// The default output cache duration for dynamic pages
10+
{ /// <summary>
11+
/// The default output cache duration for dynamic pages - extended to 6 hours
1312
/// </summary>
14-
protected virtual int CacheDurationSeconds => 300; // 5 minutes by default
13+
protected virtual int CacheDurationSeconds => 21600; // 6 hours by default
1514

1615
/// <summary>
1716
/// Whether the page allows caching by default

Web/Pages/Contribute.cshtml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ namespace Web.Pages;
44

55
public class ContributeModel : BasePageModel
66
{
7-
// Override cache duration for static content - cache for 1 hour
8-
protected override int CacheDurationSeconds => 3600;
7+
// Override cache duration for static content - cache for 3 days
8+
protected override int CacheDurationSeconds => 259200; // 3 days
99

1010
public void OnGet()
1111
{

Web/Pages/Index.cshtml.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ public class IndexModel : BasePageModel
99
{
1010
private readonly ILogger<IndexModel> _logger;
1111
private readonly IContentService _contentService;
12-
13-
// Override cache duration for home page - cache for 10 minutes
14-
protected override int CacheDurationSeconds => 600;
12+
// Use default cache duration for home page (6 hours) - good balance for fresh content
13+
// protected override int CacheDurationSeconds => base.CacheDurationSeconds; // 6 hours default
1514

1615
public List<TipModel> RecentTips { get; set; } = new();
1716
public List<string> Categories { get; set; } = new();

Web/Pages/Privacy.cshtml.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ public class PrivacyModel : BasePageModel
77
{
88
private readonly ILogger<PrivacyModel> _logger;
99
private readonly IConfiguration _configuration;
10-
11-
// Override cache duration for static pages - cache for 1 hour
12-
protected override int CacheDurationSeconds => 3600;
10+
// Override cache duration for static pages - cache for 3 days
11+
protected override int CacheDurationSeconds => 259200; // 3 days
1312

1413
public PrivacyModel(ILogger<PrivacyModel> logger, IConfiguration configuration)
1514
{

Web/Pages/Terms.cshtml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ namespace Web.Pages;
44

55
public class TermsModel : BasePageModel
66
{
7-
// Override cache duration for static pages - cache for 1 hour
8-
protected override int CacheDurationSeconds => 3600;
7+
// Override cache duration for static pages - cache for 3 days
8+
protected override int CacheDurationSeconds => 259200; // 3 days
99

1010
public void OnGet()
1111
{

Web/Pages/Tips/Category.cshtml.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ public class CategoryModel : BasePageModel
99
{
1010
private readonly IContentService _contentService;
1111
private readonly ILogger<CategoryModel> _logger;
12-
13-
// Override cache duration for category pages - cache for 5 minutes
14-
protected override int CacheDurationSeconds => 300;
12+
// Use default cache duration for category pages (6 hours) - categories change infrequently
13+
// protected override int CacheDurationSeconds => base.CacheDurationSeconds; // 6 hours default
1514

1615
public CategoryModel(IContentService contentService, ILogger<CategoryModel> logger)
1716
{

Web/Pages/Tips/Details.cshtml.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ namespace Web.Pages.Tips;
1010
public class DetailsModel : BasePageModel
1111
{
1212
private readonly IContentService _contentService;
13-
private readonly ILogger<DetailsModel> _logger;
14-
// Override cache duration for individual tips - cache for 24 hours since tips are relatively static content
15-
protected override int CacheDurationSeconds => 86400; // 24 hours
13+
private readonly ILogger<DetailsModel> _logger; // Override cache duration for individual tips - cache for 3 days since tips are static content
14+
protected override int CacheDurationSeconds => 259200; // 3 days
1615

1716
public DetailsModel(IContentService contentService, ILogger<DetailsModel> logger)
1817
{

Web/Pages/Tips/Index.cshtml.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ namespace Web.Pages.Tips;
88
public class IndexModel : BasePageModel
99
{
1010
private readonly IContentService _contentService;
11-
private readonly ILogger<IndexModel> _logger;
12-
// Override cache duration for tips list - cache for 1 hour since new tips are added infrequently
13-
protected override int CacheDurationSeconds => 3600; // 1 hour
11+
private readonly ILogger<IndexModel> _logger; // Use default cache duration for tips list (6 hours) since new tips are added infrequently
12+
// protected override int CacheDurationSeconds => base.CacheDurationSeconds; // 6 hours default
1413

1514
public IndexModel(IContentService contentService, ILogger<IndexModel> logger)
1615
{

0 commit comments

Comments
 (0)