C# CacheHepler Class
internal class CacheHelper
{
/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="o">Item to be cached</param>
/// <param name="key">Name of item</param>
public static void Add<T>(T o, string key)
{
// NOTE: Apply expiration parameters as you see fit.
// In this example, I want an absolute
// timeout so changes will always be reflected
// at that time. Hence, the NoSlidingExpiration.
HttpContext.Current.Cache.Insert(
key,
o,
null,
DateTime.Now.AddMinutes(1440),
System.Web.Caching.Cache.NoSlidingExpiration);
} /// <summary>
/// Remove item from cache
/// </summary>
/// <param name="key">Name of cached item</param>
public static void Clear(string key)
{
HttpContext.Current.Cache.Remove(key);
} /// <summary>
/// Check for item in cache
/// </summary>
/// <param name="key">Name of cached item</param>
/// <returns></returns>
public static bool Exists(string key)
{
return HttpContext.Current.Cache[key] != null;
} /// <summary>
/// Retrieve cached item
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="key">Name of cached item</param>
/// <param name="value">Cached value. Default(T) if
/// item doesn't exist.</param>
/// <returns>Cached item as type</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;
}
使用方法:
public override List<Employee> GetEmployeeList()
{
string key = ConfigurationHelper.CacheKeyEmployeeList; List<Employee> employees = CacheHelper.Get<List<Employee>>(key); if (employees == null)
{
employees = instance.GetEmployeeList();
CacheHelper.Add(employees, key);
} return employees;
}
C# CacheHepler Class的更多相关文章
随机推荐
- CAKeyframeAnimation 旋转动画
- 通过uiview动画来放大图片
UIImage *image=[UIImage imageNamed:"]; UIImageView *imageView=[[UIImageView alloc]init]; imageV ...
- batchExportPNG.py不是我的代码
#coding=gbk#@author lifc,20140806#批量制作输出专题图import arcpy,os,time,math #testpath=raw_input("testp ...
- swift 委托代理传值
委托代理 1.定义个协议 2.声明一个委托代理 3.指定委托代理,调用委托实现的协议方法 4实现LoadingDelegate协议 代码如下: import UIKit //1.定义个协议 proto ...
- Genymotion——PC上也能玩部落冲突 Clash of Clans
零.前言 部落冲突(Clash of Clans)是手机上一个很不错的联机对战游戏,安卓和iOS平台上都有. 最近玩这个略上瘾,前几天看到部落里有人说用模拟器可以在电脑上玩,就想自己试试. 不想看我啰 ...
- Oracle 约束类型
在Oracle中的约束类型:NOT NULLUNIQUE KeyPRIMARY KEYFOREIGN KEYCHECK create table emp--创建表格 ,注意约束( empno numb ...
- css边框阴影问题
阴影落在下方:box-shadow: 0 3px 5px rgba(0, 0, 0, .2); 阴影落在四周:box-shadow: 0 3px 5px rgba(0, 0, 0, .2), 0 0 ...
- itunes connect提交app教程
.打开itunes connect登陆之后,选择Manage Your Apps,再选Add New App: .填写项目相关信息,不知道怎么填的点击问号查看: Bundle ID Suffix需要和 ...
- JAVA IO流的简单总结+收集日志异常信息
1.字节流: IuputStream 所有字节输出流的超类 . 抽象类 ---- FileInputStream ---- BufferedInputStream:提供数据的读取效率,拓展方法(内部维 ...
- 关于arraylist.remove的一些小问题。
public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<Integer> ...