فهرست منبع

完善Key檢測超時時間處理。

CrazyIter 5 سال پیش
والد
کامیت
69a6c3d73f

+ 63 - 11
TEAMModelOS.SDK/Module/AzureCosmosDBV3/AzureCosmosDBV3Repository.cs

@@ -638,7 +638,15 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
                 CosmosModelInfo container = await InitializeCollection<T>();
                 ItemResponse<T> response = await container.container.CreateItemAsync<T>(entity);
                 if (container.cache && RedisHelper.Instance!=null) {
-                    await   RedisHelper.HSetAsync(CacheCosmosPrefix+container.container.Id, entity.id, entity);
+                    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;
             }
@@ -650,8 +658,15 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
 
         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 pk = GetPartitionKey<T>();
             Type type = typeof(T);
             Stopwatch stopwatch = Stopwatch.StartNew();
@@ -692,6 +707,9 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
                     }
                 });
             }
+            if (container.cache && RedisHelper.Instance != null&&!flag) {
+                await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
+            }
             stopwatch.Stop();
             return enyites;
         }
@@ -701,7 +719,15 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
             ItemResponse<T> response = await container.container.UpsertItemAsync(item: entity);
             if (container.cache && RedisHelper.Instance != null)
             {
-                await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, entity.id, entity);
+                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;
         }
@@ -714,6 +740,11 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
 
             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 pk = GetPartitionKey<T>();
             Type type = typeof(T);
             Stopwatch stopwatch = Stopwatch.StartNew();
@@ -752,6 +783,10 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
                     }
                 });
             }
+            if (container.cache && RedisHelper.Instance != null&&!flag)
+            {
+                await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
+            }
             stopwatch.Stop();
             return enyites;
         }
@@ -764,7 +799,15 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
             ItemResponse<T> response = await container.container.ReplaceItemAsync(entity, entity.id, new PartitionKey(o.ToString()));
             if (container.cache && RedisHelper.Instance != null)
             {
-                await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, entity.id, entity);
+                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;
         }
@@ -786,6 +829,11 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
 
             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 pk = GetPartitionKey<T>();
             Type type = typeof(T);
             Stopwatch stopwatch = Stopwatch.StartNew();
@@ -824,6 +872,10 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
                     }
                 });
             }
+            if (container.cache && RedisHelper.Instance != null&&!flag)
+            {
+                await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
+            }
             stopwatch.Stop();
             return enyites;
         }
@@ -839,8 +891,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
         //        CosmosClient?.Dispose();
         //    }
         //}
-
-        public async Task<T> FindById<T>(string id) where T : ID
+        private async Task<T> FindByIdAsSql<T>(string id) where T : ID
         {
             CosmosModelInfo container = await InitializeCollection<T>();
             CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
@@ -859,6 +910,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
             return (await ResultsFromFeedIterator(feedIterator)).SingleOrDefault();
         }
 
+
         public async Task<T> FindByIdPk<T>(string id, string pk) where T : ID
         {
             CosmosModelInfo container = await InitializeCollection<T>();
@@ -866,22 +918,22 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
             return response.Resource;
         }
 
-        public async Task<T> FindCacheShell<T>(string id) where T : ID
+        public async Task<T> FindById<T>(string id) where T : ID
         {
             CosmosModelInfo container = await InitializeCollection<T>();
             if (container.cache && RedisHelper.Instance != null)
             {
 
-                return await RedisHelper.CacheShellAsync(CacheCosmosPrefix + container.container.Id , id , timeoutSeconds, () => { return FindById<T>(id); }) ;
+                return await RedisHelper.CacheShellAsync(CacheCosmosPrefix + container.container.Id , id , timeoutSeconds, () => { return FindByIdAsSql<T>(id); }) ;
                 
             }
             else {
-                return await FindById<T>(id);
+                return await FindByIdAsSql<T>(id);
             }
             
         }
 
-        public async  Task<List<T>> FindListCacheShell<T>(List<string> ids) where T : ID
+        public async  Task<List<T>> FindByIds<T>(List<string> ids) where T : ID
         {
             CosmosModelInfo container = await InitializeCollection<T>();
           
@@ -911,7 +963,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
             }
         }
 
