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
244
245
246
247
248
249
250
251
252
253
254
255
256
|
//
// System.Web.Caching
//
// Author:
// Patrik Torstensson
// Daniel Cazzulino (dcazzulino@users.sf.net)
//
using System;
using System.Threading;
namespace System.Web.Caching {
/// <summary>
/// Class responsible for representing a cache entry.
/// </summary>
internal class CacheEntry {
internal enum Flags {
Removed = 0,
Public = 1
}
private CacheItemPriority _enumPriority;
private long _longHits;
private byte _byteExpiresBucket;
private int _intExpiresIndex;
private long _ticksExpires;
private long _ticksSlidingExpiration;
private string _strKey;
private object _objItem;
private long _longMinHits;
private Flags _enumFlags;
private CacheDependency _objDependency;
private Cache _objCache;
internal static readonly byte NoBucketHash = byte.MaxValue;
internal static readonly int NoIndexInBucket = int.MaxValue;
internal event CacheItemRemovedCallback _onRemoved;
private ReaderWriterLock _lock = new ReaderWriterLock();
/// <summary>
/// Constructs a new cache entry
/// </summary>
/// <param name="strKey">The cache key used to reference the item.</param>
/// <param name="objItem">The item to be added to the cache.</param>
/// <param name="objDependency">The file or cache key dependencies for the item. When any dependency changes, the object becomes invalid and is removed from the cache. If there are no dependencies, this paramter contains a null reference.</param>
/// <param name="dtExpires">The time at which the added object expires and is removed from the cache. </param>
/// <param name="tsSpan">The interval between the time the added object was last accessed and when that object expires. If this value is the equivalent of 20 minutes, the object expires and is removed from the cache 20 minutes after it is last accessed.</param>
/// <param name="longMinHits">Used to detect and control if the item should be flushed due to under usage</param>
/// <param name="boolPublic">Defines if the item is public or not</param>
/// <param name="enumPriority">The relative cost of the object, as expressed by the CacheItemPriority enumeration. The cache uses this value when it evicts objects; objects with a lower cost are removed from the cache before objects with a higher cost.</param>
internal CacheEntry (Cache objManager, string strKey, object objItem, CacheDependency objDependency, CacheItemRemovedCallback eventRemove,
System.DateTime dtExpires, System.TimeSpan tsSpan, long longMinHits, bool boolPublic, CacheItemPriority enumPriority ) {
if (boolPublic)
_enumFlags |= Flags.Public;
_strKey = strKey;
_objItem = objItem;
_objCache = objManager;
_onRemoved += eventRemove;
_enumPriority = enumPriority;
_ticksExpires = dtExpires.Ticks;
_ticksSlidingExpiration = tsSpan.Ticks;
// If we have a sliding expiration it overrides the absolute expiration (MS behavior)
// This is because sliding expiration causes the absolute expiration to be
// moved after each period, and the absolute expiration is the value used
// for all expiration calculations.
if (tsSpan.Ticks != Cache.NoSlidingExpiration.Ticks)
_ticksExpires = System.DateTime.Now.AddTicks(_ticksSlidingExpiration).Ticks;
_objDependency = objDependency;
if (_objDependency != null)
// Add the entry to the cache dependency handler (we support multiple entries per handler)
_objDependency.Changed += new CacheDependencyChangedHandler (OnChanged);
_longMinHits = longMinHits;
}
internal void OnChanged (object sender, CacheDependencyChangedArgs objDependency) {
_objCache.Remove (_strKey, CacheItemRemovedReason.DependencyChanged);
}
/// <summary>
/// Cleans up the cache entry, removes the cache dependency and calls the remove delegate.
/// </summary>
/// <param name="enumReason">The reason why the cache entry are going to be removed</param>
internal void Close(CacheItemRemovedReason enumReason) {
Delegate [] removedEvents = null;
_lock.AcquireWriterLock(-1);
try {
// Check if the item already is removed
if ((_enumFlags & Flags.Removed) != 0)
return;
_enumFlags |= Flags.Removed;
if (_onRemoved != null)
removedEvents = _onRemoved.GetInvocationList ();
}
finally {
_lock.ReleaseWriterLock();
}
if (removedEvents != null) {
// Call the delegate to tell that we are now removing the entry
if ((_enumFlags & Flags.Public) != 0) {
foreach (Delegate del in removedEvents) {
CacheItemRemovedCallback removed = (CacheItemRemovedCallback) del;
try {
removed (_strKey, _objItem, enumReason);
}
catch (System.Exception obj) {
HttpApplicationFactory.SignalError (obj);
}
}
}
else {
foreach (Delegate del in removedEvents) {
CacheItemRemovedCallback removed = (CacheItemRemovedCallback) del;
try {
removed (_strKey, _objItem, enumReason);
}
catch (Exception) {
}
}
}
}
_lock.AcquireWriterLock(-1);
try {
// If we have a dependency, remove the entry
if (_objDependency != null)
_objDependency.Changed -= new CacheDependencyChangedHandler (OnChanged);
}
finally {
_lock.ReleaseWriterLock();
}
}
internal bool HasUsage {
get {
if (_longMinHits == System.Int64.MaxValue)
return false;
return true;
}
}
internal bool HasAbsoluteExpiration {
get {
if (_ticksExpires == Cache.NoAbsoluteExpiration.Ticks)
return false;
return true;
}
}
internal bool HasSlidingExpiration {
get {
if (_ticksSlidingExpiration == Cache.NoSlidingExpiration.Ticks)
return false;
return true;
}
}
internal byte ExpiresBucket {
get {
return _byteExpiresBucket;
}
set {
_byteExpiresBucket = value;
}
}
internal int ExpiresIndex {
get {
return _intExpiresIndex;
}
set {
_intExpiresIndex = value;
}
}
internal long Expires {
get {
return _ticksExpires;
}
set {
_ticksExpires = value;
}
}
internal long SlidingExpiration {
get {
return _ticksSlidingExpiration;
}
}
internal object Item {
get {
return _objItem;
}
}
internal string Key {
get {
return _strKey;
}
}
internal long Hits {
get {
return _longHits;
}
}
internal long MinimumHits {
get {
return _longMinHits;
}
}
internal CacheItemPriority Priority {
get {
return _enumPriority;
}
}
internal bool IsPublic {
get {
return (_enumFlags & Flags.Public) != 0;
}
}
internal void Hit () {
Interlocked.Increment (ref _longHits);
}
}
}
|