[导入]自写保存字符串或文件为asp.net缓存的类
2007-08-28 09:17
435 查看
using System;
using System.Text;
using System.Web;
using System.IO;
namespace Chsword {
/// <summary>
/// 成幻互联缓存类
/// 邹健 2007.5
/// </summary>
class Cache {
TimeSpan _TimeSpan;
/// <summary>
/// 构造函数。自动设置缓存为1小时20分。
/// </summary>
public Cache() {
_TimeSpan = new TimeSpan(0, 1, 20, 0, 0);
}
/// <summary>
/// 构造函数。手动设置缓存有效时间。
/// </summary>
/// <param name="ts">缓存有效时间</param>
public Cache(TimeSpan ts) {
_TimeSpan = ts;
}
/// <summary>
/// 检测缓存是否存在或为空。
/// </summary>
/// <param name="CacheName">缓存名称</param>
/// <returns>缓存存在则返回True,反之为False。</returns>
public Boolean IsNullorEmpty(String CacheName) {
if (HttpContext.Current.Cache[CacheName] != null)
if (String.IsNullOrEmpty(HttpContext.Current.Cache[CacheName].ToString()))
return true;
else
return false;
else
return true;
}
/// <summary>
/// 设置缓存。
/// </summary>
/// <param name="CacheName">缓存名称</param>
/// <returns>是否存储成功。</returns>
public Boolean SetCache(String CacheName){
try {
String fn = HttpContext.Current.Request.MapPath(String.Format("~/Xml/{0}.xml", CacheName));
return SetCache(CacheName,OpenTextFile(fn));
}
catch {
return false;
}
}
/// <summary>
/// 设置缓存。
/// </summary>
/// <param name="CacheName">缓存名称</param>
/// <param name="CacheValue">缓存的值</param>
/// <returns>是否存储成功。</returns>
public Boolean SetCache(String CacheName,String CacheValue) {
try {
if (!IsNullorEmpty(CacheName))
return true;
else {//如果不存在,则重新载入缓存。
HttpContext.Current.Cache.Add(CacheName, CacheValue, null, DateTime.MaxValue, _TimeSpan , System.Web.Caching.CacheItemPriority.Normal, null);
return true;
}
}
catch {
return false;
}
}
/// <summary>
/// 打开文本文件,并返回文件内容。
/// </summary>
/// <param name="fn">文件路径。</param>
/// <returns>返回文本文件内容。</returns>
String OpenTextFile(String fn) {
String text;
using (StreamReader sr = new StreamReader(fn, System.Text.Encoding.UTF8)) {
text = sr.ReadToEnd();
}
return text;
}
}
}
在使用时要先进行Cache的实例化,再进行实例化。
Boolean SetCache(String CacheName)这个函数是为我的工程特制的,如果可以的话呢,可以对其进行重写。
本身这段代码并没有什么技术含量,写了这么长时间,用着也没有BUG,而且还很方便,于是就给出来,希望大家多提意见。
邹健 2007-07-24 10:55 发表评论
文章来源:http://www.cnblogs.com/chsword/archive/2007/07/24/829175.html
using System.Text;
using System.Web;
using System.IO;
namespace Chsword {
/// <summary>
/// 成幻互联缓存类
/// 邹健 2007.5
/// </summary>
class Cache {
TimeSpan _TimeSpan;
/// <summary>
/// 构造函数。自动设置缓存为1小时20分。
/// </summary>
public Cache() {
_TimeSpan = new TimeSpan(0, 1, 20, 0, 0);
}
/// <summary>
/// 构造函数。手动设置缓存有效时间。
/// </summary>
/// <param name="ts">缓存有效时间</param>
public Cache(TimeSpan ts) {
_TimeSpan = ts;
}
/// <summary>
/// 检测缓存是否存在或为空。
/// </summary>
/// <param name="CacheName">缓存名称</param>
/// <returns>缓存存在则返回True,反之为False。</returns>
public Boolean IsNullorEmpty(String CacheName) {
if (HttpContext.Current.Cache[CacheName] != null)
if (String.IsNullOrEmpty(HttpContext.Current.Cache[CacheName].ToString()))
return true;
else
return false;
else
return true;
}
/// <summary>
/// 设置缓存。
/// </summary>
/// <param name="CacheName">缓存名称</param>
/// <returns>是否存储成功。</returns>
public Boolean SetCache(String CacheName){
try {
String fn = HttpContext.Current.Request.MapPath(String.Format("~/Xml/{0}.xml", CacheName));
return SetCache(CacheName,OpenTextFile(fn));
}
catch {
return false;
}
}
/// <summary>
/// 设置缓存。
/// </summary>
/// <param name="CacheName">缓存名称</param>
/// <param name="CacheValue">缓存的值</param>
/// <returns>是否存储成功。</returns>
public Boolean SetCache(String CacheName,String CacheValue) {
try {
if (!IsNullorEmpty(CacheName))
return true;
else {//如果不存在,则重新载入缓存。
HttpContext.Current.Cache.Add(CacheName, CacheValue, null, DateTime.MaxValue, _TimeSpan , System.Web.Caching.CacheItemPriority.Normal, null);
return true;
}
}
catch {
return false;
}
}
/// <summary>
/// 打开文本文件,并返回文件内容。
/// </summary>
/// <param name="fn">文件路径。</param>
/// <returns>返回文本文件内容。</returns>
String OpenTextFile(String fn) {
String text;
using (StreamReader sr = new StreamReader(fn, System.Text.Encoding.UTF8)) {
text = sr.ReadToEnd();
}
return text;
}
}
}
在使用时要先进行Cache的实例化,再进行实例化。
Boolean SetCache(String CacheName)这个函数是为我的工程特制的,如果可以的话呢,可以对其进行重写。
本身这段代码并没有什么技术含量,写了这么长时间,用着也没有BUG,而且还很方便,于是就给出来,希望大家多提意见。
邹健 2007-07-24 10:55 发表评论
文章来源:http://www.cnblogs.com/chsword/archive/2007/07/24/829175.html
相关文章推荐
- 自写保存字符串或文件为asp.net缓存的类
- 自写保存字符串或文件为asp.net缓存的类
- 自写保存字符串或文件为asp.net缓存的类
- [导入]ASP.NET遍历配置文件的连接字符串
- [导入]ASP.NET 2.0 遍历配置文件的数据库连接字符串
- ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
- Asp.net直接保存文件到客户端
- [导入]怎样从ASP.NET 2.0中读取连接字符串
- ASP.NET性能优化之构建自定义文件缓存
- ASP.NET 2.0 遍历配置文件的数据库连接字符串
- [导入]在asp.net中利用FileUplad控件从同一个页面上传多个文件
- ASP.NET下使用xml反序列化、缓存实现个性化配置文件的实时生效
- ASP.NET——配置文件——连接字符串
- ASP.NET数据库连接字符串写在config.web配置文件中
- ASP.NET—016:ASP.NET中保存文件对话框
- Asp.net直接保存文件到客户端
- Asp.net直接保存文件到客户端
- Asp.net中Excel文件导入sqlServer
- 在Asp.Net中如何将从SQL读出来的表转化成XML文件并保存在指定的位置中