using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

using System.Web.Caching;

using System.IO;

namespace Tools.Web

{

    /// <summary> 

    /// 网页中的缓存类,使用示例: 

    /// object obj = DataCache.GetCache("file1",depfile); 

    ///if (obj == null) 

    ///{ 

    ///   string txt = "缓存内容";//从数据库或文件读取到的内容 

    ///   DataCache.SetCacheDepFile("file1", txt, depfile); 

    /// } 

    /// else 

    /// { 

    ///     string txt=obj.ToString(); 

    /// } 

    /// </summary> 

    ///

    public partial class DataCache

    {

        #region 文件路径web.config

        private static string _webconfigfile = string.Empty;

        /// <summary> 

        /// 文件路径web.config 

        /// </summary> 

        public static string webconfigfile

        {

            get

            {

                if (string.IsNullOrEmpty(_webconfigfile)) _webconfigfile = HttpContext.Current.Server.MapPath("/web.config");

                return _webconfigfile;

            }

        }

        #endregion

#region 文件路径App_Data/ShopConfig.config

        private static string _shopconfigfile = string.Empty;

        /// <summary> 

        /// 文件路径App_Data/ShopConfig.config 

        /// </summary> 

        public static string shopconfigfile

        {

            get

            {

                if (string.IsNullOrEmpty(_shopconfigfile)) _shopconfigfile = HttpContext.Current.Server.MapPath("/App_Data/ShopConfig.config");

                return _shopconfigfile;

            }

        }

        #endregion

#region 文件路径App_Data/SiteConfig.config

        private static string _siteconfigfile = string.Empty;

        /// <summary> 

        /// 文件路径App_Data/SiteConfig.config 

        /// </summary> 

        public static string siteconfigfile

        {

            get

            {

                if (string.IsNullOrEmpty(_siteconfigfile)) _siteconfigfile = HttpContext.Current.Server.MapPath("/App_Data/SiteConfig.config");

                return _siteconfigfile;

            }

        }

        #endregion

#region 文件路径App_Data/Template.config

        private static string _templateconfigfile = string.Empty;

        /// <summary> 

        /// 文件路径App_Data/Template.config 

        /// </summary> 

        public static string templateconfigfile

        {

            get

            {

                if (string.IsNullOrEmpty(_templateconfigfile)) _templateconfigfile = HttpContext.Current.Server.MapPath("/App_Data/Template.config");

                return _templateconfigfile;

            }

        }

        #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)

        {

            object obj_time = HttpRuntime.Cache[CacheKey + "_time"];

            object obj_cache = HttpRuntime.Cache[CacheKey];

            if (obj_time != null && obj_cache != null)

            {

                if (Convert.ToDateTime(obj_time) < DateTime.Now)

                {

                    DeleteCache(CacheKey);

                    DeleteCache(CacheKey + "_time");

                    return null;

                }

                else return obj_cache;

            }

            else

            {

                DeleteCache(CacheKey);

                DeleteCache(CacheKey + "_time");

                return null;

            }

        }

        #endregion

#region 获取缓存,依赖文件

        /// <summary> 

        /// 获取缓存,依赖文件 

        /// </summary> 

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

        /// <param name="depFile">依赖的文件</param> 

        /// <returns></returns> 

        public static object GetCache(string CacheKey, string depFile)

        {

            object obj_time = HttpRuntime.Cache[CacheKey + "_time"];

            object obj_cache = HttpRuntime.Cache[CacheKey];

            if (File.Exists(depFile))

            {

                FileInfo fi = new FileInfo(depFile);

if (obj_time != null && obj_cache != null)

                {

                    if (Convert.ToDateTime(obj_time) != fi.LastWriteTime)

                    {

                        DeleteCache(CacheKey);

                        DeleteCache(CacheKey + "_time");

                        return null;

                    }

                    else return obj_cache;

                }

                else

                {

                    DeleteCache(CacheKey);

                    DeleteCache(CacheKey + "_time");

                    return null;

                }

            }

            else

            {

                throw new Exception("文件(" + depFile + ")不存在!");

            }

        }

        #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);

            HttpRuntime.Cache.Insert(CacheKey + "_time", absoluteExpiration, 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);

            HttpRuntime.Cache.Insert(CacheKey + "_time", absoluteExpiration, 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);

            DateTime absoluteExpiration = System.Web.Caching.Cache.NoAbsoluteExpiration;

            TimeSpan slidingExpiration = System.Web.Caching.Cache.NoSlidingExpiration;

            System.Web.Caching.Cache objCache = HttpRuntime.Cache;

            objCache.Insert(

                CacheKey,

                objObject,

                dep,

                System.Web.Caching.Cache.NoAbsoluteExpiration, //从不过期 

                slidingExpiration, //禁用可调过期 

                System.Web.Caching.CacheItemPriority.Default,

                null);

            if (File.Exists(depfilename))

            {

                FileInfo fi = new FileInfo(depfilename);

                DateTime lastWriteTime = fi.LastWriteTime;

                HttpRuntime.Cache.Insert(CacheKey + "_time", lastWriteTime, null, absoluteExpiration, slidingExpiration);//存储文件最后修改时间 

            }

}

