Skip to content

Commit 2a6a791

Browse files
committed
feat: implement output caching policy for search results with dynamic filtering
1 parent 1dd837e commit 2a6a791

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

Web/Pages/Tips/Index.cshtml.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Web.Pages.Tips;
88

9-
[OutputCache(Duration = 21600, Tags = new[] { "tips", "content" }, VaryByQueryKeys = new[] { "category", "tag", "search", "difficulty", "page" })]
9+
[OutputCache(PolicyName = "SearchResults")]
1010
public class IndexModel : BasePageModel
1111
{
1212
private readonly IContentService _contentService;
@@ -36,6 +36,14 @@ public IndexModel(IContentService contentService, ILogger<IndexModel> logger)
3636

3737
public async Task<IActionResult> OnGetAsync()
3838
{
39+
// For search results, prevent aggressive browser caching
40+
if (!string.IsNullOrEmpty(Search) || !string.IsNullOrEmpty(Category) ||
41+
!string.IsNullOrEmpty(Tag) || !string.IsNullOrEmpty(Difficulty) || PageNumber > 1)
42+
{
43+
Response.Headers.CacheControl = "no-cache, must-revalidate";
44+
Response.Headers.Pragma = "no-cache";
45+
}
46+
3947
try
4048
{
4149
var request = new TipSearchRequest

Web/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@
111111
.Expire(TimeSpan.FromDays(3)) // Cache tips for 3 days
112112
.Tag("outputcache", "tips", "content")); // Add tags for better organization
113113

114+
// Policy for search results - shorter cache for dynamic filtering
115+
options.AddPolicy("SearchResults", builder =>
116+
builder.Cache()
117+
.SetVaryByHost(true)
118+
.SetVaryByQuery("category", "tag", "search", "difficulty", "pageNumber")
119+
.Expire(TimeSpan.FromMinutes(5)) // Cache search results for 5 minutes
120+
.Tag("outputcache", "search", "tips")); // Add tags for better organization
121+
114122
// Policy for frequently updated content - extended to 6 hours minimum
115123
options.AddPolicy("DynamicContent", builder =>
116124
builder.Cache()

0 commit comments

Comments
 (0)