asp.net 缓存公共类
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 缓存公共类的更多相关文章
- ASP.NET中常用的几个李天平开源公共类LTP.Common,Maticsoft.DBUtility,LtpPageControl
ASP.NET中常用的几个开源公共类: LTP.Common.dll: 通用函数类库 源码下载Maticsoft.DBUtility.dll 数据访问类库组件 源码下载LtpPageC ...
- ASP.NET中常用的几个李天平开源公共类LTP.Common,Maticsoft.DBUtility,LtpPageControl (转)
ASP.NET中常用的几个开源公共类: LTP.Common.dll: 通用函数类库 源码下载Maticsoft.DBUtility.dll 数据访问类库组件 源码下载LtpPageC ...
- Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类
Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类 转载自http://www.trinea.cn/android/android-common-lib/ 介绍总结的一 ...
- asp.net—缓存
1.页面缓存 要实现页面输出缓存,只要将一条 OutputCache 指令添加到页面即可. <%@ OutputCache CacheProfile=" " NoStore= ...
- Asp.Net缓存(2)
缓存页的多个版本 ASP.NET 允许在输出缓存中缓存同一页的多个版本.输出缓存可能会因下列因素而异: 初始请求 (HTTP GET) 中的查询字符串. 回发时传递的控制值(HTTP POST 值). ...
- Asp.Net缓存(1)
知其根本,方能应用.MSDN上的缓存讲解.先看原来讲解. Asp.Net缓存概述 通常,应用程序可以将那些频繁访问的数据,以及那些需要大量处理时间来创建的数据存储在内存中,从而提高性能. 在这些情况下 ...
- ASP.NET缓存策略经验谈
要提升ASP.NET应用程序的性能,最简单.最有效的方式就是使用内建的缓存引擎.虽然也能构建自己的缓存,但由于缓存引擎已提供了如此多的功能,所以完全不必如此麻烦.在很大程度上,ASP.NET开发者在W ...
- (转)ASP.NET缓存全解析6:数据库缓存依赖
ASP.NET缓存全解析文章索引 ASP.NET缓存全解析1:缓存的概述 ASP.NET缓存全解析2:页面输出缓存 ASP.NET缓存全解析3:页面局部缓存 ASP.NET缓存全解析4:应用程序数据缓 ...
- 自己封装的C#操作redis公共类
关于C#操作redis公共类,网上有很多版本,每个版本我都看了,发觉还是不够完美,都存在一个问题,只能操作单一的缓存数据库 redis指令支持上,这里可以自己去扩展,下面分享下我近期封装的一个redi ...
随机推荐
- [LUOGU]2016 Sam数
我本来想看看SAM,就看见了这个.. 这道题很容易让人想到数位DP,用\(f[i][j]\)表示考虑到第\(i\)位,最后一位是\(j\)的方案数.看到1e18,直接矩阵快速幂加速,因为它每位转移都是 ...
- debian 9 安装Virtual Box
1.去官网下载deb包,例如包名: virtualbox-.2_5.2.18-124319_Debian_stretch_amd64.deb 2.安装 .2_5.2.18-124319_Debian_ ...
- Socket实现一个简单的半双工通信
Socket是client进行在网络与server进行数据交互的一种基本通信方式.通信有三种通信.即单工.半双工,和全双工. 所谓单工,就是仅仅可以进行单向通信,如bb机. 而半双工就是一来一回的通信 ...
- xpee.vbs
xpee.vbs Win 8安装之后每一次重启桌面都会有一个360浏览器的快捷方式,终于找到原因了, 在Windows/System下面有这么个文件: Set ws = CreateObject(&q ...
- [HTML5] Inlining images with SVG and data URIs
The main reason you want to do I"nlining images with SVG and data URIs" is to reduce http ...
- 《从零開始学Swift》学习笔记(Day 46)——下标重写
原创文章.欢迎转载.转载请注明:关东升的博客 下标是一种特殊属性. 子类属性重写是重写属性的getter和setter訪问器,对下标的重写也是重写下标的getter和setter訪问器. 以下看一个演 ...
- ACM这一路
出自自己内心的声音. 大学已经读了一年,自己也老了一岁. 从開始的什么都不懂,到如今的懂了也不想多说什么,说多了也是累.在大学其中唯一还在执着的是ACM.这个也是我唯一能执着的东西,由 ...
- sap abap 对字符串的操作
替换字段内容 REPLACE [FIRST /ALL OCCURRENCES OF]<STR1>INTO <STR> WITH <STR2> DATA STR ...
- ubuntu 交叉编译qt 5.7 程序到 arm 开发板
ubuntu 交叉编译qt 5.7 程序到 arm 开发板平台1 ubuntu 12.042 arm-linux-gcc 4.5.13 QT 5.74 开发板210 armcortex-A8 一 概述 ...
- springboot shiro 多realm配置认证、授权
shiro进行登录认证和权限管理的实现.其中需求涉及使用两个角色分别是:门店,公司.现在要两者实现分开登录.即需要两个Realm——MyShiroRealmSHOP和MyShiroRealmCOMPA ...