博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在Asp.Net里使用动态缓存文件

Posted on 2007-04-18 09:14  faib  阅读(1297)  评论(2编辑  收藏  举报
  在Asp.Net里,可以使用CacheDependency可以在程序里自动的创建一个缓存文件,比如验证码控件中,图片文件就是动态的添加的。

  建立缓存文件信息的类:
 1    public class FileCacheInfo
 2    {
 3        private string m_FileName;
 4        private string m_ContentType;
 5        private object m_Content;
 6        private DateTime m_DateEntered = DateTime.Now;
 7
 8        //文件名
 9        public string FileName
10        {
11            get{return m_FileName;}
12            set{m_FileName = value;}
13        }

14
15        //文件类型,如js为javascript/text
16        public string ContentType
17        {
18            get{return m_ContentType;}
19            set{m_ContentType = value;}
20        }

21
22        //文件内容
23        public object Content
24        {
25            get{return m_Content;}
26            set{m_Content = value;}
27        }

28
29        //缓存写入日期
30        public DateTime DateEntered
31        {
32            get{return m_DateEntered;}
33            set{m_DateEntered = value;}
34        }

35
36        public FileCacheInfo()
37        {
38        }

39
40        public FileCacheInfo(string FileName, string ContentType, object Content)
41        {
42            this.FileName = FileName;
43            this.ContentType = ContentType;
44            this.Content = Content;
45        }

46    }

  建立实现IHttpHandler的类:
 1    public class FileCacheHandler : IHttpHandler
 2    {
 3        //注册一个文件到缓存中
 4        public static void RegisterFileCache(FileCacheInfo FileInfo)
 5        {
 6            FileCacheInfo ci = new FileCacheInfo(FileInfo.FileName, FileInfo.ContentType, FileInfo.Content);
 7            string FilePath = System.Web.HttpContext.Current.Server.MapPath(FileInfo.FileName);
 8            CacheDependency cd = new CacheDependency(FilePath);
 9            System.Web.HttpContext.Current.Cache.Insert(FilePath, ci, cd);
10        }

11
12        public void ProcessRequest(HttpContext Context)
13        {
14            string absFilePath = Context.Request.PhysicalPath;
15            
16            if(absFilePath.IndexOf("~\\"> -1)
17            {
18                absFilePath = absFilePath.Replace("~""").Replace("\\\\""\\");
19            }

20
21            FileCacheInfo ci = (FileCacheInfo)Context.Cache[absFilePath];
22            if(ci != null)
23            {
24                if(Context.Request.Headers["If-Modified-Since"!= null)
25                {
26                    try
27                    {
28                        DateTime date = DateTime.Parse(Context.Request.Headers["If-Modified-Since"]);
29
30                        if(ci.DateEntered.ToString() == date.ToString())
31                        {
32                            Context.Response.StatusCode = 304;
33                            Context.Response.StatusDescription = "Not Modified";
34                            Context.Response.End();
35                            return;
36                        }

37                    }

38                    catch(Exception){}
39                }

40                else
41                {
42                    Context.Response.AddHeader("If-Modified-Since", ci.DateEntered.ToString());
43                }

44            }

45            else
46            {
47                ci = UpdateFileCache(Context, absFilePath);
48            }

49
50            Context.Response.Cache.SetLastModified(ci.DateEntered);
51            Context.Response.ContentType = ci.ContentType;
52            if(ci.Content.GetType().Name == "Byte[]")
53            {
54                Context.Response.BinaryWrite((byte[])ci.Content);
55            }

56            else
57            {
58                Context.Response.Write(ci.Content);
59            }

60            Context.Response.End();
61        }

62
63        public bool IsReusable
64        {
65            get{return true;}
66        }

67
68        private static FileCacheInfo UpdateFileCache(HttpContext Context, string FilePath)
69        {
70            FileCacheInfo ci = new FileCacheInfo(FilePath, ""null);
71            CacheDependency cd = new CacheDependency(FilePath);
72            Context.Cache.Insert(FilePath, ci, cd);
73            return ci;
74        }

75    }

  在web.config中配置<httpHandlers>节:
 <httpHandlers>
    
<add verb="POST,GET" path="*.fbs.ashx" type="FaibClass.Common.Web.FileCacheHandler, FaibClass.Common" />
 
</httpHandlers>

  在页面的Page_Load中注册一个缓存文件,写入文件内容,这样在页面端就可以使用<script src=a.fbs.ashx></script>了:
FileCacheHandler.RegisterFileCache(new FileCacheInfo("a.fbs.ashx""javascript/text""alert('faib studio');"));