如何在MVC5中启用捆绑缓存

时间:2018-11-02 08:54:37

标签: c# asp.net-mvc asp.net-mvc-5 bundle

我在MVC项目中创建了样式和脚本包,如下所示。现在,我还需要为包启用缓存。有什么方法可以控制这些分发包的缓存持续时间?

   using namespace std::chrono;
   template<typename Duration , typename Clock_t = high_resolution_clock, 
   typename Time_type = time_point<Clock_t, typename Duration> , typename 
   Time_pointer = Time_type* >
 class DateRange {
   using Time_type_t = typename Time_type::duration;
 public:
   DateRange(Time_type_t start, Time_type_t end) :
    m_begin(start),
    m_end(end)
   {

   }
   DateRange(Time_type_t end):
    m_begin(Clock_t::now())

   {

   }
   Time_pointer begin(){
    return &m_begin;
   }
   Time_pointer end() {
    return &m_end;
    }

    Time_pointer operator++(){
    present +=Duration(1);
    return present_point;
   }

 Time_type operator*(){
    return present;
  }

private:
Time_type m_begin;
Time_type m_end;
Time_type present;
Time_pointer present_point = &present;
Clock_t l_clock;
};
int main()
{
DateRange<seconds> dr(40s);
dr.operator++();
    std::cout << (*dr).time_since_epoch().count();
 }

3 个答案:

答案 0 :(得分:2)

好的,因此其他答案似乎并不完全正确。您可以通过更改web.config中的“ staticContent”元素来影响浏览器尝试缓存这些文件的方式。 (假设您使用的是IIS7或更高版本)

这会影响将使用哪些缓存标头的静态内容(例如图片/脚本)。例如您可以通过设置以下内容来提供最大年龄的标头和日期:

<staticContent>
  <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
</staticContent>

有关可设置内容的更多信息,请参见以下文档:

https://docs.microsoft.com/en-us/iis/configuration/system.webserver/staticcontent/clientcache

答案 1 :(得分:0)

正如已经说过的那样,在大多数情况下(即使不是全部),捆绑源都将缓存​​在服务器和浏览器中(但这将取决于您使用的实际服务器软件)。

  

“但是当我重新加载页面时,它将每次都呈现”

捆绑本身不渲染,它们被解析(它们是JS / CSS资源)。之后,将解析的结果用于呈现页面。

JS / CSS解析步骤是否有效完成(有无解析结果的“缓存”)取决于浏览器的实现,并且不可能通过服务器端编程,脚本或设置来影响。

答案 2 :(得分:-1)

默认情况下,捆绑软件由浏览器缓存,除非您手动将其禁用。在服务器端您无能为力。

相关问题