NegotiationTokenAuthenticatorStateCache.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System;
  7. using System.Collections;
  8. using System.Runtime;
  9. using System.ServiceModel;
  10. using System.ServiceModel.Diagnostics.Application;
  11. sealed class NegotiationTokenAuthenticatorStateCache<T> : TimeBoundedCache
  12. where T : NegotiationTokenAuthenticatorState
  13. {
  14. static int lowWaterMark = 50;
  15. static TimeSpan purgingInterval = TimeSpan.FromMinutes(10);
  16. TimeSpan cachingSpan;
  17. public NegotiationTokenAuthenticatorStateCache(TimeSpan cachingSpan, int maximumCachedState)
  18. : base(lowWaterMark, maximumCachedState, null, PurgingMode.TimerBasedPurge, TimeSpan.FromTicks(cachingSpan.Ticks >> 2), true)
  19. {
  20. this.cachingSpan = cachingSpan;
  21. }
  22. public void AddState(string context, T state)
  23. {
  24. DateTime expirationTime = TimeoutHelper.Add(DateTime.UtcNow, this.cachingSpan);
  25. bool wasStateAdded = base.TryAddItem(context, state, expirationTime, false);
  26. if (!wasStateAdded)
  27. {
  28. throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new SecurityNegotiationException(SR.GetString(SR.NegotiationStateAlreadyPresent, context)));
  29. }
  30. if (TD.NegotiateTokenAuthenticatorStateCacheRatioIsEnabled())
  31. {
  32. TD.NegotiateTokenAuthenticatorStateCacheRatio(base.Count, base.Capacity);
  33. }
  34. }
  35. public T GetState(string context)
  36. {
  37. return (this.GetItem(context) as T);
  38. }
  39. public void RemoveState(string context)
  40. {
  41. this.TryRemoveItem(context);
  42. if (TD.NegotiateTokenAuthenticatorStateCacheRatioIsEnabled())
  43. {
  44. TD.NegotiateTokenAuthenticatorStateCacheRatio(base.Count, base.Capacity);
  45. }
  46. }
  47. protected override ArrayList OnQuotaReached(Hashtable cacheTable)
  48. {
  49. if (TD.NegotiateTokenAuthenticatorStateCacheExceededIsEnabled())
  50. {
  51. TD.NegotiateTokenAuthenticatorStateCacheExceeded(SR.GetString(SR.CachedNegotiationStateQuotaReached, this.Capacity));
  52. }
  53. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new QuotaExceededException(SR.GetString(SR.CachedNegotiationStateQuotaReached, this.Capacity)));
  54. }
  55. protected override void OnRemove(object item)
  56. {
  57. ((IDisposable)item).Dispose();
  58. base.OnRemove(item);
  59. }
  60. }
  61. }