VolatileStorage.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #region --------------- Copyright Dresser Wayne Pignone -------------
  2. /*
  3. * $Log: /Wrk/WayneLibraries/Wrk/Log/EventLogStorage/VolatileStorage.cs $
  4. *
  5. * 2 08-02-18 7:58 Mattias.larsson
  6. * Optimized use of Dictionaries.
  7. */
  8. #endregion
  9. using System.Collections.Generic;
  10. namespace Wayne.Lib.Log
  11. {
  12. /// <summary>
  13. /// The VolatileStorage event storage class keeps events only in memory which means that
  14. /// the unhandled events will vanish when the application is restarted.
  15. /// </summary>
  16. internal class VolatileStorage : ISubscriptionStorage
  17. {
  18. #region Fields
  19. private Dictionary<string, List<EventLogEntry>> db = new Dictionary<string, List<EventLogEntry>>();
  20. private object dbLock = new object();
  21. #endregion
  22. #region Methods
  23. public void Remove(string eventSubscriberId, EventLogEntry logEntry)
  24. {
  25. lock (dbLock)
  26. {
  27. List<EventLogEntry> pendingEventLogs;
  28. if (db.TryGetValue(eventSubscriberId, out pendingEventLogs))
  29. pendingEventLogs.Remove(logEntry);
  30. }
  31. }
  32. public EventLogEntry[] GetStoredEvents(string eventSubscriberId)
  33. {
  34. lock (dbLock)
  35. {
  36. List<EventLogEntry> pendingEventLogs;
  37. if (db.TryGetValue(eventSubscriberId, out pendingEventLogs))
  38. return pendingEventLogs.ToArray();
  39. else
  40. return new EventLogEntry[0];
  41. }
  42. }
  43. public void Add(string eventSubscriberId, EventLogEntry logEntry)
  44. {
  45. lock (dbLock)
  46. {
  47. List<EventLogEntry> pendingEventLogs;
  48. if (!db.TryGetValue(eventSubscriberId, out pendingEventLogs))
  49. {
  50. pendingEventLogs = new List<EventLogEntry>();
  51. db.Add(eventSubscriberId, pendingEventLogs);
  52. }
  53. pendingEventLogs.Add(logEntry);
  54. }
  55. }
  56. #endregion
  57. }
  58. }