欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net中缓存类DataCache(依赖文件缓存和时间缓存,或两者)

程序员文章站 2022-03-24 22:17:59
[csharp]  ///   /// 数据缓存类   ///   public class...
[csharp] 

/// <summary>  

/// 数据缓存类  

/// </summary>  

public class datacache  

{  

 

    #region 缓存依赖文件web.config  

    private static string _webconfigfile = string.empty;  

    /// <summary>  

    /// 缓存依赖文件web.config  

    /// </summary>  

    public static string webconfigfile  

    {  

        get  

        {  

            if (_webconfigfile == string.empty) _webconfigfile = httpcontext.current.server.mappath("/web.config");  

            return _webconfigfile;  

        }  

    }   

    #endregion  

 

    #region 删除缓存  

    /// <summary>  

    /// 删除缓存  

    /// </summary>  

    /// <param name="cachekey">键</param>  

    public static void deletecache(string cachekey)  

    {  

        httpruntime.cache.remove(cachekey);  

    }   

    #endregion  

 

    #region 获取缓存  

    /// <summary>  

    /// 获取缓存  

    /// </summary>  

    /// <param name="cachekey">键</param>  

    /// <returns></returns>  

    public static object getcache(string cachekey)  

    {  

        return httpruntime.cache[cachekey];  

    }   

    #endregion  

 

    #region 简单的插入缓存  

    /// <summary>  

    /// 简单的插入缓存  

    /// </summary>  

    /// <param name="cachekey">键</param>  

    /// <param name="objobject">数据</param>  

    public static void setcache(string cachekey, object objobject)  

    {  

        httpruntime.cache.insert(cachekey, objobject);  

    }   

    #endregion  

 

    #region 有过期时间的插入缓存数据  

    /// <summary>  

    /// 有过期时间的插入缓存数据  

    /// </summary>  

    /// <param name="cachekey">键</param>  

    /// <param name="objobject">数据</param>  

    /// <param name="absoluteexpiration">过期时间</param>  

    /// <param name="slidingexpiration">可调度参数,传null就是禁用可调度</param>  

    public static void setcache(string cachekey, object objobject, datetime absoluteexpiration, timespan slidingexpiration)  

    {  

        if (slidingexpiration == null) slidingexpiration = cache.noslidingexpiration;  

        httpruntime.cache.insert(cachekey, objobject, null, absoluteexpiration, slidingexpiration);  

    }   

    #endregion  

 

    #region 插入缓存数据,指定缓存多少秒  

    /// <summary>  

    /// 插入缓存数据,指定缓存多少秒  

    /// </summary>  

    /// <param name="cachekey">缓存的键</param>  

    /// <param name="objobject">缓存的数据</param>  

    /// <param name="seconds">缓存秒数</param>  

    /// <param name="slidingexpiration">传null就是禁用可调度过期</param>  

    public static void setcachesecond(string cachekey, object objobject, int seconds, timespan slidingexpiration)  

    {  

        datetime absoluteexpiration = datetime.now.addseconds(seconds);  

        if (slidingexpiration == null) slidingexpiration = cache.noslidingexpiration;  

        httpruntime.cache.insert(cachekey, objobject, null, absoluteexpiration, slidingexpiration);  

    }   

    #endregion  

 

    #region 依赖文件的缓存,文件没改不会过期  

    /// <summary>  

    /// 依赖文件的缓存,文件没改不会过期  

    /// </summary>  

    /// <param name="cachekey">键</param>  

    /// <param name="objobject">数据</param>  

    /// <param name="depfilename">依赖文件,可调用 datacache 里的变量</param>  

    public static void setcachedepfile(string cachekey, object objobject, string depfilename)  

    {  

        //缓存依赖对象  

        system.web.caching.cachedependency dep = new system.web.caching.cachedependency(depfilename);  

        system.web.caching.cache objcache = httpruntime.cache;  

        objcache.insert(  

            cachekey,  

            objobject,  

            dep,  

            system.web.caching.cache.noabsoluteexpiration, //从不过期  

            system.web.caching.cache.noslidingexpiration, //禁用可调过期  

            system.web.caching.cacheitempriority.default,  

            null);  

    }   

    #endregion  

  

}