CacheList.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using WingServerCommon.Log;
  5. namespace WingServerCommon.Interfaces.Cache
  6. {
  7. public class CacheList<T> where T : ICacheObject
  8. {
  9. private Dictionary<string, T> _collection = new Dictionary<string, T>();
  10. private object _writeLock = new Object();
  11. /// <summary>
  12. /// The collection count
  13. /// </summary>
  14. public int Count => _collection.Count;
  15. internal CacheList()
  16. {
  17. }
  18. /// <summary>
  19. /// Load cache from db objects
  20. /// </summary>
  21. /// <param name="dbOjects">the db object list</param>
  22. internal void LoadFromDbOject(IList<T> dbOjects)
  23. {
  24. _collection.Clear();
  25. foreach (var dbOject in dbOjects)
  26. {
  27. Add(dbOject);
  28. }
  29. }
  30. /// <summary>
  31. /// Remove a cache record with specified document id
  32. /// </summary>
  33. /// <param name="documentId">The db collection document id</param>
  34. internal bool Remove(string documentId)
  35. {
  36. lock (_writeLock)
  37. {
  38. if (_collection.ContainsKey(documentId))
  39. {
  40. _collection.Remove(documentId, out var removeCache);
  41. return true;
  42. }
  43. return false;
  44. }
  45. }
  46. /// <summary>
  47. /// Remove many records from cache list; this function happened on sychronization actions
  48. /// </summary>
  49. /// <param name="ids"></param>
  50. internal virtual void RemoveMany(string[] ids)
  51. {
  52. lock (_writeLock)
  53. {
  54. foreach (var documentId in ids)
  55. {
  56. if (_collection.ContainsKey(documentId))
  57. {
  58. _collection.Remove(documentId, out var removeCache);
  59. }
  60. }
  61. }
  62. }
  63. /// <summary>
  64. /// Add a cache object
  65. /// </summary>
  66. /// <param name="cacheObj"></param>
  67. internal void Add(T cacheObj)
  68. {
  69. if (cacheObj != null && !string.IsNullOrWhiteSpace(cacheObj.Code))
  70. {
  71. lock (_writeLock)
  72. {
  73. var documentId = cacheObj.Code;
  74. var result = _collection.TryAdd(documentId, cacheObj);
  75. if (!result)
  76. {
  77. Logger.WriteLineError($"Add failed with id:{documentId}");
  78. //throw new InvalidOperationException($"Add failed with id:{documentId}");
  79. }
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// Get cache with specified document id
  85. /// </summary>
  86. /// <param name="documentId">the document id, the code in db</param>
  87. /// <typeparam name="T"></typeparam>
  88. /// <returns></returns>
  89. internal T Get(string documentId)
  90. {
  91. if (_collection.ContainsKey(documentId))
  92. {
  93. return _collection[documentId];
  94. }
  95. return default(T);
  96. }
  97. /// <summary>
  98. /// Find values whith filters
  99. /// </summary>
  100. /// <param name="predicate"></param>
  101. /// <returns></returns>
  102. internal IList<T> Where(Func<T, bool> predicate)
  103. {
  104. return _collection.Values.Where(predicate)?.ToList() ?? new List<T>();
  105. }
  106. }
  107. }