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

相关阅读 >>

C# 应用npoi获取excel中的图片,保存至本地的算法的图文代码实例详解

详细介绍C#语言中字符类char的使用方法总结

详解C#常用正则验证函数的示例代码

C#使用newtonsoft的json.net进行对象的序列化与反序列化

在c,c ++和c#中的int是什么

手把手教你C#中指针的使用方法

C#最齐全的上传图片方法介绍

C#的socket实现udp协议通信的示例代码详解

详细介绍winformC#获得mac地址,ip地址,子网掩码,默认网关的代码实例(图)

C# windowsapi应用之flashwindowex -实现窗口闪烁的方法详解

更多相关阅读请进入《C#》频道 >>




打赏

取消

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

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

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

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

评论

管理员已关闭评论功能...