jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Web;
using System.Web.Caching;
using System.Collections;
using System.Text.RegularExpressions;
 
namespace HH.WMS.Utils
{
    /// <summary>
    /// <para> </para>
    ///  常用工具类——缓存类
    /// <para> --------------------------------------------------------------------------------</para>
    /// <para> SetCache:设置当前应用程序指定CacheKey的Cache值[ +2方法重载 ]</para>
    /// <para> GetCache:获取当前应用程序指定CacheKey的Cache值</para>
    /// <para> RemoveCache:删除缓存</para>
    /// </summary>
    public class ZCache
    {
 
        static Cache GetCacheObject()
        {
            HttpContext context = HttpContext.Current;
            if (context != null)
            {
                return context.Cache;
            }
            else
            {
                return HttpRuntime.Cache;
            }
        }
 
        #region 设置当前应用程序指定CacheKey的Cache值
        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey">键</param>
        /// <param name="objObject"></param>
        public static void SetCache(string CacheKey, object objObject)
        {
            System.Web.Caching.Cache objCache = GetCacheObject();
            objCache.Insert(CacheKey, objObject);
        }
        #endregion
 
        #region 获取当前应用程序指定CacheKey的Cache值
        /// <summary>
        /// 获取当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <returns></returns>
        public static object GetCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = GetCacheObject();
            return objCache[CacheKey];
        }
        #endregion
 
        #region 设置当前应用程序指定CacheKey的Cache值[带过期时间等参数]
        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        /// <param name="absoluteExpiration">可设置为Cache.NoAbsoluteExpiration</param>
        /// <param name="slidingExpiration">可设置为Cache.NoSlidingExpiration</param>
        public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            System.Web.Caching.Cache objCache = GetCacheObject();
            objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
        }
        #endregion
 
        #region 删除缓存
        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="CacheKey"></param>
        public static void RemoveCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = GetCacheObject();
            objCache.Remove(CacheKey);
        }
        #endregion
 
        /// <summary>
        /// 清空Cash对象
        /// </summary>
        public static void Clear()
        {
            Cache _cache = GetCacheObject();
            IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            ArrayList al = new ArrayList();
            while (CacheEnum.MoveNext())
            {
                al.Add(CacheEnum.Key);
            }
            foreach (string key in al)
            {
                _cache.Remove(key);
            }
        }
 
        /// <summary>
        /// 根据正则表达式的模式移除Cache
        /// </summary>
        /// <param name="pattern">模式</param>
        public static void RemoveByPattern(string pattern)
        {
            Cache _cache = GetCacheObject();
            IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
            while (CacheEnum.MoveNext())
            {
                if (regex.IsMatch(CacheEnum.Key.ToString()))
                    _cache.Remove(CacheEnum.Key.ToString());
            }
        }
    }
}