DBProxyComponent.cs 3.0 KB

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