VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#教程之Enterprise Library for .NET Framework 2.0缓存使用

Enterprise Library for .NET Framework 2.0 是微软发布的企业库,它支持.NET Framework 2.0。并且由一系列的企业应用程序块来构成。本文即以实例展示了Enterprise Library for .NET Framework 2.0缓存的使用方法,供大家参考。

关键代码如下:

?
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using System;
 
namespace ETLUtilHelpV2
{
  /// <summary>
  /// Enterprise Library for .NET Framework 2.0 缓存工具类
  /// </summary>
  public class ETLCacheToolV2
  {
    /*
     *在Caching Application Block中,主要提供以下四种保存缓存数据的途径,
     *分别是:内存存储(默认)、独立存储(Isolated Storage)、
     *数据库存储(DataBase Cache Storage)和自定义存储(Custom Cache Storage)。
     *In-Memory:保存在内存中。 
     *Isolated Storage Cache Store:系统将缓存的信息保存在独立文件中(C:\Users\<<user name>>\AppData\Local\IsolatedStorage)。
     *Data Cache Storage:将缓存数据保存在数据库中。(需要运行CreateCachingDatabase.sql脚本)
     *Custom Cache Storage:自己扩展的处理器。我们可以将数据保存在注册表中或文本文件中。
     *
     * 缓存等级,在企业库的缓存模块中已经提供了4个缓存等级:Low,Normal,High和NotRemovable,在超出最大缓存数量后会自动根据缓存等级来移除对象。
     * 过期方式,企业库默认提供4种过期方式
     * AbsoluteTime:绝对是时间过期,传递一个时间对象指定到时过期
     * SlidingTime:缓存在最后一次访问之后多少时间后过期,默认为2分钟,有2个构造函数可以指定一个过期时间或指定一个过期时间和一个最后使用时
     * ExtendedFormatTime :指定过期格式,以特定的格式来过期,通过ExtendedFormat.cs类来包装过期方式,具体可参照ExtendedFormat.cs,源代码中已经给出了很多方式
     * FileDependency:依赖于文件过期,当所依赖的文件被修改则过期,这个我觉得很有用,因为在许多网站,如论坛、新闻系统等都需要大量的配置,可以将配置文件信息进行缓存,将依赖项设为配置文件,这样当用户更改了配置文件后通过ICacheItemRefreshAction.Refresh可以自动重新缓存。
     */
 
    ///// <summary>
    ///// 自定义缓存刷新操作
    ///// </summary>
    //[Serializable]
    //public class CacheItemRefreshAction : ICacheItemRefreshAction
    //{
    //  #region ICacheItemRefreshAction 成员
    //  /// <summary>
    //  /// 自定义刷新操作
    //  /// </summary>
    //  /// <param name="removedKey">移除的键</param>
    //  /// <param name="expiredValue">过期的值</param>
    //  /// <param name="removalReason">移除理由</param>
    //  void ICacheItemRefreshAction.Refresh(string removedKey, object expiredValue, CacheItemRemovedReason removalReason)
    //  {
    //    if (removalReason == CacheItemRemovedReason.Expired)
    //    {
    //      CacheManager cache = CacheFactory.GetCacheManager();
    //      cache.Add(removedKey, expiredValue);
    //    }
    //  }
    //  #endregion
    //}
 
