简体   繁体   中英

Caching is not applying in asp.net mvc

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 different ways, but not applied.

I have added new Class name CacheFilterAttribute

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");
    }

}

and then in controller Action method

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

  }

It is not applied. then i tried through config file.

<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <add name="Cache1Hour" duration="3600" varyByParam="none"/>
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>

still not applied.

Please tell me possible ways to load images fast.

There are two misconceptions in your question:

  1. When you decorate an MVC action, like Home , the result is that, if the page has been previously requested, it is cached and the server will not run the action again, but will return directly the cached result of the previous execution.
  2. The images are usually static images served directly from the server, which require no execution at all, andhave nothing to do with the MVC caching.

And finally, the images are cached in the browser itself. When the browser requests an image, it gets it from the server, and usually caches it. If it needs it again, instead of asking it from the server it will use it local cache.

If you don't change neither the default browser configuration, nor the default server configuration, there is nothing that stops the browser from caching images. But be aware that the browser can be instructed not to cache the images if the response from the server includes some headers. As I told, this won't happen by default.

Finally, on many browser, when you open the developer tools (debugger, console, HTML examiner...) the caching is automatically disabled. You can change that configuration.To avoid this to happen, you cna monitor the network traffic using the free Fiddler tool.

EDIT If the images are loaded slowly that's perhaps because you're using very high resolution images. If this is the case, use any tool to reduce there resolution, and they'll load more quickly. If the images are compressed, you can use different levels of compression to further reduce their byte size, without changing the resolution. There are many tools available to do this, even online tools. Google "Image optimizer". There are also free software like GIMP which can be used to optimize iamges .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM