好用的缓存类
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

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
