好用的Cache辅助工具类

话不多说,直接上代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Collections;
  6 using System.Web;
  7 using System.Web.Caching;
  8 
  9 namespace Tools
 10 {
 11     /// <summary>
 12     /// 缓存辅助类
 13     /// </summary>
 14     public static class CacheHelper
 15     {
 16         #region // 绝对缓存过期时间
 17         public static DateTime Absolute_Minute_1
 18         {
 19             get { return DateTime.Now.AddMinutes(1); }
 20         }
 21 
 22         public static DateTime Absolute_Minute_10
 23         {
 24             get { return DateTime.Now.AddMinutes(10); }
 25         }
 26 
 27         public static DateTime Absolute_Minute_30
 28         {
 29             get { return DateTime.Now.AddMinutes(30); }
 30         }
 31 
 32         public static DateTime Absolute_Hour_1
 33         {
 34             get { return DateTime.Now.AddHours(1); }
 35         }
 36 
 37         public static DateTime Absolute_Hour_2
 38         {
 39             get { return DateTime.Now.AddHours(2); }
 40         }
 41 
 42         public static DateTime Absolute_Hour_5
 43         {
 44             get { return DateTime.Now.AddHours(5); }
 45         }
 46 
 47         public static DateTime Absolute_Hour_12
 48         {
 49             get { return DateTime.Now.AddHours(12); }
 50         }
 51 
 52         public static DateTime Absolute_Day_1
 53         {
 54             get { return DateTime.Now.AddDays(1); }
 55         }
 56 
 57         public static DateTime Absolute_Day_7
 58         {
 59             get { return DateTime.Now.AddDays(7); }
 60         }
 61 
 62         public static DateTime Absolute_Day_14
 63         {
 64             get { return DateTime.Now.AddDays(14); }
 65         }
 66 
 67         public static DateTime Absolute_Day_15
 68         {
 69             get { return DateTime.Now.AddDays(15); }
 70         }
 71 
 72         public static DateTime Absolute_Month_1
 73         {
 74             get { return DateTime.Now.AddMonths(1); }
 75         }
 76         #endregion
 77 
 78         #region // 滑动缓存过期时间
 79         public static TimeSpan Sliding_Minute_1
 80         {
 81             get { return new TimeSpan(TimeSpan.TicksPerMinute); }
 82         }
 83 
 84         public static TimeSpan Sliding_Minute_10
 85         {
 86             get { return new TimeSpan(TimeSpan.TicksPerMinute * 10); }
 87         }
 88 
 89         public static TimeSpan Sliding_Minute_30
 90         {
 91             get { return new TimeSpan(TimeSpan.TicksPerMinute * 30); }
 92         }
 93 
 94         public static TimeSpan Sliding_Hour_1
 95         {
 96             get { return new TimeSpan(TimeSpan.TicksPerHour); }
 97         }
 98 
 99         public static TimeSpan Sliding_Hour_2
100         {
101             get { return new TimeSpan(TimeSpan.TicksPerHour * 2); }
102         }
103 
104         public static TimeSpan Sliding_Hour_5
105         {
106             get { return new TimeSpan(TimeSpan.TicksPerHour * 5); }
107         }
108 
109         public static TimeSpan Sliding_Hour_12
110         {
111             get { return new TimeSpan(TimeSpan.TicksPerHour * 12); }
112         }
113 
114         public static TimeSpan Sliding_Day_1
115         {
116             get { return new TimeSpan(TimeSpan.TicksPerDay); }
117         } 
118         #endregion
119 
120         /// <summary>
121         /// 缓存
122         /// </summary>
123         private static Cache cache = HttpRuntime.Cache;
124 
125         /// <summary>
126         /// 根据键获取缓存数据
127         /// </summary>
128         /// <param name="cacheKey">缓存的键</param>
129         /// <returns></returns>
130         private static object GetCache(string cacheKey)
131         {
132             return cache.Get(cacheKey);
133         }
134 
135         /// <summary>
136         ///  设置缓存
137         /// </summary>
138         /// <param name="cacheKey">缓存的键</param>
139         /// <param name="objValue">缓存的值</param>
140         private static void SetCache(string cacheKey, object objValue)
141         {
142             cache.Insert(cacheKey, objValue);
143         }
144 
145         /// <summary>
146         /// 设置缓存
147         /// </summary>
148         /// <param name="cacheKey">缓存的键</param>
149         /// <param name="objValue">缓存的值</param>
150         /// <param name="slidingExpiration">滑动过期时间</param>
151         private static void SetCache(string cacheKey, object objValue, TimeSpan slidingExpiration)
152         {
153             cache.Insert(cacheKey, objValue, null, Cache.NoAbsoluteExpiration, slidingExpiration);
154         }
155 
156         /// <summary>
157         /// 设置缓存
158         /// </summary>
159         /// <param name="cacheKey">缓存的键</param>
160         /// <param name="objValue">缓存的值</param>
161         /// <param name="absoluteExpiration">绝对过期时间</param>
162         private static void SetCache(string cacheKey, object objValue, DateTime absoluteExpiration)
163         {
164             cache.Insert(cacheKey, objValue, null, absoluteExpiration, Cache.NoSlidingExpiration);
165         }
166 
167         /// <summary>
168         /// 设置缓存
169         /// </summary>
170         /// <param name="cacheKey">缓存的键</param>
171         /// <param name="objValue">缓存的值</param>
172         /// <param name="dependency">文件依赖</param>
173         private static void SetCache(string cacheKey, object objValue, CacheDependency dependency)
174         {
175             cache.Insert(cacheKey, objValue, dependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);
176         }
177 
178         /// <summary>
179         /// 移除指定的缓存
180         /// </summary>
181         /// <param name="cacheKey">缓存的键</param>
182         public static void Remove(string cacheKey)
183         {
184             cache.Remove(cacheKey);
185         }
186 
187         /// <summary>
188         /// 移除全部缓存
189         /// </summary>
190         public static void Remove()
191         {
192             IDictionaryEnumerator CacheEnum = cache.GetEnumerator();
193             while (CacheEnum.MoveNext())
194             {
195                 Remove(CacheEnum.Key.ToString());
196             }
197         }
198 
199         /// <summary>
200         /// 删除以cacheKeyPrefix为前缀的缓存Key的缓存
201         /// </summary>
202         /// <param name="cacheKeyPrefix">缓存键前缀</param>
203         public static void RemoveByKeyStartsWith(string cacheKeyPrefix)
204         {
205             IDictionaryEnumerator CacheEnum = cache.GetEnumerator();
206             while (CacheEnum.MoveNext())
207             {
208                 var key = CacheEnum.Key.ToString();
209                 if (key != null && key.StartsWith(cacheKeyPrefix))
210                 {
211                     Remove(key);
212                 }
213             }
214         }
215 
216         /// <summary>
217         /// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。
218         /// </summary>
219         /// <typeparam name="T">缓存的数据类型</typeparam>
220         /// <param name="cacheKey">缓存的键</param>
221         /// <param name="getData">回调方法</param>
222         /// <returns>缓存中的数据</returns>
223         public static T Get<T>(string cacheKey, Func<T> getData)
224         {
225             var data = GetCache(cacheKey);
226             if (data == null)
227             {
228                 data = getData();
229                 SetCache(cacheKey, data);
230             }
231             return (T)data;
232         }
233 
234         /// <summary>
235         /// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。
236         /// </summary>
237         /// <typeparam name="T">缓存的数据类型</typeparam>
238         /// <param name="cacheKey">缓存的键</param>
239         /// <param name="slidingExpiration">滑动过期时间</param>
240         /// <param name="getData">回调方法</param>
241         /// <returns>缓存中的数据</returns>
242         public static T Get<T>(string cacheKey, TimeSpan slidingExpiration, Func<T> getData)
243         {
244             var data = GetCache(cacheKey);
245             if (data == null)
246             {
247                 data = getData();
248                 SetCache(cacheKey, data, slidingExpiration);
249             }
250             return (T)data;
251         }
252 
253         /// <summary>
254         /// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。
255         /// </summary>
256         /// <typeparam name="T">缓存的数据类型</typeparam>
257         /// <param name="cacheKey">缓存的键</param>
258         /// <param name="absoluteExpiration">绝对过期时间</param>
259         /// <param name="getData">回调方法</param>
260         /// <returns>缓存中的数据</returns>
261         public static T Get<T>(string cacheKey, DateTime absoluteExpiration, Func<T> getData)
262         {
263             var data = GetCache(cacheKey);
264             if (data == null)
265             {
266                 data = getData();
267                 SetCache(cacheKey, data, absoluteExpiration);
268             }
269             return (T)data;
270         }
271 
272         /// <summary>
273         /// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。
274         /// </summary>
275         /// <typeparam name="T">缓存的数据类型</typeparam>
276         /// <param name="cacheKey">缓存的键</param>
277         /// <param name="dependency">文件依赖</param>
278         /// <param name="getData">回调方法</param>
279         /// <returns>缓存中的数据</returns>
280         public static T Get<T>(string cacheKey, CacheDependency dependency, Func<T> getData)
281         {
282             var data = GetCache(cacheKey);
283             if (data == null)
284             {
285                 data = getData();
286                 SetCache(cacheKey, data, dependency);
287             }
288             return (T)data;
289         }
290 
291         /// <summary>
292         /// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。
293         /// </summary>
294         /// <typeparam name="T">缓存的数据类型</typeparam>
295         /// <param name="cacheKey">缓存的键</param>
296         /// <param name="filename">依赖的文件路径</param>
297         /// <param name="getData">回调方法</param>
298         /// <returns>缓存中的数据</returns>
299         public static T Get<T>(string cacheKey, string filename, Func<T> getData)
300         {
301             return Get<T>(cacheKey, new CacheDependency(filename), getData);
302         }
303 
304         /// <summary>
305         /// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。
306         /// </summary>
307         /// <typeparam name="T">缓存的数据类型</typeparam>
308         /// <param name="cacheKey">缓存的键</param>
309         /// <param name="filenames">依赖的文件路径</param>
310         /// <param name="getData">回调方法</param>
311         /// <returns>缓存中的数据</returns>
312         public static T Get<T>(string cacheKey, string[] filenames, Func<T> getData)
313         {
314             return Get<T>(cacheKey, new CacheDependency(filenames), getData);
315         }
316     }
317 }

 