-        public async Task<dynamic> FindCacheShell(string CollectionName, string id)
+        public async Task<dynamic> FindById(string CollectionName, string id)
         {
             if (DocumentCollectionDict.TryGetValue(CollectionName, out CosmosModelInfo container))
             {
@@ -932,7 +984,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
             }
         }
 
-        public async Task<List<dynamic>> FindListCacheShell(string CollectionName, List<string> ids)
+        public async Task<List<dynamic>> FindByIds(string CollectionName, List<string> ids)
         {
             if (DocumentCollectionDict.TryGetValue(CollectionName, out CosmosModelInfo container))
             {

+ 5 - 5
TEAMModelOS.SDK/Module/AzureCosmosDBV3/IAzureCosmosDBV3Repository.cs

@@ -37,7 +37,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
         Task<List<T>> UpdateAll<T>(List<T> entities) where T : ID;
         Task<T> SaveOrUpdate<T>(T entity) where T : ID;
         Task<List<T>> SaveOrUpdateAll<T>(List<T> entities) where T : ID;
-        Task<T> FindById<T>(string id) where T : ID;
+     //   Task<T> FindById<T>(string id) where T : ID;
         Task<T> FindByIdPk<T>(string id, string pk) where T : ID;
         // Task<string> ReplaceObject<T>(T entity, string key, string partitionKey) where T : ID;
         Task<List<T>> FindAll<T>(List<string> propertys = null) where T : ID;
@@ -80,9 +80,9 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
         Task<List<dynamic>> FindByDict(string CollectionName, Dictionary<string, object> dict, int itemsPerPage = -1, int? maxItemCount = null, string partitionKey = null, List<string> propertys = null);
         Task<List<dynamic>> FindCountByDict(string CollectionName, Dictionary<string, object> dict, int itemsPerPage = -1, int? maxItemCount = null, string partitionKey = null);
         Task InitializeDatabase();
-        Task<T>FindCacheShell<T>(string id) where T : ID;
-        Task<List<T>> FindListCacheShell<T>(List<string> ids) where T : ID;
-        Task<dynamic> FindCacheShell(string CollectionName, string id);
-        Task<List<dynamic>> FindListCacheShell (string CollectionName, List<string> ids);
+        Task<T>FindById<T>(string id) where T : ID;
+        Task<List<T>> FindByIds<T>(List<string> ids) where T : ID;
+        Task<dynamic> FindById(string CollectionName, string id);
+        Task<List<dynamic>> FindByIds (string CollectionName, List<string> ids);
     }
 }

+ 1 - 1
TEAMModelOS/Controllers/Exam/ItemInfoController.cs

@@ -120,7 +120,7 @@ namespace TEAMModelOS.Controllers.Exam
         public async Task<BaseJosnRPCResponse> FindCacheShell(JosnRPCRequest<List<string>  > request)
         {
             JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
-            List<ExamItem> items = await cosmosDBV3Repository.FindListCacheShell<ExamItem>(request.@params);
+            List<ExamItem> items = await cosmosDBV3Repository.FindByIds<ExamItem>(request.@params);
             //List<string> ps = new List<string>() { "6f705b1b-8221-5307-18da-13da05adf91e", "c3e1e95a-561e-fabf-cce3-3a564782e443" };
             //
             //

+ 2 - 1
TEAMModelOS/Startup.cs

@@ -117,7 +117,8 @@ namespace TEAMModelOS
             services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
             //引入Jwt配置
             services.JwtAuth(Configuration.GetSection("JwtSetting"));
-            var csredis = new CSRedis.CSRedisClient("106.12.23.251:6379,password=habook,ssl=false,abortConnect=False,defaultDatabase=13,writeBuffer=10240,poolsize=50,prefix=habook:");
+            //var csredis = new CSRedis.CSRedisClient("106.12.23.251:6379,password=habook,ssl=false,abortConnect=False,defaultDatabase=13,writeBuffer=10240,poolsize=50,prefix=habook:");
+            var csredis = new CSRedis.CSRedisClient("teammodel.redis.cache.chinacloudapi.cn:6380,password=9wcTVDiAWiAf0IF5NOjZBvYrZe9JlbEw1E3F1QFcNbo=,ssl=True,abortConnect=False,defaultDatabase=13,writeBuffer=10240,poolsize=50,prefix=habook:");
             RedisHelper.Initialization(csredis);
             //services.AddSingleton<PowerPointService, PowerPointService>();