zoukankan      html  css  js  c++  java
  • Handler合并js、css文件并缓存在服务器

    <%@ WebHandler Language="C#" class="mergeHandler" %>

    using System; using System.Web; using System.IO; using System.Text;

    public class mergeHandler : IHttpHandler {

        private const string CacheKeyFormt = "CacheKey_{0}_{1}_";     public void ProcessRequest(HttpContext context)     {         MergeFile(context);     }

        public bool IsReusable     {         get         {             return false;         }     }     public void MergeFile(HttpContext context)     {         HttpRequest request = context.Request;         HttpResponse response = context.Response;

            string cachekey = string.Empty;         string content = string.Empty;

            string type = request.QueryString["type"] ?? "";         string setName = request.QueryString["setName"] ?? "";         string fileName = request.QueryString["fileName"] ?? "";

            cachekey = string.Format(CacheKeyFormt, type, setName);

            //HttpRuntime.Cache.Remove(cachekey);

            if (type != "" && setName != "")         {             string path = string.Empty;             if (type.Equals("js"))             {                 response.ContentType = "text/javascript";                 path = context.Server.MapPath("/js/");             }             else if (type.Equals("css"))             {                 response.ContentType = "text/css";                 path = context.Server.MapPath("/css/");             }             if (HttpRuntime.Cache[cachekey] != null)             {                 content = HttpRuntime.Cache[cachekey].ToString();             }             else             {                 StringBuilder sb = new StringBuilder();                 string[] files = Directory.GetFiles(path, "*." + type);

                    if (fileName.IndexOf(",") > -1)                 {                     string[] array = fileName.Split(',');                     for (int i = 0; i < array.Length; i++)                     {                         foreach (string fname in files)                         {                             if (File.Exists(fname) && fname.IndexOf(array[i] + "." + type) > -1)                             {                                 string readstr = File.ReadAllText(fname, Encoding.UTF8);                                 sb.Append(readstr);                             }                         }                     }                 }                 else                 {                     foreach (string fname in files)                     {                         if (File.Exists(fname) && fname.IndexOf(fileName + "." + type) > -1)                         {                             string readstr = File.ReadAllText(fname, Encoding.UTF8);                             sb.Append(readstr);                         }                     }                 }                 content = sb.ToString();                 HttpRuntime.Cache.Insert(cachekey, content, null, DateTime.Now.AddDays(1), System.Web.Caching.Cache.NoSlidingExpiration);             }         }         response.Write(content);     } }

  • 相关阅读:
    IntelliJ IDEA自动补全变量名称和属性名称的快捷键
    Redis客户端 Spring Data Redis(未完)
    用画小狗的方法来解释Java中的值传递
    Java -- Arrays.asList()方法
    有趣的IntegerCache
    字符串使用点滴
    字符串拼接+和concat的区别
    在一个Excel单元格内输入多行内容
    JSTL1.2学习总结
    Android ico
  • 原文地址:https://www.cnblogs.com/ajun/p/3062692.html
Copyright © 2011-2022 走看看