1

我目前正在尝试通过 Web.config 配置 ASP.net Web 应用程序,以在特定文件夹中托管 GWT WebApp。我已经设法在 system.Webserver/staticContent 部分中为 .manifest 文件扩展名配置 mimeMap,但是,我坚持使用 clientCache。我想添加一个缓存规则,以便带有“.nocache”的文件。提供以下标头:

"Expires", "Sat, 21 Jan 2012 12:12:02 GMT" (today -1);
"Pragma", "no-cache"
"Cache-control", "no-cache, no-store, must-revalidate"

任何人都知道如何在 IIS 7+ 中执行此操作?

4

3 回答 3

1

文件时间戳在 IIS 中自动检查,浏览器总是根据时间戳向服务器请求更新文件,因此 .nocache。文件在 IIS 中不需要任何特殊的东西。

但是,如果您希望浏览器缓存 .cache。文件,然后以下 HttpModule 将缓存到期日期设置为从现在起 30 天,用于以 .cache.js 或 .cache.html(或任何扩展名)结尾的文件。浏览器甚至不会请求这些文件的更新版本。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CacheModulePlayground
{
    public class CacheModule : IHttpModule
    {
        private HttpApplication _context;


        public void Init(HttpApplication context)
        {
            _context = context;
            context.PreSendRequestHeaders += context_PreSendRequestHeaders;
        }

        void context_PreSendRequestHeaders(object sender, EventArgs e)
        {

            if (_context.Response.StatusCode == 200 || _context.Response.StatusCode == 304)
            {
                var path = _context.Request.Path;

                var dotPos = path.LastIndexOf('.');
                if (dotPos > 5)
                {
                    var preExt = path.Substring(dotPos - 6, 7);
                    if (preExt == ".cache.")
                    {
                        _context.Response.Cache.SetExpires(DateTime.UtcNow.Add(TimeSpan.FromDays(30)));
                    }
                }

            }

        }


        public void Dispose()
        {
            _context = null;
        }
    }
}

用于此的 web.config 是:

<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
  <system.webServer>
    <modules>
      <add name="cacheExtension" type="CacheModulePlayground.CacheModule"/>
    </modules>
  </system.webServer>
</configuration>
于 2013-03-17T15:36:33.243 回答
0

我最终创建了一个自定义 httphandler 来处理对路径.nocache 的所有请求。使用类似于此处描述的解决方案:

防止脚本以编程方式被缓存

于 2012-01-30T13:15:52.850 回答
0
  1. 在 GwtCacheHttpModuleImpl.cs 文件中创建一个 HTTP 模块类

    using System;
    using System.Web;
    using System.Text.RegularExpressions;
    
    namespace YourNamespace
    {
        /// <summary>
        /// Classe GwtCacheHttpModuleImpl
        /// 
        /// Permet de mettre en cache pour un an ou pas du tout les fichiers générés par GWT
        /// </summary>
        public class GwtCacheHttpModuleImpl : IHttpModule
        {
    
            private HttpApplication _context;
    
            private static String GWT_FILE_EXTENSIONS_REGEX_STRING = "\\.(js|html|png|bmp|jpg|gif|htm|css|ttf|svg|woff|txt)$";
    
            private static Regex GWT_CACHE_OR_NO_CACHE_FILE_REGEX = new Regex(".*\\.(no|)cache" + GWT_FILE_EXTENSIONS_REGEX_STRING);
            private static Regex GWT_CACHE_FILE_REGEX = new Regex(".*\\.cache" + GWT_FILE_EXTENSIONS_REGEX_STRING);
    
            #region IHttpModule Membres
    
            public void Dispose()
            {
                _context = null;
            }
    
            public void Init(HttpApplication context)
            {
                context.PreSendRequestHeaders += context_PreSendRequestHeaders;
                _context = context;
            }
    
            #endregion
    
            private void context_PreSendRequestHeaders(object sender, EventArgs e)
            {
                int responseStatusCode = _context.Response.StatusCode;
    
                switch (responseStatusCode)
                {
                    case 200:
                    case 304:
                        // Réponse gérée
                        break;
                    default:
                        // Réponse non gérée
                        return;
                } /* end..switch */
    
    
                String requestPath = _context.Request.Path;
    
                if (!GWT_CACHE_OR_NO_CACHE_FILE_REGEX.IsMatch(requestPath))
                {
                    // Fichier non géré
                    return;
                }
    
                HttpCachePolicy cachePolicy = _context.Response.Cache;
    
                if (GWT_CACHE_FILE_REGEX.IsMatch(requestPath))
                {
                    // Fichier à mettre en cache
                    cachePolicy.SetExpires(DateTime.UtcNow.Add(TimeSpan.FromDays(365))); /* now plus 1 year */              
                }
                else
                {
                    // Fichier à ne pas mettre en cache
                    cachePolicy.SetExpires(DateTime.UtcNow); /* ExpiresDefault "now" */
                    cachePolicy.SetMaxAge(TimeSpan.Zero); /* max-age=0 */
                    cachePolicy.SetCacheability(HttpCacheability.Public); /* Cache-Control public */
                    cachePolicy.SetRevalidation(HttpCacheRevalidation.AllCaches); /* must-revalidate */
                }
    
            }
        }
    }
    
  2. 在 Web.Config 文件中引用您的 HTTP 模块:

  3. 通过 ISAPI 模块处理 GWT 文件扩展

您应该通过 IIS UI(在我的例子中为 IIS 5.x 和 .NET 3.5)配置您的应用程序。您可以添加其他 GWT 文件扩展名,例如 png、css、...

a)处理 .js 扩展名

可执行文件:c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll

扩展名:.js

限制为:GET,HEAD

b)处理 .html 扩展名

可执行文件:c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll

扩展名:.html

限制为:GET,HEAD

参考:Apache 服务器的 GWT 完美缓存

于 2014-07-25T13:22:32.070 回答