zhoufan 3 anos atrás
pai
commit
06e87e8c32

+ 37 - 85
Net6Demo_Api.Business/BaseBusiness.cs

@@ -26,10 +26,14 @@ namespace Net6Demo_Api.Business
26 26
         /// 构造函数
27 27
         /// </summary>
28 28
         /// <param name="db">注入数据库</param>
29
-        public BaseBusiness(IDbAccessor db, IDistributedCache cache)
29
+        public BaseBusiness(IDbAccessor db, IDistributedCache cache, bool iscache = false)
30 30
         {
31 31
             _db = db;
32 32
             _cache = cache;
33
+            if (iscache)
34
+            {
35
+                try { cache.Refresh(CacheName); IsCache = true; } catch { IsCache = false; }
36
+            }
33 37
         }
34 38
 
35 39
         #endregion
@@ -42,7 +46,7 @@ namespace Net6Demo_Api.Business
42 46
         /// <summary>
43 47
         /// 缓存名称
44 48
         /// </summary>
45
-        protected string CacheName { get; set; } = "";
49
+        protected string CacheName { get; set; } = typeof(T).Name;
46 50
         /// <summary>
47 51
         /// 主键字段
48 52
         /// </summary>
@@ -360,11 +364,7 @@ namespace Net6Demo_Api.Business
360 364
             #region 启用缓存
361 365
             if (IsCache)
362 366
             {
363
-                var listCache = GetRedis().Result;
364
-                if (listCache != null)
365
-                {
366
-                    return listCache.AsQueryable().Where(p => KeyFiled.GetValue(p).ToString() == keyValue[0].ToString()).FirstOrDefault();
367
-                }
367
+                return GetIQueryable().Where(p => KeyFiled.GetValue(p).ToString() == keyValue[0].ToString()).FirstOrDefault();
368 368
             }
369 369
             #endregion
370 370
             return _db.GetEntity<T>(keyValue);
@@ -380,11 +380,7 @@ namespace Net6Demo_Api.Business
380 380
             #region 启用缓存
381 381
             if (IsCache)
382 382
             {
383
-                var listCache = await GetRedis();
384
-                if (listCache != null)
385
-                {
386
-                    return listCache.AsQueryable().Where(p => KeyFiled.GetValue(p).ToString() == keyValue[0].ToString()).FirstOrDefault();
387
-                }
383
+                return GetIQueryable().Where(p => KeyFiled.GetValue(p).ToString() == keyValue[0].ToString()).FirstOrDefault();
388 384
             }
389 385
             #endregion
390 386
 
@@ -397,18 +393,7 @@ namespace Net6Demo_Api.Business
397 393
         /// <returns></returns>
398 394
         public int GetCount(Expression<Func<T, bool>> condition)
399 395
         {
400
-            #region 启用缓存
401
-            if (IsCache)
402
-            {
403
-                var listCache = GetRedis().Result;
404
-                if (listCache != null)
405
-                {
406
-                    return listCache.AsQueryable().Where(condition).Count();
407
-                }
408
-            }
409
-            #endregion
410
-
411
-            return _db.GetIQueryable<T>().AsNoTracking().Where(condition).Count();
396
+            return GetIQueryable().Where(condition).Count();
412 397
         }
413 398
 
414 399
         /// <summary>
@@ -417,18 +402,10 @@ namespace Net6Demo_Api.Business
417 402
         /// <returns></returns>
418 403
         public async Task<int> GetCountAsync(Expression<Func<T, bool>> condition)
419 404
         {
420
-            #region 启用缓存
421
-            if (IsCache)
422
-            {
423
-                var listCache = await GetRedis();
424
-                if (listCache != null)
425
-                {
426
-                    return listCache.AsQueryable().Where(condition).Count();
427
-                }
428
-            }
429
-            #endregion
405
+            var query = GetIQueryable().Where(condition);
430 406
 
431
-            return await _db.GetIQueryable<T>().AsNoTracking().Where(condition).CountAsync();
407
+            if (IsCache) return query.Count();
408
+            return await query.CountAsync();
432 409
         }
433 410
 
434 411
         /// <summary>
@@ -437,17 +414,7 @@ namespace Net6Demo_Api.Business
437 414
         /// <returns></returns>
438 415
         public List<T> GetList()
439 416
         {
440
-            #region 启用缓存
441
-            if (IsCache)
442
-            {
443
-                var listCache = GetRedis().Result;
444
-                if (listCache != null)
445
-                {
446
-                    return listCache;
447
-                }
448
-            }
449
-            #endregion
450
-            return _db.GetIQueryable<T>().AsNoTracking().ToList();
417
+            return GetIQueryable().ToList();
451 418
         }
