C#语法糖之 cache操作类 asp.net

因为考虑到我下面我将写session cookies 等 操作类 ,与cache具有共性。 所以都统一继承了IHttpStorageObject  abstract class 来保函数风格的统一 ,但是又为了调用方便,抽象中又使用了单例来简化调用。

 

 

使用方法很简单:

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Collections;
using System.Linq.Expressions;
 
namespace SyntacticSugar
{
    /// <summary>
    /// ** 描述:缓存操作类
    /// ** 创始时间:2015-6-9
    /// ** 修改时间:-
    /// ** 作者:sunkaixuan
    /// ** 使用说明:http://www.cnblogs.com/sunkaixuan/p/4563462.html
    /// </summary>
    /// <typeparam name="K">键</typeparam>
    /// <typeparam name="V">值</typeparam>
    public class CacheManager<V> : IHttpStorageObject<V>
    {
 
        #region 全局变量
        private static CacheManager<V> _instance = null;
        private static readonly object _instanceLock = new object();
        #endregion
 
        #region 构造函数
 
        private CacheManager() { }
        #endregion
 
        #region  属性
        /// <summary>        
        ///根据key获取value    
        /// </summary>        
        /// <value></value>     
        public override V this[string key]
        {
            get { return (V)HttpRuntime.Cache[CreateKey(key)]; }
        }
        #endregion
 
        #region 公共函数
 
        /// <summary>        
        /// key是否存在      
        /// </summary>        
        /// <param name="key">key</param>        
        /// <returns> ///     存在<c>true</c> 不存在<c>false</c>.        /// /// </returns>        
        public override bool ContainsKey(string key)
        {
            return HttpRuntime.Cache[CreateKey(key)] != null;
        }
 
        /// <summary>        
        /// 获取缓存值        
        /// </summary>        
        /// <param name="key">key</param>        
        /// <returns></returns>        
        public override V Get(string key)
        {
            return (V)HttpRuntime.Cache.Get(CreateKey(key));
        }
 
        /// <summary>        
        /// 获取实例 (单例模式)      
        /// </summary>        
        /// <returns></returns>        
        public static CacheManager<V> GetInstance()
        {
            if (_instance == null)
                lock (_instanceLock)
                    if (_instance == null)
                        _instance = new CacheManager<V>();
            return _instance;
        }
 
        /// <summary>        
        /// 插入缓存(默认20分钟)       
        /// </summary>        
        /// <param name="key"> key</param>        
        /// <param name="value">value</param>         
        public override void Add(string key, V value)
        {
            Add(key, value, Minutes*20);
        }
 
        /// <summary>        
        /// 插入缓存       
        /// </summary>        
        /// <param name="key"> key</param>        
        /// <param name="value">value</param>        
        /// <param name="cacheDurationInSeconds">过期时间单位秒</param>        
        public  void Add(string key, V value, int cacheDurationInSeconds)
        {
            Add(key, value, cacheDurationInSeconds, CacheItemPriority.Default);
        }
 
        /// <summary>        
        /// 插入缓存.        
        /// </summary>        
        /// <param name="key">key</param>        
        /// <param name="value">value</param>        
        /// <param name="cacheDurationInSeconds">过期时间单位秒</param>        
        /// <param name="priority">缓存项属性</param>        
        public void Add(string key, V value, int cacheDurationInSeconds, CacheItemPriority priority)
        {
            string keyString = CreateKey(key);
            HttpRuntime.Cache.Insert(keyString, value, null, DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null);
        }
 
        /// <summary>        
        /// 插入缓存.        
        /// </summary>        
        /// <param name="key">key</param>        
        /// <param name="value">value</param>        
        /// <param name="cacheDurationInSeconds">过期时间单位秒</param>        
        /// <param name="priority">缓存项属性</param>        
        public  void Add(string key, V value, int cacheDurationInSeconds, CacheDependency dependency, CacheItemPriority priority)
        {
            string keyString = CreateKey(key);
            HttpRuntime.Cache.Insert(keyString, value, dependency, DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null);
        }
 
        /// <summary>        
        /// 删除缓存        
        /// </summary>        
        /// <param name="key">key</param>        
        public override void Remove(string key)
        {
            HttpRuntime.Cache.Remove(CreateKey(key));
        }
 
        /// <summary>
        /// 清除所有缓存
        /// </summary>
        public override void RemoveAll()
        {
            System.Web.Caching.Cache cache = HttpRuntime.Cache;
            IDictionaryEnumerator CacheEnum = cache.GetEnumerator();
            ArrayList al = new ArrayList();
            while (CacheEnum.MoveNext())
            {
                al.Add(CacheEnum.Key);
            }
            foreach (string key in al)
            {
                cache.Remove(key);
            }
        }
 
        /// <summary>
        /// 清除所有包含关键字的缓存
        /// </summary>
        /// <param name="removeKey">关键字</param>
        public override void RemoveAll(Func<string, bool> removeExpression)
        {
            System.Web.Caching.Cache _cache = HttpRuntime.Cache;
            var allKeyList = GetAllKey();
            var delKeyList = allKeyList.Where(removeExpression).ToList();
            foreach (var key in delKeyList)
            {
                Remove(key);
            }
        }
 
        /// <summary>
        /// 获取所有缓存key
        /// </summary>
        /// <returns></returns>
        public override IEnumerable<string> GetAllKey()
        {
            IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                yield return CacheEnum.Key.ToString();
            }
        }
        #endregion
 
        #region 私有函数
 
        /// <summary>        
        ///创建KEY  
        /// </summary>        
        /// <param name="key">Key</param>        
        /// <returns></returns>        
        private string CreateKey(string key)
        {
            return "http_cache_"+key.ToString();
        }
 
        #endregion
 
 
 
 
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
namespace SyntacticSugar
{
    public abstract class IHttpStorageObject<V>
    {
 
        public int Minutes = 60;
        public int Hour = 60 * 60;
        public int Day = 60 * 60 * 24;
        public System.Web.HttpContext context = System.Web.HttpContext.Current;
        public abstract void Add(string key, V value);
        public abstract bool ContainsKey(string key);
        public abstract V Get(string key);
        public abstract global::System.Collections.Generic.IEnumerable<string> GetAllKey();
        public abstract void Remove(string key);
        public abstract void RemoveAll();
        public abstract void RemoveAll(Func<string, bool> removeExpression);
        public abstract V this[string key] { get; }
    }
}

  

posted @   阿妮亚  阅读(1722)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示