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. java实现登录的验证码和猜数字游戏_图形化界面

    实验任务四 1,出现设计思想 (1)先定义文本框.密码框和验证码框的组件 (2)定义面板和按钮的个数 (3)定义公有的虚构方法,通过对象实例化来调用 (4)利用Random类来实现生成0-9的随机数 ...

  2. Laravel源码解析之从入口开始

    前言 提升能力的方法并非使用更多工具,而是解刨自己所使用的工具.今天我们从Laravel启动的第一步开始讲起. 入口文件 laravel是单入口框架,所有请求必将经过index.php define( ...

  3. [luogu] P3202 [HNOI2009]通往城堡之路(贪心)

    P3202 [HNOI2009]通往城堡之路 题目描述 听说公主被关押在城堡里,彭大侠下定决心:不管一路上有多少坎坷,不管城堡中的看守有多少厉害,不管救了公主之后公主会不会再被抓走,不管公主是否漂亮. ...

  4. 【codeforces 738E】Subordinates

    [题目链接]:http://codeforces.com/problemset/problem/738/E [题意] 给你一个类似树形的关系; 然后告诉你某个人头顶上有多少个上司numi; 只有fat ...

  5. static方法调用

    Static方法调用,类名.方法名 int number = Integer.ParseInt(String ); 将字符串参数作为有符号的十进制整数进行解析 将数字解析成字节数组 Character ...

  6. 利用VisualVM监视远程JVM

    VisualVM介绍 VisualVM是集成了多个JDK命令工具的一个可视化工具,它主要用来监控JVM的运行情况,可以用它来查看和浏览Heap Dump.Thread Dump.内存对象实例情况.GC ...

  7. HDU 4329 Contest 3

    果然换个编译器就过了.总的来说,不难,不过就是处理一些空格.学习了一个新的类 istringstream可以按空格划分.然后,那条式子要理解. 式子的意义是: 找到一个串,该串在query中是第几个找 ...

  8. Android 自己定义RecyclerView 实现真正的Gallery效果

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38173061 .本文出自:[张鸿洋的博客] 上一篇博客我使用自己定义Horizo ...

  9. 【Linux驱动】TQ2440 DM9000E网卡驱动移植(Linux-2.6.30.4)

    花了一天的时间研究了一下Linux-2.6.30.4版本号内核下关于TQ2440 DM9000E的网卡驱动移植.总结一下自己的收获. 事实上.在Linux-2.6.30.4版本号内核下有关于网卡驱动, ...

  10. php设计模式适配器模式

    php设计模式适配器模式 简介 适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的.一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起. 其实就是通过一个转换类,这个转 ...