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());
    }
    }
    }

  • 相关阅读:
    数据库范式
    将DBF,XLS,XML,MDB文件导入C#DataGrid的方法
    在类文件中引用Server对象
    在使用了母版页的页面里加载css和js文件
    IIS 7.5 URL重写参数
    hdu Can you solve this equation?
    hdu Dome of Circus
    hdu Bone Collector
    hdu Turn the corner
    hdu 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
  • 原文地址:https://www.cnblogs.com/zwyAndDong/p/9076826.html
Copyright © 2011-2022 走看看