cacheHelper
2016-04-21 08:23
417 查看
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;
namespace AddSdJobs
{
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);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;
namespace AddSdJobs
{
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);
}
}
}
}
相关文章推荐
- 第四周项目三猜数字游戏
- 14级商软2班软件工程大作业分组定题
- 第七周项目2—友元类
- CSS position属性和实例应用
- bzoj3745: [Coci2015]Norma
- 拿什么拯救你,我的代码--c#编码规范实战篇
- 论项目的UI设计
- CustomerConfigHelper
- 第七周项目1—两点间的距离—成员函数、友元函数和一般函数
- MyBatis中SQL映射的XML文件
- 第五周项目一—三角形类定义成员函数
- LeetCode 172. Factorial Trailing Zeroes
- 三角形5
- 响应式布局图片处理
- LeetCode 311. Sparse Matrix Multiplication(稀疏矩阵相乘)
- 详解css3弹性盒模型(Flexbox)
- Outlook使用技巧
- 数码相框笔记
- 华为在线测试--学英语
- android音视频点/直播模块开发