DBCacheComponent.cs 4.4 KB

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