DBCacheComponent.cs 4.5 KB

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