Asp.Net 4.0 新特性之 使用自定义OutputCache Provider

2013/10/18 已被3247人阅读

当前位置:首页>新闻中心>技术关注【关闭】

在Asp.Net 4.0 的web.config文件中添加了关于缓存的配置节,如下所示:

 
   
   
     
       
         
       
     
   

我们可以在Web.config中配置自定义的OutputCacheProvider,并将自定义Provider指定为默认的Provider。

1.自定义OutputCacheProvider需要实现System.Web.Cacheing. OutputCacheProvider抽象类,网上有很多例子都用文件缓存做例子。这个例子太俗了,我写了一个新的例子,在设置的缓存时间小于指定阀值时,缓存到HttpRuntime.Cache中,否则缓存到文件中,如下代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Caching; 
using System.Xml.Serialization; 
using System.IO; 
using System.Runtime.Serialization.Formatters.Binary; 
  
namespace OutputCacheTest.Caching 
{ 
    ///  
    /// OutputCache精灵,如果缓存时间小于设置时间时则缓存到内存,否则缓存到文件 
    ///  
    public class SmartOutputCacheProvider : OutputCacheProvider 
    { 
        private const string KEY_PREFIX = "__outputCache_"; 
  
        ///  
        /// 初始化SmartOutputCacheProvider,读取配置文件中配置的MemoryCacheLimit和FileCacheRoot的值 
        ///  
        /// provider名字 
        /// 配置 
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) 
        { 
            string memoryLimit = config["memoryCacheLimit"]; 
            if (memoryLimit == null) 
            { 
                MemoryCacheLimit = new TimeSpan(0, 30, 0); 
            } 
            else
            { 
                MemoryCacheLimit = TimeSpan.Parse(memoryLimit); 
            } 
  
            string fileCacheRoot = config["fileCa oot"]; 
            if (string.IsNullOrEmpty(fileCacheRoot)) 
            { 
                fileCacheRoot = AppDomain.CurrentDomain.BaseDirectory + "cache\\"; 
            } 
            this.FileCacheRoot = fileCacheRoot; 
            base.Initialize(name, config); 
        } 
  
        ///  
        /// 添加缓存 
        ///  
        /// 缓存的键,key的值是有asp.net内部生成的 
        /// 缓存的对象 
        /// 过期时间 
        /// 返回缓存值 
        public override object Add(string key, object entry, DateTime utcExpiry) 
        { 
            Set(key, entry, utcExpiry); 
            return entry; 
        } 
  
