Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i need to implement Cache for Images in my web application , because my home page is taking time for loading images . i tried catching in differnt ways, but not applying.

i added new class

C#
public class CacheFilterAttribute : ActionFilterAttribute
    {
        public int Duration
        {
        get;
        set;
        }

        public CacheFilterAttribute()
        {
        Duration = 60;
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
        if (Duration <= 0) return;

        HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
        TimeSpan cacheDuration = TimeSpan.FromSeconds(Duration);

        cache.SetCacheability(HttpCacheability.Public);
        cache.SetExpires(DateTime.Now.Add(cacheDuration));
        cache.SetMaxAge(cacheDuration);
        cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
        }

    }


after in controller Action method

SQL
[CacheFilter(Duration = 60000)]
 [OutputCache(Duration = 1800)]  //, VaryByParam = "*")
  public ActionResult Home()
 {

 }


not working , i tried through web.config also, but still no use.
How can i do caching ? i just need caching for images
Posted

What I think that you need to use is the ASP.NET's WebCache.

http://www.asp.net/web-pages/tutorials/performance-and-traffic/15-caching-to-improve-the-performance-of-your-website[^]

You can use it in your MVC application too since it is a part of ASP.NET. You can easily add caching and extract from the cache too.
 
Share this answer
 
Try outputcache(location=client)
 
Share this answer
 
Comments
dorababu407 25-Aug-14 13:26pm    
IS there anything to add in config file ?
can u provide complete code snipppt
Rajiv Gogoi 26-Aug-14 0:35am    
[OutputCache(Duration=3600, VaryByParam="none", Location=OutputCacheLocation.Client)]
public string GetName()
{
return "Hi " + User.Identity.Name;
}
http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/improving-performance-with-output-caching-cs

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900