好用的缓存类
1
using System;
2
using System.Collections;
3
using System.Text.RegularExpressions;
4
using System.Web;
5
using System.Web.Caching;
6
7
namespace ChetxNews.Function
8
{
9
public class SiteCache
10
11
{
12
private static readonly Cache _cache;
13
public static readonly int DayFactor;
14
private static int Factor;
15
public static readonly int HourFactor;
16
public static readonly int MinuteFactor;
17
18
static SiteCache()
19
{
20
DayFactor = 17280;
21
HourFactor = 720;
22
MinuteFactor = 12;
23
Factor = 5;
24
_cache = HttpRuntime.Cache;
25
}
26
27
private SiteCache()
28
{
29
}
30
31
public static void Clear()
32
{
33
IDictionaryEnumerator enumerator = _cache.GetEnumerator();
34
while (enumerator.MoveNext())
35
36
{
37
_cache.Remove(enumerator.Key.ToString());
38
}
39
}
40
41
public static object Get(string key)
42
{
43
if(_cache[key] !=null)
44
{
45
return _cache[key];
46
}
47
else
48
{
49
return null;
50
}
51
52
}
53
54
public static void Insert(string key, object obj)
55
{
56
Insert(key, obj, null, 1);
57
}
58
59
public static void Insert(string key, object obj, int seconds)
60
{
61
Insert(key, obj, null, seconds);
62
}
63
64
public static void Insert(string key, object obj, CacheDependency dep)
65
{
66
Insert(key, obj, dep, HourFactor*12);
67
}
68
69
public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
70
{
71
Insert(key, obj, null, seconds, priority);
72
}
73
74
public static void Insert(string key, object obj, CacheDependency dep, int seconds)
75
{
76
Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
77
}
78
79
public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
80
{
81
if (obj != null)
82
83
{
84
_cache.Insert(key, obj, dep, DateTime.Now.AddSeconds((double) (Factor*seconds)), TimeSpan.Zero, priority, null);
85
}
86
}
87
88
public static void Max(string key, object obj)
89
{
90
Max(key, obj, null);
91
}
92
93
public static void Max(string key, object obj, CacheDependency dep)
94
{
95
if (obj != null)
96
97
{
98
_cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);
99
}
100
}
101
102
public static void MicroInsert(string key, object obj, int secondFactor)
103
{
104
if (obj != null)
105
106
{
107
_cache.Insert(key, obj, null, DateTime.Now.AddSeconds((double) (Factor*secondFactor)), TimeSpan.Zero);
108
}
109
}
110
111
public static void Remove(string key)
112
{
113
_cache.Remove(key);
114
}
115
116
public static void RemoveByPattern(string pattern)
117
{
118
IDictionaryEnumerator enumerator = _cache.GetEnumerator();
119
Regex regex1 = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
120
while (enumerator.MoveNext())
121
122
{
123
if (regex1.IsMatch(enumerator.Key.ToString()))
124
125
{
126
_cache.Remove(enumerator.Key.ToString());
127
}
128
}
129
}
130
131
public static void ReSetFactor(int cacheFactor)
132
{
133
Factor = cacheFactor;
134
}
135
136
}
137
}
138
using System;2
using System.Collections;3
using System.Text.RegularExpressions;4
using System.Web;5
using System.Web.Caching;6

7
namespace ChetxNews.Function8
{9
public class SiteCache10
11
{12
private static readonly Cache _cache;13
public static readonly int DayFactor;14
private static int Factor;15
public static readonly int HourFactor;16
public static readonly int MinuteFactor;17
18
static SiteCache()19
{20
DayFactor = 17280;21
HourFactor = 720;22
MinuteFactor = 12;23
Factor = 5;24
_cache = HttpRuntime.Cache;25
}26
27
private SiteCache()28
{29
}30
31
public static void Clear()32
{33
IDictionaryEnumerator enumerator = _cache.GetEnumerator();34
while (enumerator.MoveNext())35
36
{37
_cache.Remove(enumerator.Key.ToString());38
}39
}40
41
public static object Get(string key)42
{43
if(_cache[key] !=null)44
{45
return _cache[key];46
}47
else48
{49
return null;50
}51
52
}53

54
public static void Insert(string key, object obj)55
{56
Insert(key, obj, null, 1);57
}58

59
public static void Insert(string key, object obj, int seconds)60
{61
Insert(key, obj, null, seconds);62
}63

64
public static void Insert(string key, object obj, CacheDependency dep)65
{66
Insert(key, obj, dep, HourFactor*12);67
}68

69
public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)70
{71
Insert(key, obj, null, seconds, priority);72
}73

74
public static void Insert(string key, object obj, CacheDependency dep, int seconds)75
{76
Insert(key, obj, dep, seconds, CacheItemPriority.Normal);77
}78

79
public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)80
{81
if (obj != null)82
83
{84
_cache.Insert(key, obj, dep, DateTime.Now.AddSeconds((double) (Factor*seconds)), TimeSpan.Zero, priority, null);85
}86
}87

88
public static void Max(string key, object obj)89
{90
Max(key, obj, null);91
}92

93
public static void Max(string key, object obj, CacheDependency dep)94
{95
if (obj != null)96
97
{98
_cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);99
}100
}101

102
public static void MicroInsert(string key, object obj, int secondFactor)103
{104
if (obj != null)105
106
{107
_cache.Insert(key, obj, null, DateTime.Now.AddSeconds((double) (Factor*secondFactor)), TimeSpan.Zero);108
}109
}110

111
public static void Remove(string key)112
{113
_cache.Remove(key);114
}115

116
public static void RemoveByPattern(string pattern)117
{118
IDictionaryEnumerator enumerator = _cache.GetEnumerator();119
Regex regex1 = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);120
while (enumerator.MoveNext())121
122
{123
if (regex1.IsMatch(enumerator.Key.ToString()))124
125
{126
_cache.Remove(enumerator.Key.ToString());127
}128
}129
}130

131
public static void ReSetFactor(int cacheFactor)132
{133
Factor = cacheFactor;134
}135
136
}137
}138





浙公网安备 33010602011771号