常用工具类8-缓存类

public static class CacheHelper
{
static Cache _cache = HttpRuntime.Cache;

/// <summary>
/// 获取缓存
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static object Get(string key)
{
return _cache.Get(key);
}

/// <summary>
/// 设置缓存
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void Set(string key, object value)
{
_cache.Insert(key, value);
}

/// <summary>
/// 设置指定时间内有效的缓存
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expireSeconds">缓存有效时间,单位秒</param>
public static void Set(string key, object value, long expireSeconds)
{
_cache.Insert(key, value, null, DateTime.UtcNow.AddSeconds(expireSeconds), TimeSpan.Zero);
}

/// <summary>
/// 清除指定key的缓存内容
/// </summary>
/// <param name="key"></param>
public static void Remove(string key)
{
_cache.Remove(key);
}

/// <summary>
/// 清除关键字为某一指定前缀的所有缓存内容
/// </summary>
/// <param name="keyPrefix">缓存关键字前缀</param>
public static void RemoveCacheByKeyPrefix(string keyPrefix)
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
ArrayList al = new ArrayList();
while (CacheEnum.MoveNext())
{
if (CacheEnum.Key.ToString().StartsWith(keyPrefix))
{
al.Add(CacheEnum.Key);
}
}

foreach (string key in al)
{
_cache.Remove(key);
}
}

/// <summary>
/// 清除所有缓存内容
/// </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);
}
}
}

转载于:https://www.cnblogs.com/zhshlimi/p/5605538.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值