C# Cache缓存帮助类,拿来即用系列

更新时间:2023-12-21 下载TXT文档 下载Word文档

Cache缓存帮助类,拿来即用系列

#region 获取数据缓存
///   
/// 获取数据缓存  
///   
/// 键  
public static T GetCache(string CacheKey, params string[] catchall)
{
    try
    {
        if (catchall != null && catchall.Length > 0)
            foreach (var item in catchall)
                CacheKey += "_" + item;
        System.Web.Caching.Cache objCache = HttpRuntime.Cache;

        var obj = objCache[CacheKey];

        if (obj!=null)
        {
            var ty = obj.GetType();
            if (ty.Name == "DataTable")
            {
                var row = ((DataTable)obj).Select("1=1")[0];
                return DataTableTool.ToModel(row);
            }
        }
                
        if (typeof(T) == (0).GetType() && obj == null)
            return default(T);
        return (T)obj;
    }
    catch (Exception ex)
    {
        throw;
    }
}
#endregion

#region 设置数据缓存
/// 
/// 设置数据缓存
/// 
/// 
/// 
public static void SetCache(string CacheKey, T Data)
{
    System.Web.Caching.Cache objCache = HttpRuntime.Cache;
    objCache.Insert(CacheKey, Data);
}
#endregion

#region 文件缓存依赖,当文件更改时缓存将重新加载
/// 
/// 文件缓存依赖,当文件更改时缓存将重新加载
/// 
/// 
/// 
public static void SetCache(string CacheKey, string FilePath)
{
    System.Web.Caching.Cache objCache = HttpRuntime.Cache;
    string StrValue = IoUtils.FileToString(FilePath, Encoding.UTF8);
    objCache.Insert(CacheKey, StrValue, new CacheDependency(FilePath));
}
#endregion

#region 设置数据缓存 弹性过期时间
/// 
/// 设置数据缓存 弹性过期时间
/// 
/// 
/// 
/// 弹性过期时间,周期内一直使用则一直存在:TimeSpan.FromSeconds(30)
public static void SetCache(string CacheKey, T Data, TimeSpan Timeout)
{
    System.Web.Caching.Cache objCache = HttpRuntime.Cache;
    objCache.Insert(CacheKey, Data, null, DateTime.MaxValue, Timeout, CacheItemPriority.NotRemovable, null);
}
#endregion

#region 设置数据缓存
/// 
/// 设置数据缓存
/// 
/// 
/// 
/// 过期时间
/// 可设置TimeSpan.Zero:表示不使用平滑过期策略
public static void SetCache(string CacheKey, T Data, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
    System.Web.Caching.Cache objCache = HttpRuntime.Cache;
    objCache.Insert(CacheKey, Data, null, absoluteExpiration, slidingExpiration);
}
#endregion

#region 移除指定数据缓存
/// 
/// 移除指定数据缓存
/// 
/// 
public static void RemoveAllCache(string CacheKey)
{
    System.Web.Caching.Cache _cache = HttpRuntime.Cache;
    _cache.Remove(CacheKey);
}
#endregion

#region 移除全部缓存
///   
/// 移除全部缓存  
///   
public static void RemoveAllCache()
{
    System.Web.Caching.Cache _cache = HttpRuntime.Cache;
    IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
    while (CacheEnum.MoveNext())
    {
        _cache.Remove(CacheEnum.Key.ToString());
    }
}
#endregion

#region 以列表形式返回已存在的所有缓存
///   
/// 以列表形式返回已存在的所有缓存   
///   
///    
public static ArrayList ShowAllCache()
{
    ArrayList al = new ArrayList();
    System.Web.Caching.Cache _cache = HttpRuntime.Cache;
    if (_cache.Count > 0)
    {
        IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
        while (CacheEnum.MoveNext())
        {
            al.Add(CacheEnum.Key);
        }
    }
    return al;
}
#endregion



以上就是短码网小编为大家整理的《C# Cache缓存帮助类,拿来即用系列》相关内容,希望大家喜欢。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若内容造成侵权/违法违规/事实不符,请将联系本站反馈,一经查实,立即处理!

C# Cache缓存帮助类,拿来即用系列》文档下载仅供参考学习,下载后请在24小时内删除。

转载注明出处:https://www.duanma.net/article/0d838338258.html

回到顶部