MemoryStorageProvider.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Quadarax.Foundation.Core.Logging;
  8. namespace qdr.fnd.core.pqueue.Storages
  9. {
  10. public class MemoryStorageProvider : BaseStorageProvider
  11. {
  12. private ConcurrentDictionary<string, IPsQueueItem> _items = new ConcurrentDictionary<string, IPsQueueItem>();
  13. /// <inheritdoc />
  14. public MemoryStorageProvider(ILogger? logger) : base(logger)
  15. {
  16. }
  17. #region Overrides of BaseStorageProvider
  18. #endregion
  19. #region Overrides of DisposableObject
  20. /// <inheritdoc />
  21. protected override void OnDisposing()
  22. {
  23. _items.Clear();
  24. _items = null!;
  25. }
  26. #endregion
  27. #region Overrides of BaseStorageProvider
  28. /// <inheritdoc />
  29. protected override string OnCreateItem(string id, string owner, string ownerRef, string providerClass, int initialAttempts,
  30. DateTime created, string s)
  31. {
  32. var item = new PsQueueItem(id, owner, ownerRef, providerClass, created)
  33. {
  34. AttemptsLeft = initialAttempts
  35. };
  36. if(!_items.TryAdd(id, item))
  37. Log?.Log(LogSeverityEnum.Warn, $"Item id={id} already exists in storage (id conflict)");
  38. return id;
  39. }
  40. /// <inheritdoc />
  41. protected override void OnDeleteItem(string itemId)
  42. {
  43. if(_items.TryRemove(itemId, out _))
  44. Log?.Log(LogSeverityEnum.Warn, $"Item id={itemId} wasn't delete from storage (not exists)");
  45. }
  46. /// <inheritdoc />
  47. public override bool ContainsItem(string itemId)
  48. {
  49. return _items.ContainsKey(itemId);
  50. }
  51. #endregion
  52. }
  53. }