DBCacheComponent.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. namespace Model
  4. {
  5. [ObjectSystem]
  6. public class DbCacheComponentSystem : ObjectSystem<DBCacheComponent>, IAwake
  7. {
  8. public void Awake()
  9. {
  10. this.Get().Awake();
  11. }
  12. }
  13. /// <summary>
  14. /// 用来缓存数据
  15. /// </summary>
  16. public class DBCacheComponent : Component
  17. {
  18. public Dictionary<string, Dictionary<long, Disposer>> cache = new Dictionary<string, Dictionary<long, Disposer>>();
  19. public const int taskCount = 32;
  20. public List<DBTaskQueue> tasks = new List<DBTaskQueue>(taskCount);
  21. public void Awake()
  22. {
  23. for (int i = 0; i < taskCount; ++i)
  24. {
  25. DBTaskQueue taskQueue = EntityFactory.Create<DBTaskQueue>();
  26. this.tasks.Add(taskQueue);
  27. }
  28. }
  29. public Task<bool> Add(Disposer disposer, string collectionName = "")
  30. {
  31. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  32. this.AddToCache(disposer, collectionName);
  33. if (string.IsNullOrEmpty(collectionName))
  34. {
  35. collectionName = disposer.GetType().Name;
  36. }
  37. DBSaveTask task = EntityFactory.CreateWithId<DBSaveTask, Disposer, string, TaskCompletionSource<bool>>(disposer.Id, disposer, collectionName, tcs);
  38. this.tasks[(int)((ulong)task.Id % taskCount)].Add(task);
  39. return tcs.Task;
  40. }
  41. public Task<bool> AddBatch(List<Disposer> disposers, string collectionName)
  42. {
  43. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  44. DBSaveBatchTask task = EntityFactory.Create<DBSaveBatchTask, List<Disposer>, string, TaskCompletionSource<bool>>(disposers, collectionName, tcs);
  45. this.tasks[(int)((ulong)task.Id % taskCount)].Add(task);
  46. return tcs.Task;
  47. }
  48. public void AddToCache(Disposer disposer, string collectionName = "")
  49. {
  50. if (string.IsNullOrEmpty(collectionName))
  51. {
  52. collectionName = disposer.GetType().Name;
  53. }
  54. Dictionary<long, Disposer> collection;
  55. if (!this.cache.TryGetValue(collectionName, out collection))
  56. {
  57. collection = new Dictionary<long, Disposer>();
  58. this.cache.Add(collectionName, collection);
  59. }
  60. collection[disposer.Id] = disposer;
  61. }
  62. public Disposer GetFromCache(string collectionName, long id)
  63. {
  64. Dictionary<long, Disposer> d;
  65. if (!this.cache.TryGetValue(collectionName, out d))
  66. {
  67. return null;
  68. }
  69. Disposer result;
  70. if (!d.TryGetValue(id, out result))
  71. {
  72. return null;
  73. }
  74. return result;
  75. }
  76. public void RemoveFromCache(string collectionName, long id)
  77. {
  78. Dictionary<long, Disposer> d;
  79. if (!this.cache.TryGetValue(collectionName, out d))
  80. {
  81. return;
  82. }
  83. d.Remove(id);
  84. }
  85. public Task<Disposer> Get(string collectionName, long id)
  86. {
  87. Disposer disposer = GetFromCache(collectionName, id);
  88. if (disposer != null)
  89. {
  90. return Task.FromResult(disposer);
  91. }
  92. TaskCompletionSource<Disposer> tcs = new TaskCompletionSource<Disposer>();
  93. DBQueryTask dbQueryTask = EntityFactory.CreateWithId<DBQueryTask, string, TaskCompletionSource<Disposer>>(id, collectionName, tcs);
  94. this.tasks[(int)((ulong)id % taskCount)].Add(dbQueryTask);
  95. return tcs.Task;
  96. }
  97. public Task<List<Disposer>> GetBatch(string collectionName, List<long> idList)
  98. {
  99. List <Disposer> disposers = new List<Disposer>();
  100. bool isAllInCache = true;
  101. foreach (long id in idList)
  102. {
  103. Disposer disposer = this.GetFromCache(collectionName, id);
  104. if (disposer == null)
  105. {
  106. isAllInCache = false;
  107. break;
  108. }
  109. disposers.Add(disposer);
  110. }
  111. if (isAllInCache)
  112. {
  113. return Task.FromResult(disposers);
  114. }
  115. TaskCompletionSource<List<Disposer>> tcs = new TaskCompletionSource<List<Disposer>>();
  116. DBQueryBatchTask dbQueryBatchTask = EntityFactory.Create<DBQueryBatchTask, List<long>, string, TaskCompletionSource<List<Disposer>>>(idList, collectionName, tcs);
  117. this.tasks[(int)((ulong)dbQueryBatchTask.Id % taskCount)].Add(dbQueryBatchTask);
  118. return tcs.Task;
  119. }
  120. public Task<List<Disposer>> GetJson(string collectionName, string json)
  121. {
  122. TaskCompletionSource<List<Disposer>> tcs = new TaskCompletionSource<List<Disposer>>();
  123. DBQueryJsonTask dbQueryJsonTask = EntityFactory.Create<DBQueryJsonTask, string, string, TaskCompletionSource<List<Disposer>>>(collectionName, json, tcs);
  124. this.tasks[(int)((ulong)dbQueryJsonTask.Id % taskCount)].Add(dbQueryJsonTask);
  125. return tcs.Task;
  126. }
  127. }
  128. }