452 419
 
453 420
         /// <summary>
@@ -457,18 +424,8 @@ namespace Net6Demo_Api.Business
457 424
         /// <returns></returns>
458 425
         public List<T> GetList(Expression<Func<T, bool>> condition)
459 426
         {
460
-            #region 启用缓存
461
-            if (IsCache)
462
-            {
463
-                var listCache = GetRedis().Result;
464
-                if (listCache != null)
465
-                {
466
-                    return listCache.AsQueryable().Where(condition).ToList();
467
-                }
468
-            }
469
-            #endregion
470
-
471
-            return _db.GetIQueryable<T>().AsNoTracking().Where(condition).ToList();
427
+            var query = GetIQueryable().Where(condition);
428
+            return query.ToList();
472 429
         }
473 430
 
474 431
         /// <summary>
@@ -477,17 +434,9 @@ namespace Net6Demo_Api.Business
477 434
         /// <returns></returns>
478 435
         public async Task<List<T>> GetListAsync()
479 436
         {
480
-            #region 启用缓存
481
-            if (IsCache)
482
-            {
483
-                var listCache = await GetRedis();
484
-                if (listCache != null)
485
-                {
486
-                    return listCache;
487
-                }
488
-            }
489
-            #endregion
490
-            return await _db.GetIQueryable<T>().AsNoTracking().ToListAsync();
437
+            var query = GetIQueryable();
438
+            if (IsCache) return query.ToList();
439
+            return await query.ToListAsync();
491 440
         }
492 441
 
493 442
         /// <summary>
@@ -497,18 +446,9 @@ namespace Net6Demo_Api.Business
497 446
         /// <returns></returns>
498 447
         public async Task<List<T>> GetListAsync(Expression<Func<T, bool>> condition)
499 448
         {
500
-            #region 启用缓存
501
-            if (IsCache)
502
-            {
503
-                var listCache = await GetRedis();
504
-                if (listCache != null)
505
-                {
506
-                    return listCache.AsQueryable().Where(condition).ToList();
507
-                }
508
-            }
509
-            #endregion
510
-
511
-            return await _db.GetIQueryable<T>().AsNoTracking().Where(condition).ToListAsync();
449
+            var query = GetIQueryable().Where(condition);
450
+            if (IsCache) return query.ToList();
451
+            return await query.ToListAsync();
512 452
         }
513 453
 
514 454
         /// <summary>
@@ -520,9 +460,13 @@ namespace Net6Demo_Api.Business
520 460
         /// <returns></returns>
521 461
         public async Task<ValueTuple<List<T>, int>> GetListPageAsync(PageInput page, Expression<Func<T, bool>> condition, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy)
