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. [luogu2513 HAOI2009] 逆序对数列 (计数dp)

    题目描述 对于一个数列{ai},如果有iaj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的数列,可以很容易求出有多少个逆序对数.那么逆序对数为k的这样自然数数列到底有多少个? ...

  2. [读书笔记] Python 数据分析 (十二)高级NumPy

    da array: 一个快速而灵活的同构多维大数据集容器,可以利用这种数组对整块的数据进行一些数学运算 数据指针,系统内存的一部分 数据类型 data type/dtype 指示数据大小的元组 str ...

  3. Flask-Babel 使用简介(翻译文档)

    最近用flask-bable翻译一个项目,在网站上查找到有一个示例文档,地址:http://translations.readthedocs.io/en/latest/flask-babel.html ...

  4. Python破解Wifi密码思路

    一.前言说明 本机运行环境:系统环境Win10,运行环境Python3.6,运行工具Pycharm 需要Python的包有:pywifi 这是一种暴力破解wifi的模式,需要的时间比较长,本文主要提供 ...

  5. [转]Python常用字符串

    转自:http://blog.csdn.net/daemonpei/article/details/6325762 字符串相关操作: + :string1+string2 #联接字符串,将后一个串链接 ...

  6. C++函数的导出与导入

    DLL使用 (1)隐式链接到 DLL 的可运行文件在生成时链接到导入库(.lib文件). (2)採用显式连接(LoadLibrary和GetProcAddress)时,不须要.lib文件. 函数导出方 ...

  7. JavaScript-4.4函数递归之阶乘举例---ShinePans

    <html> <head> <meta http-equiv="content-type" content="text/html;chars ...

  8. 汇编 -- Hook API (MessageBoxW)

    说到HOOK.我看了非常多的资料和教程.无奈就是学不会HOOK.不懂是我的理解能力差.还是你们说的 不够明确,直到我看了下面这篇文章,最终学会了HOOK: http://blog.sina.com.c ...

  9. DB-MySQL:MySQL 函数

    ylbtech-DB-MySQL:MySQL 函数 1. MySQL 字符串函数返回顶部 1. MySQL 字符串函数 函数 描述 实例 ASCII(s) 返回字符串 s 的第一个字符的 ASCII ...

  10. 关于输入getline

    此函数可读取整行,包括前导和嵌入的空格,并将其存储在字符串对象中. getline 函数如下所示: getline(cin, inputLine); 其中 cin 是正在读取的输入流,而 inputL ...