1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252 |
- using Microsoft.Azure.Cosmos;
- using Microsoft.Azure.Cosmos.Linq;
- using OpenXmlPowerTools;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Net;
- using System.Reflection;
- using System.Text;
- using System.Text.Json;
- using System.Threading;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.Context.Attributes.Azure;
- using TEAMModelOS.SDK.Context.Exception;
- using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
- using TEAMModelOS.SDK.Helper.Common.JsonHelper;
- using TEAMModelOS.SDK.Helper.Common.LogHelper;
- using TEAMModelOS.SDK.Helper.Common.ReflectorExtensions;
- using TEAMModelOS.SDK.Module.AzureCosmosDB.Configuration;
- namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
- {
-
- public class AzureCosmosDBV3Repository : IAzureCosmosDBV3Repository
- {
- private CosmosClient CosmosClient { get; set; }
- public CosmosDict CosmosDict { get; set; } = new CosmosDict();
- /// <summary>
- /// 数据库名
- /// </summary>
- private string DatabaseId { get; set; }
- /// <summary>
- /// RU
- /// </summary>
- private int CollectionThroughput { get; set; }
- /// <summary>
- /// 数据库对象
- /// </summary>
- private Database database { get; set; }
- /// <summary>
- /// 分页大小
- /// </summary>
- int pageSize = 200;
- /// <summary>
- /// 缓存前缀
- /// </summary>
- private const string CacheCosmosPrefix = "cosmos:";
- /// <summary>
- /// 扫描类
- /// </summary>
- private string[] ScanModel { get; set; }
- /// <summary>
- /// 超时时间
- /// </summary>
- private const int timeoutSeconds = 86400;
- /// <summary>
- /// ttl时长 1秒
- /// </summary>
- private const int ttl = 1;
- /// <summary>
- /// 更新源通知容器
- /// </summary>
- private string leaseId = "AleaseContainer";
- public AzureCosmosDBV3Repository(AzureCosmosDBOptions options, CosmosSerializer cosmosSerializer)
- {
- try
- {
- if (!string.IsNullOrEmpty(options.ConnectionString))
- {
- CosmosClient = CosmosDBV3ClientSingleton.getInstance(options.ConnectionString, options.ConnectionKey, cosmosSerializer).GetCosmosDBClient();
- }
- else
- {
- throw new BizException("请设置正确的AzureCosmosDB数据库配置信息!");
- }
- DatabaseId = options.Database;
- CollectionThroughput = options.CollectionThroughput;
- ScanModel = options.ScanModel;
- }
- catch (CosmosException e)
- {
- throw new BizException(e.Message, 500, e.StackTrace);
- }
- }
- public AzureCosmosDBV3Repository(AzureCosmosDBOptions options)
- {
- try
- {
- if (!string.IsNullOrEmpty(options.ConnectionString))
- {
- CosmosClient = CosmosDBV3ClientSingleton.getInstance(options.ConnectionString, options.ConnectionKey, null).GetCosmosDBClient();
- }
- else
- {
- throw new BizException("请设置正确的AzureCosmosDB数据库配置信息!");
- }
- DatabaseId = options.Database;
- CollectionThroughput = options.CollectionThroughput;
- ScanModel = options.ScanModel;
- }
- catch (CosmosException e)
- {
- throw new BizException(e.Message, 500, e.StackTrace);
- }
- }
- /// <summary>
- /// 初始化CosmosDB数据库
- /// </summary>
- /// <returns></returns>
- public async Task<CosmosDict> InitializeDatabase() {
- database = await CosmosClient.CreateDatabaseIfNotExistsAsync(DatabaseId, CollectionThroughput);
- FeedIterator<ContainerProperties> resultSetIterator = database.GetContainerQueryIterator<ContainerProperties>();
- while (resultSetIterator.HasMoreResults)
- {
- foreach (ContainerProperties container in await resultSetIterator.ReadNextAsync())
- {
- CosmosDict.nameCosmos.TryAdd(container.Id, new CosmosModelInfo { container = database.GetContainer(container.Id), partitionKey= container.PartitionKeyPath.Replace("/",""), cache = false, monitor = false });
- }
- }
- bool isMonitor = false;
- //获取数据库所有的表
- List<Type> types = ReflectorExtensions.GetAllTypeAsAttribute<CosmosDBAttribute>(ScanModel);
- foreach (Type type in types)
- {
- string PartitionKey = GetPartitionKey(type);
- string CollectionName = "";
- int RU = 0;
- bool cache = false;
- bool monitor = false;
- IEnumerable<CosmosDBAttribute> attributes = type.GetCustomAttributes<CosmosDBAttribute>(true);
- if (attributes != null && !string.IsNullOrEmpty(attributes.First<CosmosDBAttribute>().Name))
- {
- CollectionName = attributes.First<CosmosDBAttribute>().Name;
- }
- else {
- throw new BizException("必须指定容器名",ResponseCode.PARAMS_ERROR);
- }
- if (attributes.First<CosmosDBAttribute>().Cache)
- {
- cache = attributes.First<CosmosDBAttribute>().Cache;
- }
- if (attributes.First<CosmosDBAttribute>().Monitor)
- {
- monitor = attributes.First<CosmosDBAttribute>().Monitor;
- if (monitor)
- {
- isMonitor = true;
- }
- }
- if (attributes.First<CosmosDBAttribute>().RU > 400)
- {
- RU = attributes.First<CosmosDBAttribute>().RU;
- }
- else
- {
- RU = CollectionThroughput;
- }
- //如果表存在于数据则检查RU是否变动,如果不存在则执行创建DocumentCollection
- if (CosmosDict.nameCosmos.TryGetValue(CollectionName, out CosmosModelInfo cosmosModelInfo))
- { //更新RU
- cosmosModelInfo.cache = cache;
- Container container = CosmosClient.GetDatabase(DatabaseId).GetContainer(cosmosModelInfo.container.Id);
- int? throughputResponse = await container.ReadThroughputAsync();
- if (throughputResponse < RU)
- {
- await CosmosClient.GetDatabase(DatabaseId).GetContainer(cosmosModelInfo.container.Id).ReplaceThroughputAsync(RU);
- }
- CosmosModelInfo cosmos = new CosmosModelInfo { container = container, cache = cache, monitor = monitor, type = type,partitionKey=PartitionKey };
- CosmosDict.nameCosmos[CollectionName] = cosmos;
- CosmosDict.typeCosmos.Add(type.Name, cosmos);
- }
- else {
- ContainerProperties containerProperties = new ContainerProperties { Id = CollectionName, DefaultTimeToLive = -1 };
- if (!string.IsNullOrEmpty(PartitionKey))
- {
- containerProperties.PartitionKeyPath = "/" + PartitionKey;
- }
- if (RU > CollectionThroughput)
- {
- CollectionThroughput = RU;
- }
- Container containerWithConsistentIndexing = await database.CreateContainerIfNotExistsAsync(containerProperties, throughput: CollectionThroughput);
- CosmosModelInfo cosmos = new CosmosModelInfo { container = containerWithConsistentIndexing, cache = cache, monitor = monitor, type = type, partitionKey = PartitionKey };
- CosmosDict.nameCosmos[CollectionName] = cosmos;
- CosmosDict.typeCosmos.Add(type.Name, cosmos);
- }
- }
- if (isMonitor)
- {
- ContainerProperties leaseProperties = new ContainerProperties { Id = leaseId, PartitionKeyPath = "/id", DefaultTimeToLive = -1 };
- Container leaseContainer = await database.CreateContainerIfNotExistsAsync(leaseProperties, throughput: CollectionThroughput);
- CosmosDict.nameCosmos.TryAdd(leaseId, new CosmosModelInfo { container = leaseContainer, cache = false, monitor = false, partitionKey = "/id" });
- }
- return CosmosDict;
- }
- private string GetPartitionKey<T>()
- {
- Type type = typeof(T);
- return GetPartitionKey(type);
- }
- private string GetPartitionKey(Type type)
- {
- PropertyInfo[] properties = type.GetProperties();
- List<PropertyInfo> attrProperties = new List<PropertyInfo>();
- foreach (PropertyInfo property in properties)
- {
- if (property.Name.Equals("PartitionKey"))
- {
- attrProperties.Add(property);
- break;
- }
- object[] attributes = property.GetCustomAttributes(true);
- foreach (object attribute in attributes) //2.通过映射,找到成员属性上关联的特性类实例,
- {
- if (attribute is PartitionKeyAttribute)
- {
- attrProperties.Add(property);
- }
- }
- }
- if (attrProperties.Count <= 0)
- {
- throw new BizException(type.Name + " has no PartitionKey !");
- }
- else
- {
- if (attrProperties.Count == 1)
- {
- return attrProperties[0].Name;
- }
- else { throw new BizException(type.Name+" PartitionKey can only be single!"); }
- }
- }
- private async Task<CosmosModelInfo> InitializeCollection<T>()
- {
- Type type = typeof(T);
- string partitionKey = GetPartitionKey<T>();
- CosmosDBAttribute cosmosDBAttribute = null;
- IEnumerable<CosmosDBAttribute> attributes = type.GetCustomAttributes<CosmosDBAttribute>(true);
- if (attributes != null && !string.IsNullOrEmpty(attributes.First<CosmosDBAttribute>().Name))
- {
- cosmosDBAttribute = attributes.First<CosmosDBAttribute>();
- }
- else {
- throw new BizException(type.Name+"未指定CosmosDB表名",ResponseCode.PARAMS_ERROR);
- }
-
- return await InitializeCollection(cosmosDBAttribute, type.Name, partitionKey);
- }
- private async Task<CosmosModelInfo> InitializeCollection(CosmosDBAttribute cosmosDBAttribute, string typeName, string PartitionKey)
- {
- /////内存中已经存在这个表则直接返回
- if (CosmosDict.typeCosmos.TryGetValue(typeName, out CosmosModelInfo cosmosModelInfo))
- {
- return cosmosModelInfo;
- }///如果没有则尝试默认创建
- else
- {
- ContainerProperties containerProperties = new ContainerProperties { Id = cosmosDBAttribute.Name };
- if (!string.IsNullOrEmpty(PartitionKey))
- {
- containerProperties.PartitionKeyPath = "/"+ PartitionKey;
- }
- Container containerWithConsistentIndexing = await database.CreateContainerIfNotExistsAsync(containerProperties, throughput: cosmosDBAttribute.RU);
- CosmosModelInfo cosmosModel = new CosmosModelInfo { container = containerWithConsistentIndexing, cache = cosmosDBAttribute.Cache,monitor= cosmosDBAttribute.Monitor };
- CosmosDict. nameCosmos.TryAdd(cosmosDBAttribute.Name, cosmosModel);
- CosmosDict.typeCosmos.TryAdd(typeName, cosmosModel);
- return cosmosModel;
- }
- }
- /// <summary>
- /// 按TTL删除
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="list"></param>
- /// <returns></returns>
- private async Task<List<T>> DeleteTTL<T>(List<T> list) where T : ID
- {
- CosmosModelInfo container = await InitializeCollection<T>();
- list.ForEach(x => { x.ttl = ttl; });
- list = await DeleteTTlALL(list);
- if (container.cache && RedisHelper.Instance != null)
- {
- list.ForEach(async x => {
- await RedisHelper.HDelAsync(CacheCosmosPrefix + container.container.Id, x.id);
- });
- }
- return list;
- }
- private async Task<List<T>> DeleteTTlALL<T>(List<T> enyites) where T : ID
- {
- //await Task.Run(() => Parallel.ForEach(entities, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (item) =>
- //{
- // Task.WaitAll(Update(item));
- //}));
- int pages = (int)Math.Ceiling((double)enyites.Count / pageSize);
- CosmosModelInfo container = await InitializeCollection<T>();
- bool flag = false;
- if (RedisHelper.Exists(CacheCosmosPrefix + container.container.Id))
- {
- flag = true;
- }
- string partitionKey = GetPartitionKey<T>();
- Type type = typeof(T);
- Stopwatch stopwatch = Stopwatch.StartNew();
- for (int i = 0; i < pages; i++)
- {
- List<T> lists = enyites.Skip((i) * pageSize).Take(pageSize).ToList();
- List<KeyValuePair<PartitionKey, Stream>> itemsToInsert = new List<KeyValuePair<PartitionKey, Stream>>();
- lists.ForEach(async x =>
- {
- x.pk = type.Name;
- //x.ttl = null;
- MemoryStream stream = new MemoryStream();
- await JsonSerializer.SerializeAsync(stream, x, new JsonSerializerOptions { IgnoreNullValues = true });
- object o = type.GetProperty(partitionKey).GetValue(x, null);
- KeyValuePair<PartitionKey, Stream> keyValue = new KeyValuePair<PartitionKey, Stream>(new PartitionKey(o.ToString()), stream);
- itemsToInsert.Add(keyValue);
- });
- List<Task> tasks = new List<Task>(lists.Count);
- itemsToInsert.ForEach(item =>
- {
- tasks.Add(container.container.UpsertItemStreamAsync(item.Value, item.Key)
- .ContinueWith((Task<ResponseMessage> task) =>
- {
- //using (ResponseMessage response = task.Result)
- //{
- // if (!response.IsSuccessStatusCode)
- // {
- // }
- //}
- }
- ));
- });
- await Task.WhenAll(tasks);
- if (container.cache && RedisHelper.Instance != null)
- {
- lists.ForEach(async x => {
- await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, x.id, x);
- });
- }
- }
- if (container.cache && RedisHelper.Instance != null && !flag)
- {
- await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
- }
- stopwatch.Stop();
- return enyites;
- }
- public async Task<List<IdPk>> DeleteAll<T>(List<IdPk> ids) where T : ID
- {
- // string pk = GetPartitionKey<T>();
- CosmosModelInfo container = await InitializeCollection<T>();
- List<IdPk> idPks = new List<IdPk>();
- if (container.monitor)
- {
- List<T> list = await FindByDict<T>(new Dictionary<string, object>() { { "id", ids.Select(x => x.id).ToArray() } });
- list = await DeleteTTL(list);
- return ids;
- }
- else
- {
- int pages = (int)Math.Ceiling((double)ids.Count / pageSize);
- Stopwatch stopwatch = Stopwatch.StartNew();
- for (int i = 0; i < pages; i++)
- {
- List<IdPk> lists = ids.Skip((i) * pageSize).Take(pageSize).ToList();
- List<Task> tasks = new List<Task>(lists.Count);
- lists.ForEach(item =>
- {
- tasks.Add(container.container.DeleteItemStreamAsync(item.id, new PartitionKey(item.pk))
- .ContinueWith((Task<ResponseMessage> task) =>
- {
- using (ResponseMessage response = task.Result)
- {
- idPks.Add(new IdPk { id = item.id, pk = item.pk, StatusCode = response.StatusCode });
- // if (!response.IsSuccessStatusCode)
- // {
- // }
- }
- }
- ));
- });
- await Task.WhenAll(tasks);
- if (container.cache && RedisHelper.Instance != null)
- {
- lists.ForEach(async x => {
- await RedisHelper.HDelAsync(CacheCosmosPrefix + container.container.Id, x.id);
- });
- }
- }
- stopwatch.Stop();
- return idPks;
- }
- }
- public async Task<List<IdPk>> DeleteAll<T>(Dictionary<string, object> dict) where T : ID
- {
- if (dict.Keys.Count > 0)
- {
- List<T> list = await FindByDict<T>(dict);
-
- return await DeleteAll(list);
- }
- else
- {
- throw new BizException("参数为空", 500);
- }
- }
- public async Task<List<IdPk>> DeleteAll<T>(List<T> enyites) where T : ID
- {
- Type type = typeof(T);
- string pk = GetPartitionKey<T>();
- CosmosModelInfo container = await InitializeCollection<T>();
- List<IdPk> idPks = new List<IdPk>();
- //log4net 日志記錄
- string uuidKey = Guid.NewGuid().ToString();
- string logkey = "\r\n【" + uuidKey + "】\r\n";
- LogHelper.Info(this,
- logkey
- + "删除------->>\r\n"
- + "表:"
- + type.Name+ "\r\n"
- + "数据:"
- + enyites.ToApiJson()
- + "\r\n"+ logkey);
- if (container.monitor)
- {
- enyites = await DeleteTTL(enyites);
- foreach (T t in enyites)
- {
- object o = type.GetProperty(pk).GetValue(t, null);
- idPks.Add(new IdPk { id = t.id, pk = o.ToString(), StatusCode = HttpStatusCode.NoContent });
- }
- return idPks;
- }
- else
- {
- int pages = (int)Math.Ceiling((double)enyites.Count / pageSize);
- Stopwatch stopwatch = Stopwatch.StartNew();
- for (int i = 0; i < pages; i++)
- {
- List<T> lists = enyites.Skip((i) * pageSize).Take(pageSize).ToList();
- List<KeyValuePair<PartitionKey, string>> itemsToInsert = new List<KeyValuePair<PartitionKey, string>>();
- lists.ForEach(x =>
- {
- object o = type.GetProperty(pk).GetValue(x, null);
- KeyValuePair<PartitionKey, string> keyValue = new KeyValuePair<PartitionKey, string>(new PartitionKey(o.ToString()), x.id);
- itemsToInsert.Add(keyValue);
- });
- List<Task> tasks = new List<Task>(lists.Count);
- itemsToInsert.ForEach(item =>
- {
- tasks.Add(container.container.DeleteItemStreamAsync(item.Value, item.Key)
- .ContinueWith((Task<ResponseMessage> task) =>
- {
- using (ResponseMessage response = task.Result)
- {
- idPks.Add(new IdPk { id = item.Value, pk = item.Key.ToString(), StatusCode = response.StatusCode });
- }
- }
- ));
- });
- await Task.WhenAll(tasks); if (container.cache && RedisHelper.Instance != null)
- {
- lists.ForEach(async x => {
- await RedisHelper.HDelAsync(CacheCosmosPrefix + container.container.Id, x.id);
- });
- }
- }
- stopwatch.Stop();
- return idPks;
- }
- }
- public async Task<IdPk> DeleteAsync<T>(IdPk idPk) where T : ID
- {
- return await DeleteAsync<T>(idPk.id, idPk.pk);
- }
- public async Task<IdPk> DeleteAsync<T>(string id, string pk) where T : ID
- {
- // pk = GetPartitionKey<T>();
- CosmosModelInfo container = await InitializeCollection<T>();
- if (container.monitor)
- {
- List<T> list = await FindByDict<T>(new Dictionary<string, object>() { { "id", id } });
- if (list.Count > 0)
- {
- await DeleteTTL<T>(list);
- return new IdPk { id = id, pk = pk, StatusCode = HttpStatusCode.NoContent };
- }
- else
- {
- throw new BizException("未找到ID匹配的数据,删除失败");
- }
- }
- else
- {
- ResponseMessage response = await container.container.DeleteItemStreamAsync(id: id, partitionKey: new PartitionKey(pk));
- if (container.cache && RedisHelper.Instance != null)
- {
- await RedisHelper.HDelAsync(CacheCosmosPrefix + container.container.Id, id);
- }
- return new IdPk { id = id, pk = pk, StatusCode = response.StatusCode };
- }
- }
- public async Task<IdPk> DeleteAsync<T>(T entity) where T : ID
- {
- CosmosModelInfo container = await InitializeCollection<T>();
- string partitionKey = GetPartitionKey<T>();
- Type type = typeof(T);
- object o = type.GetProperty(partitionKey).GetValue(entity, null);
- if (container.monitor)
- {
- await DeleteTTL<T>(new List<T>() { entity });
- return new IdPk { id = entity.id, pk = o.ToString(), StatusCode = HttpStatusCode.NoContent };
- }
- else
- {
- ResponseMessage response = await container.container.DeleteItemStreamAsync(id: entity.id, partitionKey: new PartitionKey(o.ToString()));
- if (container.cache && RedisHelper.Instance != null)
- {
- await RedisHelper.HDelAsync(CacheCosmosPrefix + container.container.Id, entity.id);
- }
- return new IdPk { id = entity.id, pk = partitionKey, StatusCode = response.StatusCode };
- }
- }
- public async Task<List<T>> FindAll<T>(List<string> propertys = null) where T : ID
- {
- CosmosModelInfo container = await InitializeCollection<T>();
- string pk = typeof(T).Name;
- StringBuilder sql;
- sql = SQLHelperParametric.GetSQLSelect(propertys);
- sql.Append(" where c.pk ='"+ pk + "'");
- CosmosDbQuery cosmosDbQuery = new CosmosDbQuery { QueryText = sql.ToString() };
- FeedIterator<T> query = container.container.GetItemQueryIterator<T>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition);
- return await ResultsFromFeedIterator(query);
- }
- private async Task<List<T>> ResultsFromFeedIterator<T>(FeedIterator<T> query, int? maxItemCount = null)
- {
- List<T> results = new List<T>();
- while (query.HasMoreResults)
- {
- foreach (T t in await query.ReadNextAsync())
- {
- results.Add(t);
- if (results.Count == maxItemCount)
- {
- return results;
- }
- }
- }
- return results;
- }
- private async Task<List<T>> ResultsFromFeedIterator<T>(FeedIterator<T> query, Func<List<T>, Task> batchAction, int itemsPerPage)
- {
- List<T> results = new List<T>();
- while (query.HasMoreResults)
- {
- if (results.Count() >= itemsPerPage)
- {
- await batchAction(results);
- results.Clear();
- }
- results.AddRange(await query.ReadNextAsync());
- }
- if (results.Count() > 0)
- {
- await batchAction(results);
- results.Clear();
- }
- return results;
- }
- public async Task<List<dynamic>> FindByDict(string CollectionName, Dictionary<string, object> dict, List<string> propertys = null)
- {
- if (CosmosDict.typeCosmos.TryGetValue(CollectionName, out CosmosModelInfo container))
- {
- //StringBuilder sql = new StringBuilder("select value(c) from c");
- //SQLHelper.GetSQL(dict, ref sql);
- //CosmosDbQuery cosmosDbQuery = new CosmosDbQuery, int itemsPerPage = -1, int?
- //{
- // QueryText = sql.ToString()
- //};
- string pk = container.type.Name;
- StringBuilder sql;
- sql = SQLHelperParametric.GetSQLSelect(propertys);
- CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql, pk);
- QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
- FeedIterator<dynamic> query = container.container.GetItemQueryIterator<dynamic>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: queryRequestOptions);
- return await ResultsFromFeedIterator(query);
- }
- else
- {
- throw new BizException("CollectionName named:" + CollectionName + " dose not exsit in Database!");
- }
- }
- public async Task<List<int>> FindCountByDict<T>(Dictionary<string, object> dict) {
- CosmosModelInfo container = await InitializeCollection<T>();
- string pk = typeof(T).Name;
- StringBuilder sql = new StringBuilder("select value count(c) from c");
- CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql, pk);
- if (cosmosDbQuery == null)
- {
- return new List<int> { 0 };
- }
- QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
- FeedIterator<int> query = container.container.GetItemQueryIterator<int>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: queryRequestOptions);
- return await ResultsFromFeedIterator(query);
- }
- public async Task<List<dynamic>> FindCountByDict(string CollectionName, Dictionary<string, object> dict)
- {
- if (CosmosDict.typeCosmos.TryGetValue(CollectionName, out CosmosModelInfo container))
- {
- string pk = container.type.Name;
- dict.Remove("@CURRPAGE");
- dict.Remove("@PAGESIZE");
- dict.Remove("@ASC");
- dict.Remove("@DESC");
- StringBuilder sql = new StringBuilder("select value count(c) from c");
- CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql, pk);
- if (cosmosDbQuery == null) {
- return new List<dynamic> {0 };
- }
- QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
- FeedIterator<dynamic> query = container.container.GetItemQueryIterator<dynamic>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: queryRequestOptions);
- return await ResultsFromFeedIterator(query);
- }
- else
- {
- throw new BizException("CollectionName named:" + CollectionName + " dose not exsit in Database!");
- }
- }
- public async Task<List<T>> FindByParams<T>(Dictionary<string, object> dict, List<string> propertys = null) where T : ID
- {
- return await FindByDict<T>(dict, propertys);
- }
- public async Task<List<T>> FindByDict<T>(Dictionary<string, object> dict, List<string> propertys = null) where T : ID
- {
- StringBuilder sql;
- sql = SQLHelperParametric.GetSQLSelect(propertys);
- string pk = typeof(T).Name;
- CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql, pk);
- QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
- return await ResultsFromQueryAndOptions<T>(cosmosDbQuery, queryRequestOptions);
- }
- private async Task<List<T>> ResultsFromQueryAndOptions<T>(CosmosDbQuery cosmosDbQuery, QueryRequestOptions queryOptions)
- {
- if (cosmosDbQuery == null) {
- return null;
- }
- CosmosModelInfo container = await InitializeCollection<T>();
- FeedIterator<T> query = container.container.GetItemQueryIterator<T>(
- queryDefinition: cosmosDbQuery.CosmosQueryDefinition,
- requestOptions: queryOptions);
- return await ResultsFromFeedIterator(query);
- }
- private int GetEffectivePageSize(int itemsPerPage, int? maxItemCount)
- {
- return itemsPerPage == -1 ? maxItemCount ?? itemsPerPage : Math.Min(maxItemCount ?? itemsPerPage, itemsPerPage);
- }
- private QueryRequestOptions GetDefaultQueryRequestOptions(int? itemsPerPage = null,
- int? maxBufferedItemCount = null,
- int? maxConcurrency = null)
- {
- QueryRequestOptions queryRequestOptions = new QueryRequestOptions
- {
- MaxItemCount = itemsPerPage == -1 ? 1000 : itemsPerPage,
- MaxBufferedItemCount = maxBufferedItemCount ?? 100,
- MaxConcurrency = maxConcurrency ?? 50
- };
- return queryRequestOptions;
- }
- private async Task<List<T>> ResultsFromQueryAndOptions<T>(CosmosDbQuery cosmosDbQuery, Func<List<T>, Task> batchAction, QueryRequestOptions queryOptions)
- {
- CosmosModelInfo container = await InitializeCollection<T>();
- FeedIterator<T> query = container.container.GetItemQueryIterator<T>(
- queryDefinition: cosmosDbQuery.CosmosQueryDefinition,
- requestOptions: queryOptions);
- return await ResultsFromFeedIterator(query, batchAction, queryOptions.MaxItemCount ?? 0);
- }
- private QueryRequestOptions GetQueryRequestOptions(int itemsPerPage)
- {
- QueryRequestOptions queryRequestOptions = new QueryRequestOptions
- {
- MaxItemCount = itemsPerPage
- };
- return queryRequestOptions;
- }
- public async Task<List<T>> FindLinq<T>(Expression<Func<T, bool>> query = null, Expression<Func<T, object>> order = null, bool isDesc = false) where T : ID
- {
- string pk = typeof(T).Name;
- query = query.And(x => x.pk == pk);
- //QueryRequestOptions queryRequestOptions = GetQueryRequestOptions(itemsPerPage);
- QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
- FeedIterator<T> feedIterator;
- CosmosModelInfo container = await InitializeCollection<T>();
- if (query == null)
- {
- if (order != null)
- {
- if (isDesc)
- {
- feedIterator = container.container
- .GetItemLinqQueryable<T>(requestOptions: queryRequestOptions).OrderByDescending(order)
- .ToFeedIterator();
- }
- else
- {
- feedIterator = container.container
- .GetItemLinqQueryable<T>(requestOptions: queryRequestOptions).OrderBy(order)
- .ToFeedIterator();
- }
- }
- else
- {
- feedIterator = container.container
- .GetItemLinqQueryable<T>(requestOptions: queryRequestOptions)
- .ToFeedIterator();
- }
- }
- else
- {
- if (order != null)
- {
- if (isDesc)
- {
- feedIterator = container.container
- .GetItemLinqQueryable<T>(requestOptions: queryRequestOptions)
- .Where(query).OrderByDescending(order)
- .ToFeedIterator();
- }
- else
- {
- feedIterator = container.container
- .GetItemLinqQueryable<T>(requestOptions: queryRequestOptions)
- .Where(query).OrderBy(order)
- .ToFeedIterator();
- }
- }
- else
- {
- feedIterator = container.container
- .GetItemLinqQueryable<T>(requestOptions: queryRequestOptions)
- .Where(query)
- .ToFeedIterator();
- }
- }
- return await ResultsFromFeedIterator<T>(feedIterator);
- }
- public async Task<List<T>> FindSQL<T>(string sql, Dictionary<string, object> Parameters = null) where T : ID
- {
- if (sql.Contains(".pk"))
- {
- CosmosModelInfo container = await InitializeCollection<T>();
- QueryRequestOptions queryOptions = GetQueryRequestOptions(GetEffectivePageSize(-1, null));
- if (Parameters != null)
- {
- CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
- {
- QueryText = sql,
- Parameters = Parameters
- };
- FeedIterator<T> feedIterator = container.container
- .GetItemQueryIterator<T>(cosmosDbQuery.CosmosQueryDefinition, requestOptions: queryOptions);
- return await ResultsFromFeedIterator(feedIterator);
- }
- else
- {
- QueryDefinition queryDefinition = new QueryDefinition(sql);
- return await ResultsFromFeedIterator<T>(container.container.GetItemQueryIterator<T>(queryDefinition));
- }
- }
- else
- {
- throw new BizException("查询参数必须设置 .pk ", ResponseCode.PARAMS_ERROR);
- }
- }
- public async Task<T> Save<T>(T entity) where T : ID
- {
- try
- {
- CosmosModelInfo container = await InitializeCollection<T>();
- entity.pk = container.type.Name;
- entity.ttl = null;
- ItemResponse<T> response = await container.container.CreateItemAsync<T>(entity);
- if (container.cache && RedisHelper.Instance != null)
- {
- if (!RedisHelper.Exists(CacheCosmosPrefix + container.container.Id))
- {
- await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, entity.id, entity);
- }
- else
- {
- await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, entity.id, entity);
- await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
- }
- }
- return response.Resource;
- }
- catch (Exception e)
- {
- throw new BizException(e.Message);
- }
- }
- public async Task<List<T>> SaveAll<T>(List<T> enyites) where T : ID
- {
- int pages = (int)Math.Ceiling((double)enyites.Count / pageSize);
- CosmosModelInfo container = await InitializeCollection<T>();
- bool flag = false;
- if (RedisHelper.Exists(CacheCosmosPrefix + container.container.Id))
- {
- flag = true;
- }
- string partitionKey = GetPartitionKey<T>();
- Type type = typeof(T);
- Stopwatch stopwatch = Stopwatch.StartNew();
- for (int i = 0; i < pages; i++)
- {
- List<T> lists = enyites.Skip((i) * pageSize).Take(pageSize).ToList();
- List<KeyValuePair<PartitionKey, Stream>> itemsToInsert = new List<KeyValuePair<PartitionKey, Stream>>();
- lists.ForEach(async x =>
- {
- x.pk = type.Name;
- x.ttl = null;
- MemoryStream stream = new MemoryStream();
- await JsonSerializer.SerializeAsync(stream, x, new JsonSerializerOptions { IgnoreNullValues = true });
- object o = type.GetProperty(partitionKey).GetValue(x, null);
- KeyValuePair<PartitionKey, Stream> keyValue = new KeyValuePair<PartitionKey, Stream>(new PartitionKey(o.ToString()), stream);
- itemsToInsert.Add(keyValue);
- });
- List<Task> tasks = new List<Task>(lists.Count);
- itemsToInsert.ForEach(item =>
- {
- tasks.Add(container.container.CreateItemStreamAsync(item.Value, item.Key)
- .ContinueWith((Task<ResponseMessage> task) =>
- {
- using (ResponseMessage response = task.Result)
- {
- if (!response.IsSuccessStatusCode)
- {
- }
- }
- }
- ));
- });
- await Task.WhenAll(tasks);
- if (container.cache && RedisHelper.Instance != null)
- {
- lists.ForEach(async x => {
- await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, x.id, x);
- });
- }
- }
- if (container.cache && RedisHelper.Instance != null && !flag)
- {
- await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
- }
- stopwatch.Stop();
- return enyites;
- }
- public async Task<T> SaveOrUpdate<T>(T entity) where T : ID
- {
- CosmosModelInfo container = await InitializeCollection<T>();
- entity.pk = container.type.Name;
- entity.ttl = null;
- ItemResponse<T> response = await container.container.UpsertItemAsync(item: entity);
- if (container.cache && RedisHelper.Instance != null)
- {
- if (!RedisHelper.Exists(CacheCosmosPrefix + container.container.Id))
- {
- await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, entity.id, entity);
- }
- else
- {
- await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, entity.id, entity);
- await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
- }
- }
- return response.Resource;
- }
- public async Task<List<T>> SaveOrUpdateAll<T>(List<T> enyites) where T : ID
- {
- //await Task.Run(() => Parallel.ForEach(entities, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (item) =>
- //{
- // Task.WaitAll(Update(item));
- //}));
- int pages = (int)Math.Ceiling((double)enyites.Count / pageSize);
- CosmosModelInfo container = await InitializeCollection<T>();
- bool flag = false;
- if (RedisHelper.Exists(CacheCosmosPrefix + container.container.Id))
- {
- flag = true;
- }
- string partitionKey = GetPartitionKey<T>();
- Type type = typeof(T);
- Stopwatch stopwatch = Stopwatch.StartNew();
- for (int i = 0; i < pages; i++)
- {
- List<T> lists = enyites.Skip((i) * pageSize).Take(pageSize).ToList();
- List<KeyValuePair<PartitionKey, Stream>> itemsToInsert = new List<KeyValuePair<PartitionKey, Stream>>();
- lists.ForEach(async x =>
- {
- x.pk = type.Name;
- x.ttl = null;
- MemoryStream stream = new MemoryStream();
- await JsonSerializer.SerializeAsync(stream, x, new JsonSerializerOptions { IgnoreNullValues = true });
- object o = type.GetProperty(partitionKey).GetValue(x, null);
- KeyValuePair<PartitionKey, Stream> keyValue = new KeyValuePair<PartitionKey, Stream>(new PartitionKey(o.ToString()), stream);
- itemsToInsert.Add(keyValue);
- });
- List<Task> tasks = new List<Task>(lists.Count);
- itemsToInsert.ForEach(item =>
- {
- tasks.Add(container.container.UpsertItemStreamAsync(item.Value, item.Key)
- .ContinueWith((Task<ResponseMessage> task) =>
- {
- //using (ResponseMessage response = task.Result)
- //{
- // if (!response.IsSuccessStatusCode)
- // {
- // }
- //}
- }
- ));
- });
- await Task.WhenAll(tasks);
- if (container.cache && RedisHelper.Instance != null)
- {
- lists.ForEach(async x => {
- await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, x.id, x);
- });
- }
- }
- if (container.cache && RedisHelper.Instance != null && !flag)
- {
- await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
- }
- stopwatch.Stop();
- return enyites;
- }
- public async Task<List<dynamic>> UpdateAll (string typeName,List<dynamic> enyites)
- {
- int pages = (int)Math.Ceiling((double)enyites.Count / pageSize);
- if (CosmosDict.typeCosmos.TryGetValue(typeName, out CosmosModelInfo container)) {
- }
- bool flag = false;
- if (RedisHelper.Exists(CacheCosmosPrefix + container.container.Id))
- {
- flag = true;
- }
- string partitionKey = container.partitionKey;
-
- Stopwatch stopwatch = Stopwatch.StartNew();
- for (int i = 0; i < pages; i++)
- {
- List<dynamic> lists = enyites.Skip((i) * pageSize).Take(pageSize).ToList();
- List<Item> itemsToInsert = new List<Item>();
- lists.ForEach(async x =>
- {
- /* x.pk = typeName;
- x.ttl = null;*/
- MemoryStream stream = new MemoryStream();
- await JsonSerializer.SerializeAsync(stream, x, new JsonSerializerOptions { IgnoreNullValues = true });
- object o = x[partitionKey];
- Item keyValue = new Item { id = x.id, pk = o.ToString(), stream = stream };
- itemsToInsert.Add(keyValue);
- });
- List<Task> tasks = new List<Task>(lists.Count);
- itemsToInsert.ForEach(item =>
- {
- tasks.Add(container.container.ReplaceItemStreamAsync(item.stream, item.id, new PartitionKey(item.pk))
- .ContinueWith((Task<ResponseMessage> task) =>
- {
- //using (ResponseMessage response = task.Result)
- //{
- // if (!response.IsSuccessStatusCode)
- // {
- // }
- //}
- }
- ));
- });
- await Task.WhenAll(tasks);
- if (container.cache && RedisHelper.Instance != null)
- {
- lists.ForEach(async x => {
- await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, x.id, x);
- });
- }
- }
- if (container.cache && RedisHelper.Instance != null && !flag)
- {
- await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
- }
- stopwatch.Stop();
- return enyites;
- }
- public async Task<T> Update<T>(T entity) where T : ID
- {
- CosmosModelInfo container = await InitializeCollection<T>();
- string partitionKey = GetPartitionKey<T>();
- Type type = typeof(T);
- entity.pk = type.Name;
- entity.ttl = null;
- object o = type.GetProperty(partitionKey).GetValue(entity, null);
- ItemResponse<T> response = await container.container.ReplaceItemAsync(entity, entity.id, new PartitionKey(o.ToString()));
- if (container.cache && RedisHelper.Instance != null)
- {
- if (!RedisHelper.Exists(CacheCosmosPrefix + container.container.Id))
- {
- await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, entity.id, entity);
- }
- else
- {
- await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, entity.id, entity);
- await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
- }
- }
- return response.Resource;
- }
- internal class Item
- {
- public string id { get; set; }
- public string pk { get; set; }
- public MemoryStream stream { get; set; }
- }
- public async Task<List<T>> UpdateAll<T>(List<T> enyites) where T : ID
- {
- //await Task.Run(() => Parallel.ForEach(entities, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (item) =>
- //{
- // Task.WaitAll(Update(item));
- //}));
- int pages = (int)Math.Ceiling((double)enyites.Count / pageSize);
- CosmosModelInfo container = await InitializeCollection<T>();
- bool flag = false;
- if (RedisHelper.Exists(CacheCosmosPrefix + container.container.Id))
- {
- flag = true;
- }
- string partitionKey = GetPartitionKey<T>();
- Type type = typeof(T);
- Stopwatch stopwatch = Stopwatch.StartNew();
- for (int i = 0; i < pages; i++)
- {
- List<T> lists = enyites.Skip((i) * pageSize).Take(pageSize).ToList();
- List<Item> itemsToInsert = new List<Item>();
- lists.ForEach(async x =>
- {
- x.pk = type.Name;
- x.ttl = null;
- MemoryStream stream = new MemoryStream();
- await JsonSerializer.SerializeAsync(stream, x, new JsonSerializerOptions { IgnoreNullValues = true });
- object o = type.GetProperty(partitionKey).GetValue(x, null);
- Item keyValue = new Item { id = x.id, pk = o.ToString(), stream = stream };
- itemsToInsert.Add(keyValue);
- });
- List<Task> tasks = new List<Task>(lists.Count);
- itemsToInsert.ForEach(item =>
- {
- tasks.Add(container.container.ReplaceItemStreamAsync(item.stream, item.id, new PartitionKey(item.pk))
- .ContinueWith((Task<ResponseMessage> task) =>
- {
- //using (ResponseMessage response = task.Result)
- //{
- // if (!response.IsSuccessStatusCode)
- // {
- // }
- //}
- }
- ));
- });
- await Task.WhenAll(tasks);
- if (container.cache && RedisHelper.Instance != null)
- {
- lists.ForEach(async x => {
- await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, x.id, x);
- });
- }
- }
- if (container.cache && RedisHelper.Instance != null && !flag)
- {
- await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
- }
- stopwatch.Stop();
- return enyites;
- }
- private async Task<List<T>> FindByIdAsSql<T>(string id) where T : ID
- {
- CosmosModelInfo container = await InitializeCollection<T>();
- string pk = container.type.Name;
- CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
- {
- QueryText = @"SELECT *
- FROM c
- WHERE c.pk='" + pk + "' and c.id = @id",
- Parameters = new Dictionary<string, object>
- {
- { "@id",id}
- }
- };
- FeedIterator<T> feedIterator = container.container
- .GetItemQueryIterator<T>(cosmosDbQuery.CosmosQueryDefinition);
- return (await ResultsFromFeedIterator(feedIterator)).ToList();
- }
- public async Task<T> FindByIdPk<T>(string id, string pk) where T : ID
- {
- CosmosModelInfo container = await InitializeCollection<T>();
- try {
- ItemResponse<T> response = await container.container.ReadItemAsync<T>(id: id, partitionKey: new PartitionKey(pk));
- return response.Resource;
- }
- catch (Exception ex) {
- return default;
- }
-
- }
- public async Task<List<T>> FindById<T>(string id, bool cache = true) where T : ID
- {
- CosmosModelInfo container = await InitializeCollection<T>();
- if (container.cache && RedisHelper.Instance != null && cache == true)
- {
- return await RedisHelper.CacheShellAsync(CacheCosmosPrefix + container.container.Id, id, timeoutSeconds, () => { return FindByIdAsSql<T>(id); });
- }
- else
- {
- return await FindByIdAsSql<T>(id);
- }
- }
- public async Task<List<T>> FindByIds<T>(List<string> ids) where T : ID
- {
- CosmosModelInfo container = await InitializeCollection<T>();
- if (container.cache && RedisHelper.Instance != null)
- {
- List<T> list = new List<T>();
- List<string> NotIn = new List<string>();
- foreach (string id in ids)
- {
- if (!await RedisHelper.HExistsAsync(CacheCosmosPrefix + container.container.Id, id))
- {
- NotIn.Add(id);
- }
- else
- {
- list.Add(await RedisHelper.HGetAsync<T>(CacheCosmosPrefix + container.container.Id, id));
- }
- }
- if (NotIn.IsNotEmpty())
- {
- List<T> noInList = await FindByDict<T>(new Dictionary<string, object> { { "id", NotIn.ToArray() } });
- noInList.ForEach(x => { RedisHelper.HSet(CacheCosmosPrefix + container.container.Id, x.id, x); RedisHelper.Expire(CacheCosmosPrefix + container.container.Id, timeoutSeconds); });
- list.AddRange(noInList);
- }
- return list;
- }
- else
- {
- return await FindByDict<T>(new Dictionary<string, object> { { "id", ids.ToArray() } });
- }
- }
- public async Task<dynamic> FindById(string typeName, string id)
- {
- if (CosmosDict.typeCosmos.TryGetValue(typeName, out CosmosModelInfo container))
- {
- if (container.cache && RedisHelper.Instance != null)
- {
- return await RedisHelper.CacheShellAsync(CacheCosmosPrefix + container.container.Id, id, timeoutSeconds, () => { return FindByDict(typeName, new Dictionary<string, object> { { "id", id } }); });
- }
- else
- {
- return await FindByDict(typeName, new Dictionary<string, object> { { "id", id } });
- }
- }
- else
- {
- throw new BizException("CollectionName named:" + typeName + " dose not exsit in Database!");
- }
- }
- public async Task<List<dynamic>> FindByIds(string typeName, List<string> ids)
- {
- if (CosmosDict. typeCosmos.TryGetValue(typeName, out CosmosModelInfo container))
- {
- if (container.cache && RedisHelper.Instance != null)
- {
- return await RedisHelper.CacheShellAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds, () => { return FindByDict(typeName, new Dictionary<string, object> { { "id", ids.ToArray() } }); });
- }
- else
- {
- return await FindByDict(typeName, new Dictionary<string, object> { { "id", ids.ToArray() } });
- }
- }
- else
- {
- throw new BizException("CollectionName named:" + typeName + " dose not exsit in Database!");
- }
- }
- }
- }
|