522 462
         {
523
-            var iq = _db.GetIQueryable<T>().AsNoTracking().Where(condition);
524
-
525
-            var list = await orderBy(iq).Skip((page.PageIndex - 1) * page.PageRows).Take(page.PageRows).ToListAsync();
463
+            var iq = GetIQueryable().Where(condition);
464
+            var query = orderBy(iq).Skip((page.PageIndex - 1) * page.PageRows).Take(page.PageRows);
465
+            if (IsCache)
466
+            {
467
+                return (query.ToList(), iq.Count());
468
+            }
469
+            var list = await query.ToListAsync();
526 470
             int total = await iq.CountAsync();
527 471
 
528 472
             return (list, total);
@@ -534,7 +478,15 @@ namespace Net6Demo_Api.Business
534 478
         /// <returns></returns>
535 479
         public virtual IQueryable<T> GetIQueryable()
536 480
         {
537
-            return _db.GetIQueryable<T>();
481
+            if (IsCache)
482
+            {
483
+                var listCache = GetRedis().Result;
484
+                if (listCache != null)
485
+                {
486
+                    return listCache.AsQueryable();
487
+                }
488
+            }
489
+            return _db.GetIQueryable<T>().AsNoTracking();
538 490
         }
539 491
 
540 492
         #endregion

+ 1 - 3
Net6Demo_Api.Business/System/CustomFiledBusiness.cs

@@ -8,10 +8,8 @@ namespace Net6Demo_Api.Business
8 8
 {
9 9
     public class CustomFiledBusiness : BaseBusiness<T_Sys_CustomFiled>, ICustomFiledBusiness, ITransientDependency
10 10
     {
11
-        public CustomFiledBusiness(IDbAccessor db, IDistributedCache cache) : base(db, cache)
11
+        public CustomFiledBusiness(IDbAccessor db, IDistributedCache cache) : base(db, cache, true)
12 12
         {
13
-            IsCache = true;
14
-            CacheName = "T_Sys_CustomFiled";
15 13
         }
16 14
     }
17 15
 }

+ 1 - 3
Net6Demo_Api.Business/System/DepartmentBusiness.cs

@@ -14,10 +14,8 @@ namespace Net6Demo_Api.Business
14 14
 {
15 15
     public class DepartmentBusiness : BaseBusiness<T_Sys_Department>, IDepartmentBusiness, ITransientDependency
16 16
     {
17
-        public DepartmentBusiness(IDbAccessor db, IDistributedCache cache) : base(db, cache)
17
+        public DepartmentBusiness(IDbAccessor db, IDistributedCache cache) : base(db, cache, true)
18 18
         {
19
-            IsCache = true;
20
-            CacheName = "T_Sys_Department";
21 19
         }
22 20
     }
23 21
 }

+ 1 - 3
Net6Demo_Api.Business/System/MenuBusiness.cs

@@ -8,10 +8,8 @@ namespace Net6Demo_Api.Business
8 8
 {
9 9
     public class MenuBusiness : BaseBusiness<T_Sys_Menu>, IMenuBusiness, ITransientDependency
10 10
     {
11
-        public MenuBusiness(IDbAccessor db, IDistributedCache cache) : base(db, cache)
11
+        public MenuBusiness(IDbAccessor db, IDistributedCache cache) : base(db, cache, true)
12 12
         {
13
-            IsCache = true;
14
-            CacheName = "T_Sys_Menu";
15 13
         }
16 14
     }
17 15
 }

+ 1 - 3
Net6Demo_Api.Business/System/RoleBusiness.cs

@@ -8,10 +8,8 @@ namespace Net6Demo_Api.Business
8 8
 {
9 9
     public class RoleBusiness : BaseBusiness<T_Sys_Role>, IRoleBusiness, ITransientDependency
10 10
     {
11
-        public RoleBusiness(IDbAccessor db, IDistributedCache cache) : base(db, cache)
11
+        public RoleBusiness(IDbAccessor db, IDistributedCache cache) : base(db, cache, true)
12 12
         {
13
-            IsCache = true;
14
-            CacheName = "T_Sys_Role";
15 13
         }
16 14
     }
17 15
 }

+ 1 - 3
Net6Demo_Api.Business/System/RoleMenuBusiness.cs

@@ -8,10 +8,8 @@ namespace Net6Demo_Api.Business
8 8
 {
9 9
     public class RoleMenuBusiness : BaseBusiness<T_Sys_RoleMenu>, IRoleMenuBusiness, ITransientDependency
10 10
     {
11
-        public RoleMenuBusiness(IDbAccessor db, IDistributedCache cache) : base(db, cache)
11
+        public RoleMenuBusiness(IDbAccessor db, IDistributedCache cache) : base(db, cache, true)
12 12
         {
13
-            IsCache = true;
14
-            CacheName = "T_Sys_RoleMenu";
15 13
         }
16 14
 
17 15
         /// <summary>

+ 1 - 3
Net6Demo_Api.Business/System/SystemConfigBusiness.cs

@@ -8,10 +8,8 @@ namespace Net6Demo_Api.Business
8 8
 {
9 9
     public class SystemConfigBusiness : BaseBusiness<T_Sys_SystemConfig>, ISystemConfigBusiness, ITransientDependency
10 10
     {
11
-        public SystemConfigBusiness(IDbAccessor db, IDistributedCache cache) : base(db, cache)
11
+        public SystemConfigBusiness(IDbAccessor db, IDistributedCache cache) : base(db, cache, true)
12 12
         {
13
-            IsCache = true;
14
-            CacheName = "T_Sys_SystemConfig";
15 13
         }
16 14
     }
17 15
 }

+ 1 - 3
Net6Demo_Api.Business/System/UserAccountBusiness.cs

@@ -18,10 +18,8 @@ namespace Net6Demo_Api.Business
18 18
     public class UserAccountBusiness : BaseBusiness<T_Sys_UserAccount>, IUserAccountBusiness, ITransientDependency
19 19
     {
20 20
         private readonly IMapper _mapper;
21
-        public UserAccountBusiness(IDbAccessor db, IDistributedCache cache, IMapper mapper) : base(db, cache)
21
+        public UserAccountBusiness(IDbAccessor db, IDistributedCache cache, IMapper mapper) : base(db, cache, true)
22 22
         {
23
-            IsCache = true;
24
-            CacheName = "T_Sys_UserAccount";
25 23
             _mapper = mapper;
26 24
         }
27 25