InMemoryStorageProvider.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Collections.Concurrent;
  2. using Quadarax.Foundation.Core.Business.Processor.Enums;
  3. using Quadarax.Foundation.Core.Business.Processor.Task;
  4. namespace Quadarax.Foundation.Core.Business.Processor.Providers
  5. {
  6. public class InMemoryStorageProvider : ProcessorStorageProvider
  7. {
  8. #region *** Private fields ***
  9. private readonly IDictionary<long, ITaskItemData> _cache = new ConcurrentDictionary<long, ITaskItemData>();
  10. #endregion
  11. #region *** Overrides ***
  12. protected override void OnDisposing()
  13. {
  14. _cache.Clear();
  15. }
  16. public override IQueryable<ITaskItemData> Query(params ProcessStatusEnum[] statuses)
  17. {
  18. throw new NotImplementedException();
  19. }
  20. protected override void OnOpen()
  21. {
  22. }
  23. protected override void OnClose()
  24. {
  25. }
  26. protected override bool OnEnsureStorage()
  27. {
  28. return false;
  29. }
  30. protected override ITaskIdentifier OnCreate(ITaskItemData item)
  31. {
  32. var id = GetNextId();
  33. ((TaskItem)item).Id = id;
  34. _cache.TryAdd(id, item);
  35. return item;
  36. }
  37. protected override ITaskItemData[] OnGet(ITaskIdentifier[] ids)
  38. {
  39. var result = ids.Select(id => _cache[id.Id]).ToList();
  40. return result.ToArray();
  41. }
  42. protected override void OnDeleteOne(ITaskItemData item)
  43. {
  44. _cache.Remove(item.Id);
  45. }
  46. protected override void OnSetOne(ITaskItemData item)
  47. {
  48. var existing = (TaskItem) _cache[item.Id];
  49. existing.CopyFrom(item);
  50. }
  51. #endregion
  52. #region *** Private operations ***
  53. private long GetNextId()
  54. {
  55. return _cache.Keys.Any() ? _cache.Keys.Max() + 1 : 1;
  56. }
  57. #endregion
  58. }
  59. }