如何在C#asp.net MVC4中应用缓存 [英] How to apply Caching in C# asp.net MVC4

查看:283
本文介绍了如何在C#asp.net MVC4中应用缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要在我的网络应用程序中实现图像缓存,因为我的主页需要时间来加载图像。我尝试以不同的方式捕捉,但没有申请。



i添加新课程



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

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

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

 }





无法正常工作,我也试过了web.config,但仍然没用。

如何进行缓存?我只需要缓存图像



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

推荐答案

尝试outputcache(location = client)
Try outputcache(location=client)


我认为你需要使用的是ASP.NET的WebCache。



http://www.asp.net/web-pages/tutorials/performance-and-traffic/15-caching-to-improve-the-你网站的表现 [ ^ ]



您也可以在MVC应用程序中使用它因为它是ASP.NET的一部分。您也可以轻松地从缓存中添加缓存和提取。
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.


这篇关于如何在C#asp.net MVC4中应用缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