DBCacheComponent.cs 4.7 KB

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