    static CacheManager CacheMgr = null;
    static ETLCacheToolV2()
    {
      CacheMgr = CacheFactory.GetCacheManager();
    }
    /// <summary>
    /// 获取CacheManager实例
    /// </summary>
    /// <returns>CacheManager</returns>
    public static CacheManager Instance()
    {
      return CacheMgr;
    }
    /// <summary>
    /// 添加缓存
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    public static void Add(string key, object value)
    {
      CacheMgr.Add(key, value);
    }
    /// <summary>
    /// 添加缓存_滑动过期_小时
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="hour">小时</param>
    public static void AddWithHour(string key, object value, int hour)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromHours(hour)));
    }
    /// <summary>
    /// 添加缓存_滑动过期_天
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="days">天</param>
    public static void AddWithDay(string key, object value, int days)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromDays(days)));
    }
    /// <summary>
    /// 添加缓存_滑动过期_毫秒
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="millisecond">毫秒</param>
    public static void AddWithMillisecond(string key, object value, int millisecond)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromMilliseconds(millisecond)));
    }
    /// <summary>
    ///添加缓存_滑动过期_分钟
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="minutes">分钟</param>
    public static void AddWithMinutes(string key, object value, int minutes)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromMinutes(minutes)));
    }
    /// <summary>
    ///添加缓存_滑动过期_秒
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="seconds">秒</param>
    public static void AddWithSeconds(string key, object value, int seconds)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromSeconds(seconds)));
    }
    /// <summary>
    /// 添加缓存_滑动过期_文件依赖
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="filePath">文件路径</param>
    public static void AddFileDependency(string key, object value, string filePath)
    {
      FileDependency _fileDependency = new FileDependency(filePath);
      CacheMgr.Add(key, value, CacheItemPriority.Normal, null, _fileDependency);
    }
    /// <summary>
    /// 添加缓存_滑动过期_小时
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="hour">小时</param>
    /// <param name="refreshAction">ICacheItemRefreshAction</param>
    public static void AddWithHour(string key, object value, int hour, ICacheItemRefreshAction refreshAction)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, new SlidingTime(TimeSpan.FromHours(hour)));
    }
    /// <summary>
    /// 添加缓存_滑动过期_天
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="days">天</param>
    /// <param name="refreshAction">ICacheItemRefreshAction</param>
    public static void AddWithDay(string key, object value, int days, ICacheItemRefreshAction refreshAction)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, new SlidingTime(TimeSpan.FromDays(days)));
    }
    /// <summary>
    /// 添加缓存_滑动过期_毫秒
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="millisecond">毫秒</param>
    /// <param name="refreshAction">ICacheItemRefreshAction</param>
    public static void AddWithMillisecond(string key, object value, int millisecond, ICacheItemRefreshAction refreshAction)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, new SlidingTime(TimeSpan.FromMilliseconds(millisecond)));
    }
    /// <summary>
    /// 添加缓存_滑动过期_分钟
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="minutes">分钟</param>
    /// <param name="refreshAction">ICacheItemRefreshAction</param>
    public static void AddWithMinutes(string key, object value, int minutes, ICacheItemRefreshAction refreshAction)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, new SlidingTime(TimeSpan.FromMinutes(minutes)));
    }
    /// <summary>
    /// 添加缓存_滑动过期_秒
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="seconds">秒</param>
    /// <param name="refreshAction">ICacheItemRefreshAction</param>
    public static void AddWithSeconds(string key, object value, int seconds, ICacheItemRefreshAction refreshAction)
    {
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, new SlidingTime(TimeSpan.FromSeconds(seconds)));
    }
    /// <summary>
    /// 添加缓存_滑动过期_文件依赖
    /// </summary>
    /// <param name="key">键</param>
    /// <param name="value">值</param>
    /// <param name="filePath">文件路径</param>
    /// <param name="refreshAction">ICacheItemRefreshAction</param>
    public static void AddFileDependency(string key, object value, string filePath, ICacheItemRefreshAction refreshAction)
    {
      FileDependency _fileDependency = new FileDependency(filePath);
      CacheMgr.Add(key, value, CacheItemPriority.Normal, refreshAction, _fileDependency);
    }
    /// <summary>
    /// 清空缓存
    /// </summary>
    public static void Flush()
    {
      CacheMgr.Flush();
    }
    /// <summary>
    /// 移出缓存
    /// </summary>
    /// <param name="key"></param>
    public static void Remove(string key)
    {
      if (CacheMgr.Contains(key))
        CacheMgr.Remove(key);
    }
    /// <summary>
    /// 获取缓存
    /// </summary>
    /// <param name="key">键</param>
    /// <returns>值</returns>
    public static object GetData(string key)
    {
      if (CacheMgr.Contains(key))
        return CacheMgr.GetData(key);
      return null;
    }
    /// <summary>
    /// 获取缓存
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    /// <param name="key">键</param>
    /// <returns>值</returns>
    public static T GetData<T>(string key)
    {
      if (CacheMgr.Contains(key))
        return (T)CacheMgr.GetData(key);
      return default(T);
    }
  }
}

读者可在自身项目中对上述代码加以测试,相信会对大家的C#程序设计起到一定的帮助作用。


相关教程