zoukankan      html  css  js  c++  java
  • 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);
    }
    }
    }

  • 相关阅读:
    【BZOJ】4636: 蒟蒻的数列
    BZOJ1878 [SDOI2009]HH的项链
    【网络流24题----02】太空飞行计划
    【网络流24题----03】Air Raid最小路径覆盖
    【网络流24题----01】飞行员配对方案问题
    素数判定(米勒测试定理-费马小定理+快速乘)
    一堆模板(丑陋0.0)------数据结构
    丑数(USACO)
    NOI[2001]食物链
    关于Tarjan(2)
  • 原文地址:https://www.cnblogs.com/special-tao/p/4961224.html
Copyright © 2011-2022 走看看