转载于:https://www.cnblogs.com/Jarvan-Chan/p/5590126.html

1 安装及卸载......................................................................................................4 1.1 安装需求.......................................................................................................4 1.2 Caché的标准安装..........................................................................................4 1.3 软件许可证的安装........................................................................................6 1.4 卸载Caché...................................................................................................7 2 Caché工具菜单...............................................................................................8 2.1 Caché Cube简介...........................................................................................8 2.2 Studio............................................................................................................9 2.3 Terminal.......................................................................................................11 2.4 Explorer.......................................................................................................11 2.5 SQL Manager..............................................................................................15 2.5.1 用户权限管理...................................................................................16 2.5.2 表格管理..........................................................................................18 2.5.3 数据迁移..........................................................................................19 2.5.4 查看sql查询计划...............................................................................22 2.6 Control Panel..............................................................................................23 2.7 Configuration Manager...............................................................................23 3 配置缓存........................................................................................................29 3.1 缓存简介.....................................................................................................29 3.2 数据缓存配置.............................................................................................30 3.3 程序缓存配置.............................................................................................31 4 指令日志........................................................................................................33 4.1 数据库物理操作日志...................................................................................33 4.1.1 WIJ概述............................................................................................33 4.1.2 WIJ 配置方法...................................................................................34 4.2 数据库指令日志..........................................................................................34 4.2.1 Journaling概述..................................................................................34 4.2.2 Journaling配置方法...........................................................................35 4.2.3 Journal 文件.....................................................................................36 4.2.4 Journal文件设置................................................................................37 4.2.5 查看 Journal 文件内容的方法.........................................................38 4.2.6 Journal 文件管理..............................................................................38 4.3 崩溃缓冲.....................................................................................................38 5 镜像服务........................................................................................................40 5.1 镜像服务原理.............................................................................................40 5.1.1镜像服务的特点..................................................................................41 5.2 镜像服务的配置..........................................................................................41 5.2.1 配置方法..........................................................................................41 5.3 Shadowing和Journaling区别联系................................................................45 6 备份及恢复....................................................................................................46 6.1 备份流程.....................................................................................................46 6.2 备份方法.....................................................................................................46 2/80 Caché5.0.x 数据库管理和维护手册 6.2.1 Caché中备份数据库的方法...............................................................47 6.2.2 其它外部备份方法............................................................................48 6.3 备份的恢复.................................................................................................50 6.3.1 Caché中的数据恢复..........................................................................50 6.3.2 其他方法的数据恢复.........................................................................52 7 ECP网络........................................................................................................53 7.1 ECP原理.....................................................................................................53 7.2 ECP特点.....................................................................................................54 7.3 ECP配置.....................................................................................................55 7.3.1 ECP配置方法....................................................................................55 7.4 ECP 监视和管理.........................................................................................59 7.4.1 ECP 服务器端监视...........................................................................59 7.4.2 ECP 客户端监视...............................................................................59 7.5 ECP的基本状态..........................................................................................59 8 数据库完整性保护..........................................................................................61 8.1数据库完整性检查........................................................................................61 8.1.1 检查所有数据库的完整性.................................................................61 8.1.2 你可以检查所有本地数据库的完整性................................................61 8.1.3 在后台检查所有数据库的完整性.......................................................61 8.1.4 查看后台数据库完整性检查的输出...................................................62 8.1.5 查看个别数据库的完整性.................................................................62 8.2 数据库完整性检查工具...............................................................................62 9 用^GLOSTAT routine获得全局信息...............................................................63 9.1 运行^GLOSTAT.........................................................................................63 9.2 统计数据概况.............................................................................................64 9.3 ^GLOSTAT输出的例子...............................................................................65 9.3.1 例子 A.............................................................................................65 9.3.2 例子 B.............................................................................................66 9.3.3 例子 C.............................................................................................67 9.3.4 例子 D.............................................................................................68 9.3.5 例子 E.............................................................................................70 9.3.6 例子 F..............................................................................................71 9.3.8 例子 G.............................................................................................72 10 Caché 常见问题与回答..........................................................................76 11 联系我们.................................................................................................79 11.1 网络资源...................................................................................................79 11.2 InterSystems公司上海...............................................................................79 11.3 InterSystems公司北京...............................................................................79
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值