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的更多相关文章

  1. 缓存工具类CacheHelper

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  2. MySqlHelper、CacheHelper

    MySqlHelper代码: using System; using System.Collections; using System.Collections.Generic; using Syste ...

  3. [Cache] C#操作缓存--CacheHelper缓存帮助类 [复制链接]

    using System;using System.Web;using System.Collections; namespace DotNet.Utilities{ public class Cac ...

  4. Asp.net Core CacheHelper 通用缓存帮助类

    using System; using Microsoft.Extensions.Caching.Memory; using System.Runtime; namespace UFX.Tools { ...

  5. [Cache] C#操作缓存--CacheHelper缓存帮助类 (转载)

    点击下载 CacheHelper.zip CacheHelper 缓存帮助类 C#怎么操作缓存 怎么设置和取缓存数据,都在这个类里面呢 下面看一下代码吧 /// <summary> /// ...

  6. CacheHelper工具类的使用

    package com.bbcmart.util; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import ne ...

  7. C#操作缓存--CacheHelper缓存帮助类

    /// <summary>/// 类说明:Assistant/// 联系方式:361983679  /// 更新网站:<a href=\"http://www.cckan. ...

  8. C#缓存-依赖 CacheHelper

    缓存依赖文件或文件夹 //创建缓存依赖项 CacheDependency dep = new CacheDependency(fileName);//Server.MapPath("&quo ...

  9. C# WebHelper-CookieHelper,CacheHelper,SessionHelper

    常用web操作工具类,记录一下,本文记录的工具类,都要求引用 System.Web 1.CookieHelper /// <summary> /// Cookie工具类 /// </ ...

  10. WebHelper-SessionHelper、CookieHelper、CacheHelper、Tree

    ylbtech-Unitity: cs-WebHelper-SessionHelper.CookieHelper.CacheHelper.Tree SessionHelper.cs CookieHel ...

随机推荐

  1. equals(),hashcode(),克隆学习心得

    equals(),hashcode(),克隆学习心得 其实在开发时候,很少去重写equals(),hashCode()方法,但是有些时候业务需要还是要重写. 注意: 重写equals()方法一定要重写 ...

  2. Robot Framework 的安装和配置(转载)

    Robot Framework 的安装和配置 在使用 RF(Rebot framework)的时候需要 Python 或 Jython 环境,具体可根据自己的需求来确定.本文以在有 Python 的环 ...

  3. ASP.NET MVC 项目中 一般处理程序ashx 获取Session

    1-在 aspx和aspx.cs中,都是以Session["xxx"]="aaa"和aaa=Session["xxx"].ToString( ...

  4. 【Android测试】【第十九节】Espresso——API详解

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5997557.html 前言 Espresso的提供了不少A ...

  5. CSS3 媒体查询移动设备尺寸 Media Queries for Standard Devices (包括 苹果手表 apple watch)

    /* ----------- iPhone 4 and 4S ----------- */ /* Portrait and Landscape */ @media only screen and (m ...

  6. http工作流程

    一次HTTP操作称为一个事务,其工作过程可分为四步:1)首先客户机与服务器需要建立连接.只要单击某个超级链接,HTTP的工作开始.2)建立连接后,客户机发送一个请求给服务器,请求方式的格式为:统一资源 ...

  7. linux下文件搜索命令学习笔记

    1. locate:按照文件名搜索文件 locate filename 与find在整个操作系统中遍历搜索不同,locate命令在/var/lib/mlocate这个后台数据库中按照文件名搜索,所以优 ...

  8. zookeeper+jstorm的集群搭建

    zookeeper的配置: zookeeper有三种配置方式:单机式/伪分布式/集群式 其中伪分布式是在一台电脑上通过不同的端口来模拟分布式情形,需要N份配置文件和启动程序,而集群式是多个zookee ...

  9. 创建redis集群

    假设你已经安装好了redis ,如果还没有请安装 将多个实例跑起来 创建一个目录,比如 redis-cluster 把redis-server拷贝到这个目录下 在目录下为每一个实例创建一个文件夹 在每 ...

  10. c语言简易版文法

    文法 <程序>→<外部声明>|<程序><外部声明> <外部声明>→<函数定义>|<声明> <函数定义>→ ...