DBCacheComponent.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. namespace Model
  4. {
  5. [ObjectEvent]
  6. public class DBCacheComponentEvent : ObjectEvent<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, Entity>> cache = new Dictionary<string, Dictionary<long, Entity>>();
  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 = new DBTaskQueue();
  26. this.tasks.Add(taskQueue);
  27. taskQueue.Start();
  28. }
  29. }
  30. public Task<bool> Add(Entity entity, string collectionName = "")
  31. {
  32. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  33. this.AddToCache(entity, collectionName);
  34. if (collectionName == "")
  35. {
  36. collectionName = entity.GetType().Name;
  37. }
  38. DBSaveTask task = new DBSaveTask(entity, collectionName, tcs);
  39. this.tasks[(int)((ulong)task.Id % taskCount)].Add(task);
  40. return tcs.Task;
  41. }
  42. public Task<bool> AddBatch(List<Entity> entitys, string collectionName)
  43. {
  44. TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  45. DBSaveBatchTask task = new DBSaveBatchTask(entitys, collectionName, tcs);
  46. this.tasks[(int)((ulong)task.Id % taskCount)].Add(task);
  47. return tcs.Task;
  48. }
  49. public void AddToCache(Entity entity, string collectionName = "")
  50. {
  51. if (collectionName == "")
  52. {
  53. collectionName = entity.GetType().Name;
  54. }
  55. Dictionary<long, Entity> collection;
  56. if (!this.cache.TryGetValue(collectionName, out collection))
  57. {
  58. collection = new Dictionary<long, Entity>();
  59. this.cache.Add(collectionName, collection);
  60. }
  61. collection[entity.Id] = entity;
  62. }
  63. public Entity GetFromCache(string collectionName, long id)
  64. {
  65. Dictionary<long, Entity> d;
  66. if (!this.cache.TryGetValue(collectionName, out d))
  67. {
  68. return null;
  69. }
  70. Entity 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, Entity> d;
  80. if (!this.cache.TryGetValue(collectionName, out d))
  81. {
  82. return;
  83. }
  84. d.Remove(id);
  85. }
  86. public Task<Entity> Get(string collectionName, long id)
  87. {
  88. Entity entity = GetFromCache(collectionName, id);
  89. if (entity != null)
  90. {
  91. return Task.FromResult(entity);
  92. }
  93. TaskCompletionSource<Entity> tcs = new TaskCompletionSource<Entity>();
  94. this.tasks[(int)((ulong)id % taskCount)].Add(new DBQueryTask(id, collectionName, tcs));
  95. return tcs.Task;
  96. }
  97. public Task<List<Entity>> GetBatch(string collectionName, List<long> idList)
  98. {
  99. List <Entity> entitys = new List<Entity>();
  100. bool isAllInCache = true;
  101. foreach (long id in idList)
  102. {
  103. Entity entity = this.GetFromCache(collectionName, id);
  104. if (entity == null)
  105. {
  106. isAllInCache = false;
  107. break;
  108. }
  109. entitys.Add(entity);
  110. }
  111. if (isAllInCache)
  112. {
  113. return Task.FromResult(entitys);
  114. }
  115. TaskCompletionSource<List<Entity>> tcs = new TaskCompletionSource<List<Entity>>();
  116. DBQueryBatchTask dbQueryBatchTask = new DBQueryBatchTask(idList, collectionName, tcs);
  117. this.tasks[(int)((ulong)dbQueryBatchTask.Id % taskCount)].Add(dbQueryBatchTask);
  118. return tcs.Task;
  119. }
  120. public Task<List<Entity>> GetJson(string collectionName, string json)
  121. {
  122. TaskCompletionSource<List<Entity>> tcs = new TaskCompletionSource<List<Entity>>();
  123. DBQueryJsonTask dbQueryJsonTask = new DBQueryJsonTask(collectionName, json, tcs);
  124. this.tasks[(int)((ulong)dbQueryJsonTask.Id % taskCount)].Add(dbQueryJsonTask);
  125. return tcs.Task;
  126. }
  127. }
  128. }