一开始我的网站上没有用缓存,很明细速度非常慢。用过缓存之后速度明显好多了。大家看看这几个方法,当初也是在网上学习之后的笔记,然后整理出来的一个类。大家看看有什么不足,或需要改进的地方。 我的网站:我爱编码网using System;
using System.Collections.Generic;
using System.Web;
using System.Configuration;
using System.Collections;/// <summary>
///CacheHelper 的摘要说明
/// </summary>
public class CacheHelper
{
#region 从配置文件中读取缓存时间
//缓存时间
private static string _CacheTime = string.Empty;
public static string CacheTime
{
get
{
try
{
_CacheTime = ConfigurationManager.AppSettings["CacheTime"].ToString();
}
catch (Exception)
{
_CacheTime = "0";
}
return _CacheTime;
}
set { _CacheTime = value; }
}
#endregion public CacheHelper()
{
//
//TODO: 在此处添加构造函数逻辑
//
} #region 插入Cache
/// <summary>
/// 插入Cache
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="o"></param>
/// <param name="key"></param>
public static void Add<T>(T o, string key)
{
HttpContext.Current.Cache.Insert(
key,
o,
null,
DateTime.Now.AddMinutes(Convert.ToDouble(CacheTime)), // Cache的缓存时间,通常建议配置在Config文件中
System.Web.Caching.Cache.NoSlidingExpiration);
}
#endregion #region 删除指定的Cache
/// <summary>
/// 删除指定的Cache
/// </summary>
/// <param name="key">Cache的key</param>
public static void Clear(string key)
{
HttpContext.Current.Cache.Remove(key);
}
#endregion #region 判断Cache是否存在
/// <summary>
/// 判断Cache是否存在
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool Exists(string key)
{
return HttpContext.Current.Cache[key] != null;
}
#endregion #region 取得Cache值,带类型 T
/// <summary>
/// 取得Cache值,带类型 T
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool Get<T>(string key, out T value)
{
try
{
if (!Exists(key))
{
value = default(T); //
return false;
} value = (T)HttpContext.Current.Cache[key];
}
catch
{
value = default(T);
return false;
} return true;
}
#endregion #region 清除所有缓存
/// <summary>
/// 有时可能需要立即更新,这里就必须手工清除一下Cache
///Cache类有一个Remove方法,但该方法需要提供一个CacheKey,但整个网站的CacheKey我们是无法得知的
///只能经过遍历
/// </summary>
public static void RemoveAllCache()
{ System.Web.Caching.Cache _cache = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
ArrayList al = new ArrayList();
while (CacheEnum.MoveNext())
{
al.Add(CacheEnum.Key);
} foreach (string key in al)
{
_cache.Remove(key);
}
}
#endregion #region 显示所有缓存 //显示所有缓存
public static string show()
{
string str = "";
IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator(); while (CacheEnum.MoveNext())
{
str += "<br />缓存名<b>[" + CacheEnum.Key + "]</b><br />";
}
return "当前网站总缓存数:" + HttpRuntime.Cache.Count + "<br />" + str;
}
#endregion
}
using System.Collections.Generic;
using System.Web;
using System.Configuration;
using System.Collections;/// <summary>
///CacheHelper 的摘要说明
/// </summary>
public class CacheHelper
{
#region 从配置文件中读取缓存时间
//缓存时间
private static string _CacheTime = string.Empty;
public static string CacheTime
{
get
{
try
{
_CacheTime = ConfigurationManager.AppSettings["CacheTime"].ToString();
}
catch (Exception)
{
_CacheTime = "0";
}
return _CacheTime;
}
set { _CacheTime = value; }
}
#endregion public CacheHelper()
{
//
//TODO: 在此处添加构造函数逻辑
//
} #region 插入Cache
/// <summary>
/// 插入Cache
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="o"></param>
/// <param name="key"></param>
public static void Add<T>(T o, string key)
{
HttpContext.Current.Cache.Insert(
key,
o,
null,
DateTime.Now.AddMinutes(Convert.ToDouble(CacheTime)), // Cache的缓存时间,通常建议配置在Config文件中
System.Web.Caching.Cache.NoSlidingExpiration);
}
#endregion #region 删除指定的Cache
/// <summary>
/// 删除指定的Cache
/// </summary>
/// <param name="key">Cache的key</param>
public static void Clear(string key)
{
HttpContext.Current.Cache.Remove(key);
}
#endregion #region 判断Cache是否存在
/// <summary>
/// 判断Cache是否存在
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool Exists(string key)
{
return HttpContext.Current.Cache[key] != null;
}
#endregion #region 取得Cache值,带类型 T
/// <summary>
/// 取得Cache值,带类型 T
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool Get<T>(string key, out T value)
{
try
{
if (!Exists(key))
{
value = default(T); //
return false;
} value = (T)HttpContext.Current.Cache[key];
}
catch
{
value = default(T);
return false;
} return true;
}
#endregion #region 清除所有缓存
/// <summary>
/// 有时可能需要立即更新,这里就必须手工清除一下Cache
///Cache类有一个Remove方法,但该方法需要提供一个CacheKey,但整个网站的CacheKey我们是无法得知的
///只能经过遍历
/// </summary>
public static void RemoveAllCache()
{ System.Web.Caching.Cache _cache = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
ArrayList al = new ArrayList();
while (CacheEnum.MoveNext())
{
al.Add(CacheEnum.Key);
} foreach (string key in al)
{
_cache.Remove(key);
}
}
#endregion #region 显示所有缓存 //显示所有缓存
public static string show()
{
string str = "";
IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator(); while (CacheEnum.MoveNext())
{
str += "<br />缓存名<b>[" + CacheEnum.Key + "]</b><br />";
}
return "当前网站总缓存数:" + HttpRuntime.Cache.Count + "<br />" + str;
}
#endregion
}
解决方案 »
- 一个标签上绑2个值 !求救
- RowCreated事件 缺少对象
- 自动创建路径
- 启动网站出现的错误信息
- asp.net里面的WEB.Config可以通过文件来读写吗
- web打印的问题,设置了没用。
- 如何vb脚本给服务器端的上传控件赋值?
- 不使用showModalDialog() ,而使用window.open()打开一个窗口,怎么样保证子窗口未关闭前无法操作母窗口?
- 软件技术QQ群 1265452 有意的请加入
- TextBox的好事,让的我没办法该了!!!!
- 被逼疯了,google chrome和safari居然不支持radio input的onblur,onfocus事件?
- ASP.NET用FileUpload上传图片在本地可以,上传FTP服务器出错,怎么回事
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web.Caching;namespace DotNet.Common
{
/// <summary>
/// 缓存常用操作
/// </summary>
public class CacheUtil
{
public CacheUtil()
{
//
// TODO: 在此处添加构造函数逻辑
//
} /// <summary>
/// 增加一个缓存对象
/// </summary>
/// <param name="strKey">键</param>
/// <param name="valueObj">值</param>
/// <param name="duration">以秒为单位</param>
/// <returns></returns>
public static bool Insert(string strKey, object valueObj, double durationSecond)
{
if (strKey != null && strKey.Length != 0 && valueObj != null)
{
//建立回调委托的一个实例 CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
HttpContext.Current.Cache.Insert(strKey, valueObj, null,DateTime.Now.AddSeconds(durationSecond),System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Default,
callBack);
return true;
}
else
{
return false;
}
} /// <summary>
/// 判断缓存对象是否存在
/// </summary>
/// <param name="strKey"></param>
/// <returns></returns>
public static bool IsExist(string strKey)
{
return HttpContext.Current.Cache[strKey] != null;
} /// <summary>
/// 读取缓存对象
/// </summary>
/// <param name="strKey"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static object Read(string strKey)
{
//取出值 if (HttpContext.Current.Cache[strKey] != null)
{
object obj = HttpContext.Current.Cache[strKey];
if (obj == null)
{
return null;
}
else
{
return obj;
}
}
else
{
return null;
}
} /// <summary>
/// 删除缓存对象
/// </summary>
/// <param name="strKey"></param>
/// <returns></returns>
public static void Remove(string strKey)
{
if (HttpContext.Current.Cache[strKey] != null)
{
HttpContext.Current.Cache.Remove(strKey);
}
} /// <summary>
/// 根据设置的正则表达式清除缓存对象
/// </summary>
/// <param name="pattern">正则表达式</param>
public static void RemoveByRegexp(string pattern)
{
if (pattern != "")
{
IDictionaryEnumerator enu = HttpContext.Current.Cache.GetEnumerator();
while (enu.MoveNext())
{
string key = enu.Key.ToString();
if(Regex.IsMatch(key,pattern))
{
Remove(key);
}
}
}
} /// <summary>
/// 清除所有缓存对象
/// </summary>
public static void Clear()
{
IDictionaryEnumerator enu = HttpContext.Current.Cache.GetEnumerator();
while (enu.MoveNext())
{
Remove(enu.Key.ToString());
}
} /// <summary>
/// 此方法在值失效之前调用,可以用于在失效之前更新数据库,或从数据库重新获取数据
/// </summary>
/// <param name="strKey"></param>
/// <param name="obj"></param>
/// <param name="reason"></param>
private static void onRemove(string strKey, object obj, CacheItemRemovedReason reason)
{ }
}
}
看看LTP的系统缓存介绍
和
Insert
会不会有并发问题啊
当我把那个文件夹清空之后,我发现我的cache还有东西,我哪里理解错了?