Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CacheEntry.cs « System.Web.Caching « System.Web « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0943be5db302a1ca4822f1fc155f803df62c1a2c (plain)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// 
// System.Web.Caching
//
// Author:
//   Patrik Torstensson (Patrik.Torstensson@labs2.com)
//
// (C) Copyright Patrik Torstensson, 2001
//
namespace System.Web.Caching
{
	/// <summary>
	/// Class responsible for representing a cache entry.
	/// </summary>
	public class CacheEntry
	{
		/// <summary>
		/// Defines the status of the current cache entry
		/// </summary>
		public 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;

		/// <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>
		public 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)
			{
				SetFlag(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)
			if (tsSpan.Ticks != System.TimeSpan.Zero.Ticks)
			{
				_ticksExpires = System.DateTime.Now.AddTicks(_ticksSlidingExpiration).Ticks;
			}
			
			_objDependency = objDependency;
			if (_objDependency != null)
			{
				if (_objDependency.IsDisposed)
				{
					throw new System.ObjectDisposedException("System.Web.CacheDependency");
				}

				// Add the entry to the cache dependency handler (we support multiple entries per handler)
				_objDependency.Changed += new CacheDependency.CacheDependencyCallback(OnChanged); 
			}

			_longMinHits = longMinHits;
		}

		private event CacheItemRemovedCallback _onRemoved;

		public void OnChanged(CacheDependency 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>
		public void Close(CacheItemRemovedReason enumReason)
		{	
			lock(this)
			{
				// Check if the item already is removed
				if (TestFlag(Flags.Removed))
				{
					return;
				}

				SetFlag(Flags.Removed);

				if (_onRemoved != null)
				{
					// Call the delegate to tell that we are now removing the entry
					try 
					{
						_onRemoved(_strKey, _objItem, enumReason);		
					}
					catch (System.Exception objException)
					{
						System.Diagnostics.Debug.Fail("System.Web.CacheEntry.Close() Exception when calling remove delegate", "Message: " + objException.Message + " Stack: " + objException.StackTrace + " Source:" + objException.Source);
					}
				}

				// If we have a dependency, remove the entry
				if (_objDependency != null)
				{
					_objDependency.Changed -= new CacheDependency.CacheDependencyCallback(OnChanged);
					if (!_objDependency.HasEvents)
					{
						_objDependency.Dispose();
					}
				}
			}
		}

		/// <summary>
		/// Tests a specific flag is set or not.
		/// </summary>
		/// <param name="oFlag">Flag to test agains</param>
		/// <returns>Returns true if the flag is set.</returns>
		public bool TestFlag(Flags oFlag)
		{
			lock(this) 
			{
				if ((_enumFlags & oFlag) != 0)
				{
					return true;
				} 

				return false;
			}
		}

		/// <summary>
		/// Sets a specific flag.
		/// </summary>
		/// <param name="oFlag">Flag to set.</param>
		public void SetFlag(Flags oFlag)
		{
			lock (this)
			{
				_enumFlags |= oFlag;
			}
		}

		/// <summary>
		/// Returns true if the object has minimum hit usage flushing enabled.
		/// </summary>
		public bool HasUsage
		{
			get { 
				if (_longMinHits == System.Int64.MaxValue) 
				{
					return false;
				}

				return true;
			}
		}

		/// <summary>
		/// Returns true if the entry has absolute expiration.
		/// </summary>
		public bool HasAbsoluteExpiration
		{
			get 
			{ 
				if (_ticksExpires == System.DateTime.MaxValue.Ticks) 
				{
					return false;
				}

				return true;
			}
		}

		/// <summary>
		/// Returns true if the entry has sliding expiration enabled.
		/// </summary>
		public bool HasSlidingExpiration
		{
			get 
			{ 
				if (_ticksSlidingExpiration == System.TimeSpan.Zero.Ticks) 
				{
					return false;
				}

				return true;
			}
		}
		
		/// <summary>
		/// Gets and sets the current expires bucket the entry is active in.
		/// </summary>
		public byte ExpiresBucket
		{
			get 
			{ 
				lock (this) 
				{
					return _byteExpiresBucket; 
				}
			}
			set 
			{ 
				lock (this)
				{
					_byteExpiresBucket = ExpiresBucket; 
				}
			}
		}

		/// <summary>
		/// Gets and sets the current index in the expires bucket of the current cache entry.
		/// </summary>
		public int ExpiresIndex
		{
			get 
			{ 
				lock (this)
				{
					return _intExpiresIndex; 
				}
			}
			
			set 
			{ 
				lock (this)
				{
					_intExpiresIndex = ExpiresIndex; 
				}
			}
		}
        
		/// <summary>
		/// Gets and sets the expiration of the cache entry.
		/// </summary>
		public long Expires
		{
			get 
			{ 
				lock (this)
				{
					return _ticksExpires; 
				}
			}
			set 
			{ 
				lock (this)
				{
					_ticksExpires = Expires; 
				}
			}
		}

		/// <summary>
		/// Gets the sliding expiration value. The return value is in ticks (since 0/0-01 in 100nanosec)
		/// </summary>
		public long SlidingExpiration
		{
			get 
			{ 
				return _ticksSlidingExpiration; 
			}
		}

		/// <summary>
		/// Returns the current cached item.
		/// </summary>
		public object Item
		{
			get
			{
				return _objItem; 
			}
		}

		/// <summary>
		/// Returns the current cache identifier.
		/// </summary>
		public string Key
		{
			get 
			{ 
				return _strKey; 
			}
		}

		/// <summary>
		/// Gets and sets the current number of hits on the cache entry.
		/// </summary>
		public long Hits
		{
			// todo: Could be optimized by using interlocked methods..
			get 
			{
				lock (this)
				{
					return _longHits; 
				}
			}
			set 
			{ 
				lock (this)
				{
					_longHits = Hits; 
				}
			}
		}

		/// <summary>
		/// Returns minimum hits for the usage flushing rutine.
		/// </summary>
		public long MinimumHits
		{
			get 
			{ 
				return _longMinHits; 
			}
		}

		/// <summary>
		/// Returns the priority of the cache entry.
		/// </summary>
		public CacheItemPriority Priority
		{
			get 
			{ 
				return _enumPriority; 
			}
		}
	}
}