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的更多相关文章
随机推荐
- Debian 中添加ppa
在Debian8中默认没有"apt-add-repository"命令,所有也就没法安装ppa. 怎么破? sudo apt-get install software-proper ...
- UITabBarItem编写的时候出现得图片显示异常,和有一些比较忽略的方法总结
我现在学到可分栏控制器,UITabBarController.我总结了它的层次有,UITabBarController控制并且只有以个UITanBat(他是一个UIIView的子类),UITabBar ...
- Android渠道汇总
序号 渠道名 渠道说明 特殊渠道 1 googleplay google市场 2 umeng 自动更新 3 office_web 官方网络 4 office_qrcode 官方二维码 硬件 ...
- Macbook之设置Finder显示文件完整路径
终端里输入:defaults write com.apple.finder _FXShowPosixPathInTitle -bool TRUE;killall Finder 回复输入:default ...
- 使 div 元素看上去像一个按钮
使 div 元素看上去像一个按钮 div { appearance:button; -moz-appearance:button; /* Firefox */ -webkit-appearance:b ...
- Flex前台和后台WCF服务之间数据的接收与传输
1.首先在flex程序中通过添加webservice,方式是主菜单Data->Connect to WebService,然后输入wsdl文档的地址.如果输入地址后始终添加不进了,或者报错,一般 ...
- AjaxPro框架
AjaxPro框架 本章主要了解Ajax框架,掌握Ajaxpro框架的配置以及应用. 内容如下,请点击AjaxPro框架查看:
- XAPI(XenAPI)
转载:http://www.cnblogs.com/dkblog/archive/2011/07/07/2099885.html 初识toolstack--XEN的XenServer管理的核心 什 ...
- asp.net中c# TextBox.MaxLength例子
TextBox.MaxLength 属性获取或设置文本框中最多允许的字符数文本框中最多允许的字符数.默认值为 0,表示未设置该属性.使用 MaxLength 属性限定可以在 TextBox 控件中输入 ...
- Cocos2d-x标签文乱码问题
我们在Windows下使用Visual Studio 2012开发游戏的时候,使用标签中包含中文时候会出现乱码或无法显示,如下图所示: 而应该显示的中文是如下图所示: HelloWorldScene. ...