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. 【转】Maven Jetty 插件的问题(css/js等目录死锁)的解决

    Maven Jetty 插件的问题(css/js等目录死锁,不能自动刷新)的解决:   1. 打开下面的目录:C:\Users\用户名\.m2\repository\org\eclipse\jetty ...

  2. 关于iis8.5中发布的网站无法连接数据库的解决方案。

    发布的网站在浏览时出现如下提示: “/”应用程序中的服务器错误. 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL ...

  3. mysql优化杂记

    一.mysqladmin的使用#mysqladmin extended-status -u root -i 2 -c 2 -p | grep connect查看mysql的状态中带有connect字符 ...

  4. java 性能优化(代码优化)

    参考博文: java 性能优化:35 个小细节,让你提升 java 代码的运行效率

  5. java中Collections.sort排序详解

    Comparator是个接口,可重写compare()及equals()这两个方法,用于比价功能:如果是null的话,就是使用元素的默认顺序,如a,b,c,d,e,f,g,就是a,b,c,d,e,f, ...

  6. 转:PostgreSQL Cheat Sheet

    PostgreSQL Cheat Sheet CREATE DATABASE CREATE DATABASE dbName; CREATE TABLE (with auto numbering int ...

  7. Android 使用js调用Java

    效果如: 主要用到一个接口类:MyObject package com.example.jsdemo; import android.content.Context; import android.s ...

  8. SpringMVC的几种返回方式

    package com.boventech.learning.controller; import java.util.HashMap; import java.util.Map; import or ...

  9. 线性时间O(n)内求数组中第k大小的数

    --本文为博主原创,转载请注明出处 因为最近做的WSN(wireless sensor network)实验要求用3个传感器节点接受2000个包的数据并算出一些统计量,其中就有算出中位数这么一个要求, ...

  10. 《Linux及安全》实践2

    <Linux及安全>实践2 [edited by 5216lwr] 一.Linux基本内核模块 1.1理解什么是内核模块 linux模块是一些可以作为独立程序来编译的函数和数据类型的集合. ...