        #endregion

    }

}

asp.net 缓存公共类的更多相关文章

  1. ASP.NET中常用的几个李天平开源公共类LTP.Common,Maticsoft.DBUtility,LtpPageControl

    ASP.NET中常用的几个开源公共类: LTP.Common.dll: 通用函数类库     源码下载Maticsoft.DBUtility.dll 数据访问类库组件     源码下载LtpPageC ...

  2. ASP.NET中常用的几个李天平开源公共类LTP.Common,Maticsoft.DBUtility,LtpPageControl (转)

    ASP.NET中常用的几个开源公共类: LTP.Common.dll: 通用函数类库     源码下载Maticsoft.DBUtility.dll 数据访问类库组件     源码下载LtpPageC ...

  3. Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类

    Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类 转载自http://www.trinea.cn/android/android-common-lib/ 介绍总结的一 ...

  4. asp.net—缓存

    1.页面缓存 要实现页面输出缓存,只要将一条 OutputCache 指令添加到页面即可. <%@ OutputCache CacheProfile=" " NoStore= ...

  5. Asp.Net缓存(2)

    缓存页的多个版本 ASP.NET 允许在输出缓存中缓存同一页的多个版本.输出缓存可能会因下列因素而异: 初始请求 (HTTP GET) 中的查询字符串. 回发时传递的控制值(HTTP POST 值). ...

  6. Asp.Net缓存(1)

    知其根本,方能应用.MSDN上的缓存讲解.先看原来讲解. Asp.Net缓存概述 通常,应用程序可以将那些频繁访问的数据,以及那些需要大量处理时间来创建的数据存储在内存中,从而提高性能. 在这些情况下 ...

  7. ASP.NET缓存策略经验谈

    要提升ASP.NET应用程序的性能,最简单.最有效的方式就是使用内建的缓存引擎.虽然也能构建自己的缓存,但由于缓存引擎已提供了如此多的功能,所以完全不必如此麻烦.在很大程度上,ASP.NET开发者在W ...

  8. (转)ASP.NET缓存全解析6:数据库缓存依赖

    ASP.NET缓存全解析文章索引 ASP.NET缓存全解析1:缓存的概述 ASP.NET缓存全解析2:页面输出缓存 ASP.NET缓存全解析3:页面局部缓存 ASP.NET缓存全解析4:应用程序数据缓 ...

  9. 自己封装的C#操作redis公共类

    关于C#操作redis公共类,网上有很多版本,每个版本我都看了,发觉还是不够完美,都存在一个问题,只能操作单一的缓存数据库 redis指令支持上,这里可以自己去扩展,下面分享下我近期封装的一个redi ...

随机推荐

  1. Flask-RESTful(转载)

    Flask-RESTful 是一个 Flask 扩展,它添加了快速构建 REST APIs 的支持.它当然也是一个能够跟你现有的ORM/库协同工作的轻量级的扩展.Flask-RESTful 鼓励以最小 ...

  2. C语言使用memcpy函数实现两个数间任意位置的复制操作

    c和c++使用的内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中. 用法:void *memcpy(void *dest ...

  3. webpack操作基础

    webpack 是一个前端加载/打包工具,根据模块的依赖关系进行静态分析,并依根据规则生成对应的静态资源

  4. CSLA框架的codesmith模板改造

    一直有关注CSLA框架,最近闲来无事,折腾了下,在最新的r3054版本基础上修改了一些东西,以备自己用,有兴趣的园友可以下载共同研究 1.添加了默认的授权规则 如果是列表对象则生成列表权限,User的 ...

  5. iBase4J部署总结

    iBase4J部署总结 序言 最近看到个分布式框架,只有一个字:好.所以部署起来看看.开始的时候说实话遇到了点困难.去码云上看了下,貌似想得到指导要加入一个群,而且需要收费的,反正闲来无事,索性自己搞 ...

  6. ASP.NET-ActionResutlt

    @RenderPage("Page_part1"); 上面的这种写法是错误的应该是 @RenderPage("Page_part1.cshtml"); // 要 ...

  7. POJ 1496 POJ 1850 组合计数

    Code Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8256 Accepted: 3906 Description Tran ...

  8. bzoj2150: 部落战争(匈牙利)

    2150: 部落战争 题目:传送门 题解: 辣鸡数据..毁我AC率 先说做法,很容易就可以看出是二分图匹配的最小路径覆盖(可能是之前不久刚做过类似的题) 一开始还傻逼逼的去直接连边然后准备跑floyd ...

  9. nyoj--983--首尾相连数组的最大子数组和(动态规划)

    首尾相连数组的最大子数组和 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 给定一个由N个整数元素组成的数组arr,数组中有正数也有负数,这个数组不是一般的数组,其首尾是 ...

  10. 修改DNS

    解决方案一: 修改/etc/resolv.conf,添加 nameserver 8.8.8.8 nameserver 8.8.4.4 然后停用NetworkManager,service Networ ...