Prechádzať zdrojové kódy

优化下db代码,减少gc,查询多个结果返回List<ComponentWithId>,这样少构造一个List

tanghai 7 rokov pred
rodič
commit
6d31b4d0d8

+ 20 - 23
Server/Hotfix/Module/DB/DBProxyComponentSystem.cs

@@ -62,44 +62,41 @@ namespace ETHotfix
 			return (T)dbQueryResponse.Component;
 		}
 		
-		public static async Task<List<T>> Query<T>(this DBProxyComponent self, Expression<Func<T ,bool>> exp, bool needCache = true) where T: ComponentWithId
+		/// <summary>
+		/// 根据查询表达式查询
+		/// </summary>
+		/// <param name="self"></param>
+		/// <param name="exp"></param>
+		/// <typeparam name="T"></typeparam>
+		/// <returns></returns>
+		public static async Task<List<ComponentWithId>> Query<T>(this DBProxyComponent self, Expression<Func<T ,bool>> exp) where T: ComponentWithId
 		{
-			List<T> list = new List<T>();
-			Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
 			ExpressionFilterDefinition<T> filter = new ExpressionFilterDefinition<T>(exp);
 			IBsonSerializerRegistry serializerRegistry = BsonSerializer.SerializerRegistry;
 			IBsonSerializer<T> documentSerializer = serializerRegistry.GetSerializer<T>();
 			string json = filter.Render(documentSerializer, serializerRegistry).ToJson();
-			DBQueryJsonResponse dbQueryResponse = (DBQueryJsonResponse)await session.Call(new DBQueryJsonRequest { CollectionName = typeof(T).Name, Json = json });
-			foreach (ComponentWithId component in dbQueryResponse.Components)
-			{
-				list.Add((T)component);
-			}
-			return list;
+			return await self.Query<T>(json);
 		}
 
-		public static async Task<List<T>> QueryBatch<T>(this DBProxyComponent self, List<long> ids, bool needCache = true) where T : ComponentWithId
+		public static async Task<List<ComponentWithId>> Query<T>(this DBProxyComponent self, List<long> ids, bool needCache = true) where T : ComponentWithId
 		{
-			List<T> list = new List<T>();
 			Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
 			DBQueryBatchResponse dbQueryBatchResponse = (DBQueryBatchResponse)await session.Call(new DBQueryBatchRequest { CollectionName = typeof(T).Name, IdList = ids, NeedCache = needCache});
-			foreach (ComponentWithId component in dbQueryBatchResponse.Components)
-			{
-				list.Add((T)component);
-			}
-			return list;
+			return dbQueryBatchResponse.Components;
 		}
 
-		public static async Task<List<T>> QueryJson<T>(this DBProxyComponent self, string json) where T : ComponentWithId
+		/// <summary>
+		/// 根据json查询条件查询
+		/// </summary>
+		/// <param name="self"></param>
+		/// <param name="json"></param>
+		/// <typeparam name="T"></typeparam>
+		/// <returns></returns>
+		public static async Task<List<ComponentWithId>> Query<T>(this DBProxyComponent self, string json) where T : ComponentWithId
 		{
-			List<T> list = new List<T>();
 			Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.dbAddress);
 			DBQueryJsonResponse dbQueryJsonResponse = (DBQueryJsonResponse)await session.Call(new DBQueryJsonRequest { CollectionName = typeof(T).Name, Json = json });
-			foreach (ComponentWithId component in dbQueryJsonResponse.Components)
-			{
-				list.Add((T)component);
-			}
-			return list;
+			return dbQueryJsonResponse.Components;
 		}
 	}
 }