Take the community feedback survey now.

Custom Response Cache Attribute

Vote:
 

HI,

I have written code for Response Cache custom cache attribute and decorate in controller page wise.

Everything is working fine values of cache reflecting property in network cache-control. Like cache duration value is for Home page i set 60 sec. then i would expect till to 60 sec page should display from cache but it is not happening.

Can anyone suggest how can i test this functionality?

Custom cache attribute

{
    public enum CacheType
    {
        ProductPage,
        HomePage,
    }
 
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public class CustomCacheAttribute : Attribute, IActionFilter
    {
        private readonly CacheType _cacheType;
        public CustomCacheAttribute(CacheType cacheType)
        {
            _cacheType = cacheType;
        }
       
        public void OnActionExecuted(ActionExecutedContext context)
        {
            var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
 
            var page = contentRepository
               .GetChildren<ConfigPageModel>(ContentReference.RootPage)
               .FirstOrDefault();
 
            if (page is not null)
            {
                int? duration = _cacheType switch
                {
                    CacheType.ProductPage => page.ProductPageCacheDuration,
                    CacheType.HomePage => page.HomePagePageCacheDuration,
                 };
 
                if (duration.HasValue && duration.Value > 0)
                {
                    var headers = context.HttpContext.Response.GetTypedHeaders();
                    headers.CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue
                    {
                        Public = true,
                        MaxAge = TimeSpan.FromSeconds(duration.Value)
                    };
                    headers.Expires = DateTimeOffset.UtcNow.AddSeconds(duration.Value);
                    //  Location = ResponseCacheLocation.Any;
 
                    context.HttpContext.Response.Headers["Pragma"] = "cache";
                    context.HttpContext.Response.Headers["Vary"] = "Accept-Encoding";
                }
            }
 
        }
 
        public void OnActionExecuting(ActionExecutingContext context)
        {
 
        }
    }
}

Custorm Attribute Decorate in controller on index method

[CustomCache(CacheType.HomePage)]


Thanks,

Deepmala

#341056
Edited, Nov 21, 2025 12:15
Vote:
 

Hi

Caching is complex but I must ask what the goal is? To have 60 seconds static cache serving correct headers?

What I typically recommend is to use built in attributes in .net. In your scenarion it would probably look like this.

[OutputCache(PolicyName = "ProductPageCacheServer")]
[ResponseCache(CacheProfileName = "ProductPageCacheClient")]
public async Task<IActionResult> Index(ProductPageType currentPage)
{
    // ...
}

Or do you have specific requirements for the code you've posted?

 

#341081
Nov 22, 2025 17:59
Vote:
 

Hi Eric,

The issue is that the website is hosted on Azure DXP with only 2 GB of memory available. Because of this limited memory, the application is experiencing higher load and performance issues. Optimizely has suggested enabling caching to reduce memory consumption and improve response times.

We need to determine:

  1. Which type of caching should be enabled,

  2. How to configure it properly, and

  3. What impact it will have on the existing website.

Our requirement is to set the cache duration dynamically on a per-page basis through a configuration page in Optimizely. Because the  response cache attribute only accepts a static duration value, it will not work for our scenario where the duration must be dynamic.

Thanks,

Deepmala

#341082
Edited, Nov 24, 2025 7:58
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.