        ///  
        /// 处理缓存键值,防止在文件缓存时出现文件路径不允许的字符 
        ///  
        /// 缓存键 
        /// 处理后的键 
        private string ProcessKey(string key) 
        { 
            return KEY_PREFIX + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key, "MD5"); 
        } 
  
        ///  
        /// 返回缓存文件的物理路径 
        ///  
        /// 处理后的键 
        /// 物理路径 
        private string GetFilePath(string processedKey) 
        { 
            return Path.Combine(FileCacheRoot, processedKey + ".data"); 
        } 
  
        ///  
        /// 获得缓存值,如果在HttpRuntime.Cache中有则直接读取内存中的值,否则从文件读取 
        ///  
        /// 缓存键 
        /// 缓存值 
        public override object Get(string key) 
        { 
            string processedKey = ProcessKey(key); 
  
            CacheDataWithExpiryTimeUtc result = HttpRuntime.Cache[processedKey] as CacheDataWithExpiryTimeUtc; 
            if (result == null) 
            { 
                string path = GetFilePath(processedKey); 
                if (!File.Exists(path)) 
                    return null; 
  
                using (FileStream file = File.OpenRead(path)) 
                { 
                    var formatter = new BinaryFormatter(); 
                    result = (CacheDataWithExpiryTimeUtc)formatter.Deserialize(file); 
                } 
            } 
  
            if (result == null || result.ExpiryTimeUtc <= DateTime.UtcNow) 
            { 
                Remove(key); 
                return null; 
            } 
            return result.Data; 
        } 
  
        ///  
        /// 根据键移除缓存 
        ///  
        /// 缓存键 
        public override void Remove(string key) 
        { 
            string processedKey = ProcessKey(key); 
            HttpRuntime.Cache.Remove(processedKey); 
            string path = GetFilePath(processedKey); 
            if (!File.Exists(path)) 
                File. (path); 
        } 
  
        ///  
        /// 设置缓存 
        ///  
        /// 缓存键 
        /// 缓存内容 
        /// 过期时间 
        public override void Set(string key, object entry, DateTime utcExpiry) 
        { 
            TimeSpan ts = utcExpiry - DateTime.UtcNow; 
            string processedKey = ProcessKey(key); 
  
            CacheDataWithExpiryTimeUtc cacheItem = new CacheDataWithExpiryTimeUtc 
            { 
                Data = entry, 
                ExpiryTimeUtc = utcExpiry 
            }; 
  
            if (ts <= MemoryCacheLimit) 
            { 
                HttpRuntime.Cache. (processedKey, cacheItem, null, utcExpiry.ToLocalTime(), TimeSpan.Zero); 
            } 
            else
            { 
                string cacheFilePath = GetFilePath(processedKey); 
                  
                using (var fs = new FileStream(cacheFilePath,FileMode.OpenOrCreate,FileAccess.ReadWrite)) 
                { 
                    var formatter = new BinaryFormatter(); 
                    formatter.Serialize(fs, cacheItem); 
                } 
            } 
        } 
  
        ///  
        /// 如果缓存设定的时间超过此值则缓存到文件中,否则在HttpRuntime.Cache中做缓存 
        ///  
        [XmlAttribute("memoryCacheLimit")] 
        public TimeSpan MemoryCacheLimit { get; set; } 
  
  
        ///  
        /// 文件缓存的根目录,可以指定任何可访问目录 
        ///  
        [XmlAttribute("fileCacheRoot")] 
        public string FileCacheRoot { get; set; } 
    } 
  
    ///  
    /// 对缓存数据和缓存的过期时间的封装 
    ///  
    [Serializable] 
    internal class CacheDataWithExpiryTimeUtc 
    { 
        public object Data { get; set; } 
  
        public DateTime ExpiryTimeUtc { get; set; } 
    } 
} 

2.如何使用自定义的OutputCacheProvider

  1)在配置文件中做配置,将自定义的实现作为默认输出缓存支持,请看文章开始的配置
  2)在UserControl中指定使用Provider的名字,改名字在web.config中定义,例如

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="IamUserControl.ascx.cs" Inherits="OutputCacheTest.IamUserControl" %> 
<%@ OutputCache Duration="3000" ProviderName="AspNetInternalProvider" VaryByParam="None" %> 


  需要注意的是,只能在UserControl中指定Provider的名字,在Page的生明中是不允许的,在Page中默认情况会使用web.config中配置的defaultProvider,但是我们可以通过3)中介绍的方法给不同的页面使用不同的OutputCacheProvider实现。

  3)在Global.asax文件中重写GetOutputCacheProviderName(HttpContext context)方法,根据context返回不同的实现名字,如下例子

public override string GetOutputCacheProviderName(HttpContext context) 
{ 
    if (context.Request.Path.StartsWith("/default.aspx",StringComparison.CurrentCultureIgnoreCase)) 
    { 
        return "AspNetInternalProvider"; 
    } 
              
    return base.GetOutputCacheProviderName(context); 
} 

预约洽谈

多一份方案,多一个参考,预约我们的商务面对面沟通,总有益处 周一至周五,9:00-18:00    咨询热线:021-3100 6558 24小时热线:15900965330

我要预约
电话咨询 在线沟通 QQ咨询 微信聊天