zoukankan      html  css  js  c++  java
  • c#Cache的用法

    public class Cache
    {
    /// <summary>
    /// 获取数据缓存
    /// </summary>
    /// <param name="cacheKey">键</param>
    public static object GetCache(string cacheKey)
    {
    var objCache = HttpRuntime.Cache.Get(cacheKey);
    return objCache;
    }
    /// <summary>
    /// 设置数据缓存
    /// </summary>
    public static void SetCache(string cacheKey, object objObject)
    {
    var objCache = HttpRuntime.Cache;
    objCache.Insert(cacheKey, objObject);
    }
    /// <summary>
    /// 设置数据缓存
    /// </summary>
    public static void SetCache(string cacheKey, object objObject, int timeout = 7200)
    {
    try
    {
    if (objObject == null) return;
    var objCache = HttpRuntime.Cache;
    //相对过期
    //objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);
    //绝对过期时间
    objCache.Insert(cacheKey, objObject, null, DateTime.Now.AddSeconds(timeout), TimeSpan.Zero, CacheItemPriority.High, null);
    }
    catch (Exception)
    {
    //throw;
    }
    }
    /// <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());
    }
    }
    }

  • 相关阅读:
    android学习计划
    Android源码下载及开发环境的搭建
    I2C总线时序
    I2C原理和实例
    sql语句获取字段扩展属性
    浏览器兼容:火狐不支持webq格式图片
    js节流函数中的参数传递
    分享插件使用
    图片等比例自适应填充
    window.event在IE和Firefox的异同
  • 原文地址:https://www.cnblogs.com/zwyAndDong/p/9076826.html
Copyright © 2011-2022 走看看