CacheHelper
public static ObjectCache Cache
{
get
{
return MemoryCache.Default;
}
}
public static bool TryGetCache<T>(string key, ref T value)
{
object v = null;
//Type t = typeof(T);
bool hit;
hit = TryGetCacheObject(key, ref v);
if (hit)
value = (T)v;
return hit;
}
public static bool TryGetCacheStruct<T>(string key, ref T value) where T : struct
{
object v = null;
bool hit = TryGetCacheObject(key, ref v);
if (hit)
value = (T)v;
return hit;
}
public static bool TryGetCacheObject(string key, ref object value)
{
object v = Cache.Get(key);
bool hit = false;
if (v == null)
hit = false;
else if (v == DBNull.Value)
{
hit = true;
value = null;
}
else
{
hit = true;
value = v;
}
TraceHelper.Trace("Cache", string.Format("TryGetCache({0}) = {1}", key, hit));
return hit;
}
public static bool ContainsCache(string key)
{
return Cache.Contains(key);
}
public static object GetCache(string key)
{
return Cache.Get(key);
}
public static void SetCache(string key, object value)
{
Cache.Set(key, value, CacheItemPolicy);
}
public static void SetCache(string key, object value, CacheItemPolicy cacheItemPolicy)
{
Cache.Set(key, value, cacheItemPolicy);
}
public static CacheItemPolicy CacheItemPolicy
{
get
{
CacheItemPolicy policy = new CacheItemPolicy();
policy.SlidingExpiration = , AppConfiguration.CacheSlidingExpirationInMins, );
return policy;
}
}
public static void ClearCacheByPrefix(string prefix)
{
List<string> keys = new List<string>();
foreach (var c in Cache)
{
if (c.Key.StartsWith(prefix))
{
keys.Add(c.Key);
}
}
foreach (var key in keys)
{
Cache.Remove(key);
}
}
Use:
string cachekey = string.Format("HasPermission_{0}_{1}", User.Id, functionName);
bool result = false;
if (!WebHelper.TryGetCache(cachekey, ref result))
{
result = roleBO.FunctionIsAllowForStaff(functionName, this.CurrentActualUser.Id);
WebHelper.SetCache(cachekey, result);
}
CacheHelper的更多相关文章
- 缓存工具类CacheHelper
代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...
- MySqlHelper、CacheHelper
MySqlHelper代码: using System; using System.Collections; using System.Collections.Generic; using Syste ...
- [Cache] C#操作缓存--CacheHelper缓存帮助类 [复制链接]
using System;using System.Web;using System.Collections; namespace DotNet.Utilities{ public class Cac ...
- Asp.net Core CacheHelper 通用缓存帮助类
using System; using Microsoft.Extensions.Caching.Memory; using System.Runtime; namespace UFX.Tools { ...
- [Cache] C#操作缓存--CacheHelper缓存帮助类 (转载)
点击下载 CacheHelper.zip CacheHelper 缓存帮助类 C#怎么操作缓存 怎么设置和取缓存数据,都在这个类里面呢 下面看一下代码吧 /// <summary> /// ...
- CacheHelper工具类的使用
package com.bbcmart.util; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import ne ...
- C#操作缓存--CacheHelper缓存帮助类
/// <summary>/// 类说明:Assistant/// 联系方式:361983679 /// 更新网站:<a href=\"http://www.cckan. ...
- C#缓存-依赖 CacheHelper
缓存依赖文件或文件夹 //创建缓存依赖项 CacheDependency dep = new CacheDependency(fileName);//Server.MapPath("&quo ...
- C# WebHelper-CookieHelper,CacheHelper,SessionHelper
常用web操作工具类,记录一下,本文记录的工具类,都要求引用 System.Web 1.CookieHelper /// <summary> /// Cookie工具类 /// </ ...
- WebHelper-SessionHelper、CookieHelper、CacheHelper、Tree
ylbtech-Unitity: cs-WebHelper-SessionHelper.CookieHelper.CacheHelper.Tree SessionHelper.cs CookieHel ...
随机推荐
- DateUtil工具类
package com.autoserve.mh.common.util; import java.text.SimpleDateFormat; import java.util.Calendar ...
- http请求,普通的get和post方法
在http://www.cnblogs.com/ITtangtang/p/3968093.html的基础上封装了一下get和post请求的常用方法, 虽然很简单,也晒晒 import org.apac ...
- 简单而兼容性好的Web自适应高度布局,纯CSS
纯CSS实现的自适应高度布局,中间内容不符自动滚动条.兼容IE9以上.chrome.FF.关键属性是box-sizing: border-box. 不废话,直接上代码: <!doctype ht ...
- Windows一些零碎
一.端口被占用: 1.netstat -ano |findstr 80 //查看3306端口是否存在 2.tasklist |findstr 3036(PID号)//查看pid为3036的是什么程序在 ...
- UNIX中的文件类型
Unix的文件类型信息包含在stat结构的st_mode成员中可以用宏确定文件类型: 普通文件(S_ISREG()):包含某种形式数据的常用文件类型 目录文件(S_ISDIR()):这种文件包含其他文 ...
- wamp下多域名配置问题
1.找到wamp安装目录的apache安装目录 找到 httpd.conf文件 例如我安装的目录为 E:\wamp\bin\apache\apache2.2.8\conf\httpd.conf 也可以 ...
- LeetCode Sum of Two Integers
原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...
- C# Console控制命令
参考博客:C# 控制台程序(命令行程序)设置字体颜色,窗口宽高,光标行数 禁用控制台关闭按钮,参考博客:禁用C#控制台应用程序的关闭按钮 参考博客:c# 控制台程序禁用关闭按钮完美解决 #region ...
- varnish 隐藏版本号
varnish 隐藏方法: 修改default.vcl配置文件. 找到或添加 vcl_deliver 子程序,代码如下: 1 2 3 4 5 sub vcl_deliver { unse ...
- 关于JS嵌套点击事件的问题。
$().click() 是点击命令$().click(function(){代码}) 是绑定click事件,并不会直接运行.所以在嵌套的时候就有可能出现重复绑定的问题.下面是使用jsonp跨站访问代码 ...