另类的缓存技术(存储数据)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System;
2 using System.Web;
3 using System.Web.Caching;
4
5 namespace Mysoft.Cache
6 {
7 /// <summary>
8 /// 缓存生成类
9 /// </summary>
10 public class CacheControl
11 {
12 //const double CacheDuration = 60.0;
13
14 private static double _CacheDuration = 1800.0;
15
16 public static double CacheDuration
17 {
18 get
19 {
20 return _CacheDuration;
21 }
22 set
23 {
24 _CacheDuration = value;
25 }
26 }
27
28 private static string GetCacheKey(string cacheKey, string[] MasterCacheKeyArray)
29 {
30 return string.Concat(MasterCacheKeyArray[0], "-", cacheKey);
31 }
32
33 public static object GetCacheItem(string rawKey, string[] MasterCacheKeyArray)
34 {
35 return HttpRuntime.Cache[GetCacheKey(rawKey, MasterCacheKeyArray)];
36 }
37
38 public static void AddCacheItem(string rawKey, object value, string[] MasterCacheKeyArray)
39 {
40 System.Web.Caching.Cache DataCache = HttpRuntime.Cache;
41
42 // Make sure MasterCacheKeyArray[0] is in the cache - if not, add it
43 if (DataCache[MasterCacheKeyArray[0]] == null)
44 {
45 DataCache[MasterCacheKeyArray[0]] = DateTime.Now;
46 }
47 CacheDependency dependency = new CacheDependency(null, MasterCacheKeyArray);
48 DataCache.Insert(GetCacheKey(rawKey, MasterCacheKeyArray), value, dependency, DateTime.Now.AddSeconds(CacheDuration), System.Web.Caching.Cache.NoSlidingExpiration);
49 }
50
51 public static void InvalidateCache(string[] MasterCacheKeyArray)
52 {
53 // Remove the cache dependency
54 HttpRuntime.Cache.Remove(MasterCacheKeyArray[0]);
55 }
56 }
57 }
2 using System.Web;
3 using System.Web.Caching;
4
5 namespace Mysoft.Cache
6 {
7 /// <summary>
8 /// 缓存生成类
9 /// </summary>
10 public class CacheControl
11 {
12 //const double CacheDuration = 60.0;
13
14 private static double _CacheDuration = 1800.0;
15
16 public static double CacheDuration
17 {
18 get
19 {
20 return _CacheDuration;
21 }
22 set
23 {
24 _CacheDuration = value;
25 }
26 }
27
28 private static string GetCacheKey(string cacheKey, string[] MasterCacheKeyArray)
29 {
30 return string.Concat(MasterCacheKeyArray[0], "-", cacheKey);
31 }
32
33 public static object GetCacheItem(string rawKey, string[] MasterCacheKeyArray)
34 {
35 return HttpRuntime.Cache[GetCacheKey(rawKey, MasterCacheKeyArray)];
36 }
37
38 public static void AddCacheItem(string rawKey, object value, string[] MasterCacheKeyArray)
39 {
40 System.Web.Caching.Cache DataCache = HttpRuntime.Cache;
41
42 // Make sure MasterCacheKeyArray[0] is in the cache - if not, add it
43 if (DataCache[MasterCacheKeyArray[0]] == null)
44 {
45 DataCache[MasterCacheKeyArray[0]] = DateTime.Now;
46 }
47 CacheDependency dependency = new CacheDependency(null, MasterCacheKeyArray);
48 DataCache.Insert(GetCacheKey(rawKey, MasterCacheKeyArray), value, dependency, DateTime.Now.AddSeconds(CacheDuration), System.Web.Caching.Cache.NoSlidingExpiration);
49 }
50
51 public static void InvalidateCache(string[] MasterCacheKeyArray)
52 {
53 // Remove the cache dependency
54 HttpRuntime.Cache.Remove(MasterCacheKeyArray[0]);
55 }
56 }
57 }