• HttpCache缓存扩展方法


    using System;
    using System.Collections;
    using System.Configuration;
    using System.Web;
    using System.Web.Caching;

    namespace Meb.Common.Extensions
    {
    //缓存写入
    //验证缓存是否存在
    //缓存读取 按缓存索引读取相应缓存值
    public static class CacheExtension
    {
    //private static int CacheMinute = int.Parse(ConfigurationManager.AppSettings["CacheMinute"] ?? "30");

    /// <summary>
    /// 获取当前应用程序指定CacheKey的Cache值
    /// </summary>
    /// <param name="cacheKey"></param>
    /// <returns></returns>
    public static object GetCache(string cacheKey)
    {
    var objCache = HttpRuntime.Cache;
    return objCache[cacheKey];
    }
    /// <summary>
    /// 查询指定CacheKey的Cache值是否存在
    /// </summary>
    /// <param name="cacheKey"></param>
    /// <returns></returns>
    public static bool IsExistsCache(string cacheKey)
    {
    var objCache = HttpRuntime.Cache;
    return objCache[cacheKey] != null;
    }

    /// <summary>
    /// 设置当前应用程序指定CacheKey的Cache值
    /// </summary>
    /// <param name="cacheKey"></param>
    /// <param name="objObject"></param>
    public static void SetCache(string cacheKey, object objObject)
    {
    var objCache = HttpRuntime.Cache;
    objCache.Insert(cacheKey, objObject);
    }

    /// <summary>
    /// 设置当前应用程序指定CacheKey的Cache值
    /// </summary>
    /// <param name="cacheKey"></param>
    /// <param name="objObject"></param>
    /// <param name="absoluteExpiration"></param>
    /// <param name="slidingExpiration"></param>
    public static void SetCache(string cacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
    {
    var objCache = HttpRuntime.Cache;
    objCache.Insert(cacheKey, objObject, null, absoluteExpiration, slidingExpiration);
    }
    /// <summary>
    /// 设置数据缓存
    /// </summary>
    public static void SetCache(string cacheKey, object objObject, TimeSpan timeout)
    {
    var objCache = HttpRuntime.Cache;
    objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);
    }
    /// <summary>
    /// 移除指定数据缓存
    /// </summary>
    public static void RemoveAllCache(string cacheKey)
    {
    var cache = HttpRuntime.Cache;
    cache.Remove(cacheKey);
    }
    /// <summary>
    /// 移除全部缓存
    /// </summary>
    public static void RemoveAllCache()
    {
    var cache = HttpRuntime.Cache;
    var cacheEnum = cache.GetEnumerator();
    while (cacheEnum.MoveNext())
    {
    cache.Remove(cacheEnum.Key.ToString());
    }
    }

    private static int CacheMinute = int.Parse(ConfigurationManager.AppSettings["CacheMinute"] ?? "30");
    private const string ApplicationCacheKeyFormat = "Application_{0}";
    private const string UserInfoCacheKeyFormat = "UserInfo_{0}";
    private const string RolePopedomCacheKeyFormat = "RolePopedom_{0}";

    public static void InsertForSlidingExpiration(this Cache cache, string key, object value)
    {
    cache.Insert(key, value, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(CacheMinute));
    }

    public static T Get<T>(this Cache cache, object dataCacheKey, Func<object[], T> getService) where T : class
    {
    var cacheKey = string.Format("{0}_{1}", typeof(T).FullName, dataCacheKey);
    var data = cache.Get(cacheKey) as T;
    if (data != null)
    {
    return data;
    }
    data = getService(new object[] { dataCacheKey });
    if (data != null)
    {
    cache.InsertForSlidingExpiration(cacheKey, data);
    return data;
    }
    return null;
    }

    public static void Remove<T>(this Cache cache, object dataCacheKey)
    {
    var cacheKey = string.Format("{0}_{1}", typeof(T).FullName, dataCacheKey);
    cache.Remove(cacheKey);
    }
    }
    }

  • 相关阅读:
    【新阁教育】能不能让你的电脑变成一台PLC?
    【新阁教育】针对零基础小白的SQL2012安装攻略完整版
    【新阁教育】穷学上位机系列——搭建STEP7仿真环境
    【新阁教育】做了这么久,才知道什么是上位机
    【新阁教育】S7.NET+Log4Net+SQLSugar+MySQL搭建Iot平台
    【新阁教育】基于ModbusTCP实现西门子1200PLC定位控制案例
    C#数据结构-二叉树-链式存储结构
    C#数据结构-二叉树-顺序存储结构
    DataTable 将一列转为List
    字符串匹配—KMP算法
  • 原文地址:https://www.cnblogs.com/special-tao/p/4961224.html
Copyright © 2020-2023  润新知