DBProxyComponentSystem.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Collections.Generic;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using ETModel;
  5. namespace ETHotfix
  6. {
  7. [ObjectSystem]
  8. public class DbProxyComponentSystem : AwakeSystem<DBProxyComponent>
  9. {
  10. public override void Awake(DBProxyComponent self)
  11. {
  12. self.Awake();
  13. }
  14. }
  15. /// <summary>
  16. /// 用来与数据库操作代理
  17. /// </summary>
  18. public static class DBProxyComponentEx
  19. {
  20. public static void Awake(this DBProxyComponent self)
  21. {
  22. StartConfig dbStartConfig = Game.Scene.GetComponent<StartConfigComponent>().DBConfig;
  23. self.dbAddress = dbStartConfig.GetComponent<InnerConfig>().IPEndPoint;
  24. }
  25. public static async Task Save(this DBProxyComponent self, ComponentWithId component, bool needCache = true)
  26. {
  27. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  28. await session.Call(new DBSaveRequest { Component = component, NeedCache = needCache});
  29. }
  30. public static async Task SaveBatch(this DBProxyComponent self, List<ComponentWithId> components, bool needCache = true)
  31. {
  32. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  33. await session.Call(new DBSaveBatchRequest { Components = components, NeedCache = needCache});
  34. }
  35. public static async Task Save(this DBProxyComponent self, ComponentWithId component, bool needCache, CancellationToken cancellationToken)
  36. {
  37. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  38. await session.Call(new DBSaveRequest { Component = component, NeedCache = needCache}, cancellationToken);
  39. }
  40. public static async void SaveLog(this DBProxyComponent self, ComponentWithId component)
  41. {
  42. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  43. await session.Call(new DBSaveRequest { Component = component, NeedCache = false, CollectionName = "Log" });
  44. }
  45. public static async Task<T> Query<T>(this DBProxyComponent self, long id, bool needCache = true) where T: ComponentWithId
  46. {
  47. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  48. DBQueryResponse dbQueryResponse = (DBQueryResponse)await session.Call(new DBQueryRequest { CollectionName = typeof(T).Name, Id = id, NeedCache = needCache });
  49. return (T)dbQueryResponse.Component;
  50. }
  51. public static async Task<List<T>> QueryBatch<T>(this DBProxyComponent self, List<long> ids, bool needCache = true) where T : ComponentWithId
  52. {
  53. List<T> list = new List<T>();
  54. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  55. DBQueryBatchResponse dbQueryBatchResponse = (DBQueryBatchResponse)await session.Call(new DBQueryBatchRequest { CollectionName = typeof(T).Name, IdList = ids, NeedCache = needCache});
  56. foreach (ComponentWithId component in dbQueryBatchResponse.Components)
  57. {
  58. list.Add((T)component);
  59. }
  60. return list;
  61. }
  62. public static async Task<List<T>> QueryJson<T>(this DBProxyComponent self, string json) where T : ComponentWithId
  63. {
  64. List<T> list = new List<T>();
  65. Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
  66. DBQueryJsonResponse dbQueryJsonResponse = (DBQueryJsonResponse)await session.Call(new DBQueryJsonRequest { CollectionName = typeof(T).Name, Json = json });
  67. foreach (ComponentWithId component in dbQueryJsonResponse.Components)
  68. {
  69. list.Add((T)component);
  70. }
  71. return list;
  72. }
  73. }
  74. }