C# 实现 CacheHelper

C# 实现 CacheHelper


using System;
using System.Web;
using System.Web.Caching;

public class CacheHelper
{
    /// <summary>
    /// 创建缓存
    /// </summary>
    /// <param name="key">缓存Key</param>
    /// <param name="obj">object对象</param>
    public static void Insert(string key, object obj)
    {
        //创建缓存
        HttpContext.Current.Cache.Insert(key, obj);
    }

    /// <summary>
    /// 移除缓存
    /// </summary>
    /// <param name="key">缓存Key</param>
    public static void Remove(string key)
    {
        //创建缓存
        HttpContext.Current.Cache.Remove(key);
    }

    /// <summary>
    /// 创建缓存项的文件依赖
    /// </summary>
    /// <param name="key">缓存Key</param>
    /// <param name="obj">object对象</param>
    /// <param name="fileName">文件绝对路径</param>
    public static void Insert(string key, object obj, string fileName)
    {
        //创建缓存依赖项
        CacheDependency dep = new CacheDependency(fileName);
        //创建缓存
        HttpContext.Current.Cache.Insert(key, obj, dep);
    }

    /// <summary>
    /// 创建缓存项过期
    /// </summary>
    /// <param name="key">缓存Key</param>
    /// <param name="obj">object对象</param>
    /// <param name="expires">过期时间(分钟)</param>
    public static void Insert(string key, object obj, int expires)
    {
        HttpContext.Current.Cache.Insert(key, obj, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, expires, 0));
    }

    /// <summary>
    /// 获取缓存对象
    /// </summary>
    /// <param name="key">缓存Key</param>
    /// <returns>object对象</returns>
    public static object Get(string key)
    {
        if (string.IsNullOrEmpty(key))
        {
            return null;
        }
        return HttpContext.Current.Cache.Get(key);
    }

    /// <summary>
    /// 获取缓存对象
    /// </summary>
    /// <typeparam name="T">T对象</typeparam>
    /// <param name="key">缓存Key</param>
    /// <returns></returns>
    public static T Get<T>(string key)
    {
        object obj = Get(key);
        return obj == null ? default(T) : (T)obj;
    }
    /// <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());
        }
    }
}


欢迎分享,(木庄网络博客交流QQ群:562366239

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论