谁会asp.net中的cache应用
给点代码啊

解决方案 »

  1.   

    using System;
    using System.Web;
    using System.Web.Caching;namespace AMS.Common
    {
    public interface ICache
    {
    object this[string key]
    {
    get;
    set;
    } int Count
    {
    get;
    }
    /// <summary>
    /// 向Cache中插入键、值、和依赖
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="val">值</param>
    /// <param name="fileDependency">依赖</param>
    void Insert(string key, object val, string fileDependency); /// <summary>
    /// 向Cache中插入键、值、和过期时间
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="val">值</param>
    /// <param name="seconds"></param>
    void Insert(string key, object val, TimeSpan span); void Remove(string key);
    } /// <summary>
    /// 封装ASP.NET的Cache
    /// </summary>
    public class ASPCacheAdapter : ICache
    {
    private static ICache _cache = null; private ASPCacheAdapter()
    {

    } public static ICache GetCache()
    {
    if (_cache == null)
    {
    _cache = new ASPCacheAdapter();
    }
    return _cache;
    } public object this[string key]
    { get
    {
    return HttpRuntime.Cache[key];
    }
    set
    {
    HttpRuntime.Cache[key] = value;
    }
    } public int Count
    {
    get
    {
    return HttpRuntime.Cache.Count;
    }
    }
    public void Insert(string key, object val, string fileDependency)
    {
    HttpRuntime.Cache.Insert(key, 
    val, 
    new CacheDependency(fileDependency));
    } public void Insert(string key, object val, TimeSpan span)
    {
    HttpRuntime.Cache.Insert(key, 
    val, 
    null, 
    DateTime.Now.Add(span), 
    Cache.NoSlidingExpiration);
    } public void Remove(string key)
    {
    HttpRuntime.Cache.Remove(key);
    }
    }
    public class CacheFactory
    {
    public static ICache Create()
    {
    return ASPCacheAdapter.GetCache();
    }
    }
    }