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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
|
//
// System.Web.Caching.Cache
//
// Author(s):
// Lluis Sanchez (lluis@ximian.com)
// Marek Habersack <mhabersack@novell.com>
//
// (C) 2005-2009 Novell, Inc (http://novell.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
using System.Web.Configuration;
namespace System.Web.Caching
{
// CAS - no InheritanceDemand here as the class is sealed
[AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class Cache: IEnumerable
{
const int LOW_WATER_MARK = 10000; // Target number of items if high water mark is reached
const int HIGH_WATER_MARK = 15000; // We start collection after exceeding this count
public static readonly DateTime NoAbsoluteExpiration = DateTime.MaxValue;
public static readonly TimeSpan NoSlidingExpiration = TimeSpan.Zero;
// cacheLock will be released in the code below without checking whether it was
// actually acquired. The API doesn't offer a reliable way to check whether the lock
// is being held by the current thread and since Mono does't implement CER
// (Constrained Execution Regions -
// http://msdn.microsoft.com/en-us/library/ms228973.aspx) currently, we have no
// reliable way of recording the information that the lock has been successfully
// acquired.
// It can happen that a Thread.Abort occurs while acquiring the lock and the lock
// isn't actually held. In this case the attempt to release a lock will throw an
// exception. It's better than a race of setting a boolean flag after acquiring the
// lock and then relying upon it here to release it - that may cause a deadlock
// should we fail to release the lock which was successfully acquired but
// Thread.Abort happened right after that during the stloc instruction to set the
// boolean flag. Once CERs are supported we can use the boolean flag reliably.
ReaderWriterLockSlim cacheLock;
CacheItemLRU cache;
CacheItemPriorityQueue timedItems;
Timer expirationTimer;
long expirationTimerPeriod = 0;
Cache dependencyCache;
bool? disableExpiration;
long privateBytesLimit = -1;
long percentagePhysicalMemoryLimit = -1;
bool DisableExpiration {
get {
if (disableExpiration == null) {
var cs = WebConfigurationManager.GetWebApplicationSection ("system.web/caching/cache") as CacheSection;
if (cs == null)
disableExpiration = false;
else
disableExpiration = (bool)cs.DisableExpiration;
}
return (bool)disableExpiration;
}
}
public long EffectivePrivateBytesLimit {
get {
if (privateBytesLimit == -1) {
var cs = WebConfigurationManager.GetWebApplicationSection ("system.web/caching/cache") as CacheSection;
if (cs == null)
privateBytesLimit = 0;
else
privateBytesLimit = cs.PrivateBytesLimit;
if (privateBytesLimit == 0) {
// http://blogs.msdn.com/tmarq/archive/2007/06/25/some-history-on-the-asp-net-cache-memory-limits.aspx
// TODO: calculate
privateBytesLimit = 734003200;
}
}
return privateBytesLimit;
}
}
public long EffectivePercentagePhysicalMemoryLimit {
get {
if (percentagePhysicalMemoryLimit == -1) {
var cs = WebConfigurationManager.GetWebApplicationSection ("system.web/caching/cache") as CacheSection;
if (cs == null)
percentagePhysicalMemoryLimit = 0;
else
percentagePhysicalMemoryLimit = cs.PercentagePhysicalMemoryUsedLimit;
if (percentagePhysicalMemoryLimit == 0) {
// http://blogs.msdn.com/tmarq/archive/2007/06/25/some-history-on-the-asp-net-cache-memory-limits.aspx
// TODO: calculate
percentagePhysicalMemoryLimit = 97;
}
}
return percentagePhysicalMemoryLimit;
}
}
public Cache ()
{
cacheLock = new ReaderWriterLockSlim ();
cache = new CacheItemLRU (this, HIGH_WATER_MARK, LOW_WATER_MARK);
}
public int Count {
get { return cache.Count; }
}
public object this [string key] {
get { return Get (key); }
set { Insert (key, value); }
}
// Must ALWAYS be called with the cache write lock held
CacheItem RemoveCacheItem (string key)
{
if (key == null)
return null;
CacheItem ret = cache [key];
if (ret == null)
return null;
if (timedItems != null)
timedItems.OnItemDisable (ret);
ret.Disabled = true;
cache.Remove (key);
return ret;
}
public object Add (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
{
if (key == null)
throw new ArgumentNullException ("key");
try {
cacheLock.EnterWriteLock ();
CacheItem it = cache [key];
if (it != null)
return it.Value;
Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback, null, false);
} finally {
// See comment at the top of the file, above cacheLock declaration
cacheLock.ExitWriteLock ();
}
return null;
}
public object Get (string key)
{
try {
cacheLock.EnterUpgradeableReadLock ();
CacheItem it = cache [key];
if (it == null)
return null;
if (it.Dependency != null && it.Dependency.HasChanged) {
try {
cacheLock.EnterWriteLock ();
if (!NeedsUpdate (it, CacheItemUpdateReason.DependencyChanged, false))
Remove (it.Key, CacheItemRemovedReason.DependencyChanged, false, true);
} finally {
// See comment at the top of the file, above cacheLock declaration
cacheLock.ExitWriteLock ();
}
return null;
}
if (!DisableExpiration) {
if (it.SlidingExpiration != NoSlidingExpiration) {
it.AbsoluteExpiration = DateTime.Now + it.SlidingExpiration;
// Cast to long is ok since we know that sliding expiration
// is less than 365 days (31536000000ms)
long remaining = (long)it.SlidingExpiration.TotalMilliseconds;
it.ExpiresAt = it.AbsoluteExpiration.Ticks;
if (expirationTimer != null && (expirationTimerPeriod == 0 || expirationTimerPeriod > remaining)) {
expirationTimerPeriod = remaining;
expirationTimer.Change (expirationTimerPeriod, expirationTimerPeriod);
}
} else if (DateTime.Now >= it.AbsoluteExpiration) {
try {
cacheLock.EnterWriteLock ();
if (!NeedsUpdate (it, CacheItemUpdateReason.Expired, false))
Remove (key, CacheItemRemovedReason.Expired, false, true);
} finally {
// See comment at the top of the file, above cacheLock declaration
cacheLock.ExitWriteLock ();
}
return null;
}
}
return it.Value;
} finally {
// See comment at the top of the file, above cacheLock declaration
cacheLock.ExitUpgradeableReadLock ();
}
}
public void Insert (string key, object value)
{
Insert (key, value, null, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null, null, true);
}
public void Insert (string key, object value, CacheDependency dependencies)
{
Insert (key, value, dependencies, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null, null, true);
}
public void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, CacheItemPriority.Normal, null, null, true);
}
public void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration,
CacheItemUpdateCallback onUpdateCallback)
{
Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, CacheItemPriority.Normal, null, onUpdateCallback, true);
}
public void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration,
CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
{
Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback, null, true);
}
void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration,
CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback, CacheItemUpdateCallback onUpdateCallback, bool doLock)
{
if (key == null)
throw new ArgumentNullException ("key");
if (value == null)
throw new ArgumentNullException ("value");
if (slidingExpiration < TimeSpan.Zero || slidingExpiration > TimeSpan.FromDays (365))
throw new ArgumentNullException ("slidingExpiration");
if (absoluteExpiration != NoAbsoluteExpiration && slidingExpiration != NoSlidingExpiration)
throw new ArgumentException ("Both absoluteExpiration and slidingExpiration are specified");
CacheItem ci = new CacheItem ();
ci.Value = value;
ci.Key = key;
if (dependencies != null) {
ci.Dependency = dependencies;
dependencies.DependencyChanged += new EventHandler (OnDependencyChanged);
dependencies.SetCache (DependencyCache);
}
ci.Priority = priority;
SetItemTimeout (ci, absoluteExpiration, slidingExpiration, onRemoveCallback, onUpdateCallback, key, doLock);
}
internal void SetItemTimeout (string key, DateTime absoluteExpiration, TimeSpan slidingExpiration, bool doLock)
{
CacheItem ci = null;
try {
if (doLock)
cacheLock.EnterWriteLock ();
ci = cache [key];
if (ci != null)
SetItemTimeout (ci, absoluteExpiration, slidingExpiration, ci.OnRemoveCallback, null, key, false);
} finally {
if (doLock) {
// See comment at the top of the file, above cacheLock declaration
cacheLock.ExitWriteLock ();
}
}
}
void SetItemTimeout (CacheItem ci, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemRemovedCallback onRemoveCallback,
CacheItemUpdateCallback onUpdateCallback, string key, bool doLock)
{
bool disableExpiration = DisableExpiration;
if (!disableExpiration) {
ci.SlidingExpiration = slidingExpiration;
if (slidingExpiration != NoSlidingExpiration)
ci.AbsoluteExpiration = DateTime.Now + slidingExpiration;
else
ci.AbsoluteExpiration = absoluteExpiration;
}
ci.OnRemoveCallback = onRemoveCallback;
ci.OnUpdateCallback = onUpdateCallback;
try {
if (doLock)
cacheLock.EnterWriteLock ();
if (key != null) {
cache [key] = ci;
cache.EvictIfNecessary ();
}
ci.LastChange = DateTime.Now;
if (!disableExpiration && ci.AbsoluteExpiration != NoAbsoluteExpiration) {
bool enqueue;
if (ci.IsTimedItem) {
enqueue = UpdateTimedItem (ci);
if (!enqueue)
UpdateTimerPeriod (ci);
} else
enqueue = true;
if (enqueue) {
ci.IsTimedItem = true;
EnqueueTimedItem (ci);
}
}
} finally {
if (doLock) {
// See comment at the top of the file, above cacheLock declaration
cacheLock.ExitWriteLock ();
}
}
}
// MUST be called with cache lock held
bool UpdateTimedItem (CacheItem item)
{
if (timedItems == null)
return true;
item.ExpiresAt = item.AbsoluteExpiration.Ticks;
return !timedItems.Update (item);
}
// MUST be called with cache lock held
void UpdateTimerPeriod (CacheItem item)
{
if (timedItems == null)
timedItems = new CacheItemPriorityQueue ();
long remaining = Math.Max (0, (long)(item.AbsoluteExpiration - DateTime.Now).TotalMilliseconds);
item.ExpiresAt = item.AbsoluteExpiration.Ticks;
if (remaining > 4294967294)
// Maximum due time for timer
// Item will expire properly anyway, as the timer will be
// rescheduled for the item's expiration time once that item is
// bubbled to the top of the priority queue.
remaining = 4294967294;
if (expirationTimer != null && expirationTimerPeriod <= remaining)
return;
expirationTimerPeriod = remaining;
if (expirationTimer == null)
expirationTimer = new Timer (new TimerCallback (ExpireItems), null, expirationTimerPeriod, expirationTimerPeriod);
else
expirationTimer.Change (expirationTimerPeriod, expirationTimerPeriod);
}
// MUST be called with cache lock held
void EnqueueTimedItem (CacheItem item)
{
UpdateTimerPeriod (item);
timedItems.Enqueue (item);
}
public object Remove (string key)
{
return Remove (key, CacheItemRemovedReason.Removed, true, true);
}
internal object Remove (string key, CacheItemRemovedReason reason, bool doLock, bool invokeCallback)
{
CacheItem it = null;
try {
if (doLock)
cacheLock.EnterWriteLock ();
it = RemoveCacheItem (key);
} finally {
if (doLock) {
// See comment at the top of the file, above cacheLock declaration
cacheLock.ExitWriteLock ();
}
}
object ret = null;
if (it != null) {
if (it.Dependency != null) {
it.Dependency.SetCache (null);
it.Dependency.DependencyChanged -= new EventHandler (OnDependencyChanged);
it.Dependency.Dispose ();
}
if (invokeCallback && it.OnRemoveCallback != null) {
try {
it.OnRemoveCallback (key, it.Value, reason);
} catch {
//TODO: anything to be done here?
}
}
ret = it.Value;
it.Value = null;
it.Key = null;
it.Dependency = null;
it.OnRemoveCallback = null;
it.OnUpdateCallback = null;
it = null;
}
return ret;
}
// Used when shutting down the application so that
// session_end events are sent for all sessions.
internal void InvokePrivateCallbacks ()
{
try {
cacheLock.EnterReadLock ();
cache.InvokePrivateCallbacks ();
} finally {
// See comment at the top of the file, above cacheLock declaration
cacheLock.ExitReadLock ();
}
}
public IDictionaryEnumerator GetEnumerator ()
{
List <CacheItem> list = null;
try {
cacheLock.EnterReadLock ();
list = cache.ToList ();
} finally {
// See comment at the top of the file, above cacheLock declaration
cacheLock.ExitReadLock ();
}
return new CacheItemEnumerator (list);
}
IEnumerator IEnumerable.GetEnumerator ()
{
return GetEnumerator ();
}
void OnDependencyChanged (object o, EventArgs a)
{
CheckDependencies ();
}
bool NeedsUpdate (CacheItem item, CacheItemUpdateReason reason, bool needLock)
{
try {
if (needLock)
cacheLock.EnterWriteLock ();
if (item == null || item.OnUpdateCallback == null)
return false;
object expensiveObject;
CacheDependency dependency;
DateTime absoluteExpiration;
TimeSpan slidingExpiration;
string key = item.Key;
CacheItemUpdateCallback updateCB = item.OnUpdateCallback;
updateCB (key, reason, out expensiveObject, out dependency, out absoluteExpiration, out slidingExpiration);
if (expensiveObject == null)
return false;
CacheItemPriority priority = item.Priority;
CacheItemRemovedCallback removeCB = item.OnRemoveCallback;
CacheItemRemovedReason whyRemoved;
switch (reason) {
case CacheItemUpdateReason.Expired:
whyRemoved = CacheItemRemovedReason.Expired;
break;
case CacheItemUpdateReason.DependencyChanged:
whyRemoved = CacheItemRemovedReason.DependencyChanged;
break;
default:
whyRemoved = CacheItemRemovedReason.Removed;
break;
}
Remove (key, whyRemoved, false, false);
Insert (key, expensiveObject, dependency, absoluteExpiration, slidingExpiration, priority, removeCB, updateCB, false);
return true;
} catch (Exception) {
return false;
} finally {
if (needLock) {
// See comment at the top of the file, above cacheLock declaration
cacheLock.ExitWriteLock ();
}
}
}
void ExpireItems (object data)
{
DateTime now = DateTime.Now;
CacheItem item = null;
expirationTimer.Change (Timeout.Infinite, Timeout.Infinite);
try {
cacheLock.EnterWriteLock ();
while (true) {
item = timedItems.Peek ();
if (item == null) {
if (timedItems.Count == 0)
break;
timedItems.Dequeue ();
continue;
}
if (!item.Disabled && item.ExpiresAt > now.Ticks)
break;
if (item.Disabled) {
item = timedItems.Dequeue ();
continue;
}
item = timedItems.Dequeue ();
if (item != null)
if (!NeedsUpdate (item, CacheItemUpdateReason.Expired, false))
Remove (item.Key, CacheItemRemovedReason.Expired, false, true);
}
} finally {
// See comment at the top of the file, above cacheLock declaration
cacheLock.ExitWriteLock ();
}
if (item != null) {
long remaining = Math.Max (0, (long)(item.AbsoluteExpiration - now).TotalMilliseconds);
if (remaining > 0 && (expirationTimerPeriod == 0 || expirationTimerPeriod > remaining)) {
expirationTimerPeriod = remaining;
expirationTimer.Change (expirationTimerPeriod, expirationTimerPeriod);
return;
}
if (expirationTimerPeriod > 0)
return;
}
expirationTimer.Change (Timeout.Infinite, Timeout.Infinite);
expirationTimerPeriod = 0;
}
internal void CheckDependencies ()
{
try {
cacheLock.EnterWriteLock ();
List <CacheItem> list = cache.SelectItems (it => {
if (it == null)
return false;
if (it.Dependency != null && it.Dependency.HasChanged && !NeedsUpdate (it, CacheItemUpdateReason.DependencyChanged, false))
return true;
return false;
});
foreach (CacheItem it in list)
Remove (it.Key, CacheItemRemovedReason.DependencyChanged, false, true);
list.Clear ();
list.TrimExcess ();
} finally {
// See comment at the top of the file, above cacheLock declaration
cacheLock.ExitWriteLock ();
}
}
internal DateTime GetKeyLastChange (string key)
{
try {
cacheLock.EnterReadLock ();
CacheItem it = cache [key];
if (it == null)
return DateTime.MaxValue;
return it.LastChange;
} finally {
// See comment at the top of the file, above cacheLock declaration
cacheLock.ExitReadLock ();
}
}
internal Cache DependencyCache {
get {
if (dependencyCache == null)
return this;
return dependencyCache;
}
set { dependencyCache = value; }
}
}
}
|