Просмотр исходного кода

升级优化SqlSugar二级缓存实现及短雪花Id

zuohuaijun 3 лет назад
Родитель
Сommit
ca906f57c5

+ 1 - 1
Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj

@@ -36,7 +36,7 @@
     <PackageReference Include="OnceMi.AspNetCore.OSS" Version="1.1.8" />
     <PackageReference Include="SKIT.FlurlHttpClient.Wechat.Api" Version="2.17.0" />
     <PackageReference Include="SKIT.FlurlHttpClient.Wechat.TenpayV3" Version="2.13.1" />
-    <PackageReference Include="SqlSugarCore" Version="5.1.2.9" />
+    <PackageReference Include="SqlSugarCore" Version="5.1.3.1" />
     <PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.20" />
     <PackageReference Include="UAParser" Version="3.1.47" />
     <PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />

+ 30 - 17
Admin.NET/Admin.NET.Core/Cache/SqlSugarCache.cs

@@ -1,53 +1,66 @@
-namespace Admin.NET.Core;
+using Microsoft.Extensions.Caching.Memory;
+
+namespace Admin.NET.Core;
 
 /// <summary>
 /// SqlSugar二级缓存
 /// </summary>
-public class SqlSugarCache : ICacheService
+public class SqlSugarCache : ICacheService, ISingleton, IDisposable
 {
-    private static IDistributedCache _cache = App.GetService<IDistributedCache>();
+    private static readonly Lazy<IMemoryCache> lazyCache = new(() => new MemoryCache(new MemoryCacheOptions()));
+    public static IMemoryCache _cache => lazyCache.Value;
 
     public void Add<V>(string key, V value)
     {
-        _cache.Set(key, Encoding.UTF8.GetBytes(JSON.Serialize(value)));
+        _cache.Set(key, value);
     }
 
     public void Add<V>(string key, V value, int cacheDurationInSeconds)
     {
-        _cache.Set(key, Encoding.UTF8.GetBytes(JSON.Serialize(value)), new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(cacheDurationInSeconds) });
+        _cache.Set(key, value, TimeSpan.FromSeconds(cacheDurationInSeconds));
     }
 
     public bool ContainsKey<V>(string key)
     {
-        return _cache.Get(key) != null;
+        return _cache.TryGetValue(key, out _);
     }
 
     public V Get<V>(string key)
     {
-        var res = _cache.Get(key);
-        return res == null ? default : JSON.Deserialize<V>(Encoding.UTF8.GetString(res));
+        return _cache.Get<V>(key);
     }
 
     public IEnumerable<string> GetAllKey<V>()
     {
         const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
-        var entries = _cache.GetType().GetField("_entries", flags)?.GetValue(_cache);
-        var cacheItems = entries?.GetType().GetProperty("Keys").GetValue(entries) as ICollection<object>;
-        return cacheItems == null ? new List<string>() : cacheItems.Select(u => u.ToString()).ToList();
+        var entries = _cache.GetType().GetField("_entries", flags).GetValue(_cache);
+        var cacheItems = entries as IDictionary;
+        var keys = new List<string>();
+        if (cacheItems == null) return keys;
+        foreach (DictionaryEntry cacheItem in cacheItems)
+        {
+            keys.Add(cacheItem.Key.ToString());
+        }
+        return keys;
     }
 
     public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
     {
-        if (!ContainsKey<V>(cacheKey))
-            return JSON.Deserialize<V>(Encoding.UTF8.GetString(_cache.Get(cacheKey)));
-
-        var result = create();
-        Add(cacheKey, result, cacheDurationInSeconds);
-        return result;
+        if (!_cache.TryGetValue<V>(cacheKey, out V value))
+        {
+            value = create();
+            _cache.Set(cacheKey, value, TimeSpan.FromSeconds(cacheDurationInSeconds));
+        }
+        return value;
     }
 
     public void Remove<V>(string key)
     {
         _cache.Remove(key);
     }
+
+    public void Dispose()
+    {
+        _cache.Dispose();
+    }
 }

+ 8 - 9
Admin.NET/Admin.NET.Core/Job/OnlineUserJob.cs

@@ -11,15 +11,14 @@ public class OnlineUserJob : ISpareTimeWorker
     [SpareTime(1000, "清空在线用户", Description = "服务启动时运行", DoOnce = true, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)]
     public void ClearOnlineUser(SpareTimer timer, long count)
     {
-        Scoped.Create(async (_, scope) =>
-        {
-            var services = scope.ServiceProvider;
-            var rep = services.GetService<SqlSugarRepository<SysOnlineUser>>();
-            if (rep == null) return;
-            await rep.AsDeleteable().ExecuteCommandAsync();
+        //Scoped.Create(async (_, scope) =>
+        //{
+        //    var services = scope.ServiceProvider;
+        //    var rep = services.GetService<SqlSugarRepository<SysOnlineUser>>();
+        //    await rep.AsDeleteable().ExecuteCommandAsync();
 
-            Console.ForegroundColor = ConsoleColor.Blue;
-            Console.WriteLine("【" + DateTime.Now + "——清空在线用户】\r\n服务重启触发清空在线用户");
-        });
+        //    Console.ForegroundColor = ConsoleColor.Blue;
+        //    Console.WriteLine("【" + DateTime.Now + "——清空在线用户】\r\n服务重启触发清空在线用户");
+        //});
     }
 }

+ 19 - 18
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -11,6 +11,7 @@ public static class SqlSugarSetup
     public static void AddSqlSugar(this IServiceCollection services)
     {
         var dbOptions = App.GetOptions<DbConnectionOptions>();
+        var sqlSugarCache = new SqlSugarCache();
         var configureExternalServices = new ConfigureExternalServices
         {
             EntityService = (type, column) => // 修改列可空-1、带?问号 2、String类型若没有Required
@@ -19,7 +20,7 @@ public static class SqlSugarSetup
                     || (type.PropertyType == typeof(string) && type.GetCustomAttribute<RequiredAttribute>() == null))
                     column.IsNullable = true;
             },
-            DataInfoCacheService = new SqlSugarCache(),
+            DataInfoCacheService = sqlSugarCache,
         };
         dbOptions.ConnectionConfigs.ForEach(config =>
         {
@@ -32,17 +33,17 @@ public static class SqlSugarSetup
             };
         });
 
-        SqlSugarScope sqlSugar = new(dbOptions.ConnectionConfigs, db =>
+        SqlSugarScope sqlSugar = new(dbOptions.ConnectionConfigs, client =>
         {
             dbOptions.ConnectionConfigs.ForEach(config =>
             {
-                var dbProvider = db.GetConnectionScope((string)config.ConfigId);
+                var db = client.GetConnectionScope((string)config.ConfigId);
 
                 // 设置超时时间
-                dbProvider.Ado.CommandTimeOut = 30;
+                db.Ado.CommandTimeOut = 30;
 
                 // 打印SQL语句
-                dbProvider.Aop.OnLogExecuting = (sql, pars) =>
+                db.Aop.OnLogExecuting = (sql, pars) =>
                 {
                     if (sql.StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
                         Console.ForegroundColor = ConsoleColor.Green;
@@ -53,7 +54,7 @@ public static class SqlSugarSetup
                     Console.WriteLine("【" + DateTime.Now + "——执行SQL】\r\n" + UtilMethods.GetSqlString(config.DbType, sql, pars) + "\r\n");
                     App.PrintToMiniProfiler("SqlSugar", "Info", sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                 };
-                dbProvider.Aop.OnError = (ex) =>
+                db.Aop.OnError = (ex) =>
                 {
                     Console.ForegroundColor = ConsoleColor.Red;
                     var pars = db.Utilities.SerializeObject(((SugarParameter[])ex.Parametres).ToDictionary(it => it.ParameterName, it => it.Value));
@@ -62,7 +63,7 @@ public static class SqlSugarSetup
                 };
 
                 // 数据审计
-                dbProvider.Aop.DataExecuting = (oldValue, entityInfo) =>
+                db.Aop.DataExecuting = (oldValue, entityInfo) =>
                 {
                     // 新增操作
                     if (entityInfo.OperationType == DataFilterType.InsertByObject)
@@ -101,7 +102,7 @@ public static class SqlSugarSetup
                 };
 
                 // 差异日志
-                dbProvider.Aop.OnDiffLogEvent = async u =>
+                db.Aop.OnDiffLogEvent = async u =>
                 {
                     if (!dbOptions.EnableDiffLog) return;
                     var LogDiff = new SysLogDiff
@@ -118,7 +119,7 @@ public static class SqlSugarSetup
                         Parameters = JsonConvert.SerializeObject(u.Parameters),
                         Duration = u.Time == null ? 0 : (long)u.Time.Value.TotalMilliseconds
                     };
-                    await db.GetConnectionScope(SqlSugarConst.ConfigId).Insertable(LogDiff).ExecuteCommandAsync();
+                    await client.GetConnectionScope(SqlSugarConst.ConfigId).Insertable(LogDiff).ExecuteCommandAsync();
                     Console.ForegroundColor = ConsoleColor.Red;
                     Console.WriteLine(DateTime.Now + $"\r\n**********差异日志开始**********\r\n{Environment.NewLine}{JsonConvert.SerializeObject(LogDiff)}{Environment.NewLine}**********差异日志结束**********\r\n");
                 };
@@ -128,19 +129,19 @@ public static class SqlSugarSetup
                 if (queryFilterProvider == null)
                 {
                     // 配置实体假删除过滤器
-                    SetDeletedEntityFilter(dbProvider);
+                    SetDeletedEntityFilter(db);
                     // 配置实体机构过滤器
-                    SetOrgEntityFilter(dbProvider);
+                    SetOrgEntityFilter(db);
                     // 配置自定义实体过滤器
-                    SetCustomEntityFilter(dbProvider);
+                    SetCustomEntityFilter(db);
                     // 配置租户实体过滤器
-                    SetTenantEntityFilter(dbProvider);
+                    SetTenantEntityFilter(db);
 
-                    db.DataCache.Add(config.ConfigId, dbProvider.QueryFilter);
+                    db.DataCache.Add(config.ConfigId, db.QueryFilter);
                 }
                 else
                 {
-                    dbProvider.QueryFilter = queryFilterProvider;
+                    db.QueryFilter = queryFilterProvider;
                 }
             });
         });
@@ -193,13 +194,13 @@ public static class SqlSugarSetup
 
             var entityType = seedType.GetInterfaces().First().GetGenericArguments().First();
             var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
-            var provider = db.GetConnectionScope(tAtt == null ? SqlSugarConst.ConfigId : tAtt.configId);
+            var db2 = db.GetConnectionScope(tAtt == null ? SqlSugarConst.ConfigId : tAtt.configId);
 
             var seedDataTable = seedData.ToList().ToDataTable();
             seedDataTable.TableName = db.EntityMaintenance.GetEntityInfo(entityType).DbTableName;
             if (seedDataTable.Columns.Contains(SqlSugarConst.PrimaryKey))
             {
-                var storage = provider.Storageable(seedDataTable).WhereColumns(SqlSugarConst.PrimaryKey).ToStorage();
+                var storage = db2.Storageable(seedDataTable).WhereColumns(SqlSugarConst.PrimaryKey).ToStorage();
                 //// 如果添加一条种子数,sqlsugar 默认以 @param 的方式赋值,如果 PropertyType 为空,则默认数据类型为字符串。插入 pgsql 时候会报错,所以要忽略为空的值添加
                 //_ = ((InsertableProvider<Dictionary<string, object>>)storage.AsInsertable).IsSingle ?
                 //    storage.AsInsertable.IgnoreColumns("UpdateTime", "UpdateUserId", "CreateUserId").ExecuteCommand() :
@@ -208,7 +209,7 @@ public static class SqlSugarSetup
             }
             else // 没有主键或者不是预定义的主键(没主键有重复的可能)
             {
-                var storage = provider.Storageable(seedDataTable).ToStorage();
+                var storage = db2.Storageable(seedDataTable).ToStorage();
                 storage.AsInsertable.ExecuteCommand();
             }
         }

+ 5 - 4
Admin.NET/Admin.NET.Web.Core/Startup.cs

@@ -25,10 +25,6 @@ public class Startup : AppStartup
     {
         // 配置选项
         services.AddProjectOptions();
-        // 缓存注册
-        services.AddCache();
-        // SqlSugar
-        services.AddSqlSugar();
         // JWT
         services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
         // 允许跨域
@@ -65,6 +61,11 @@ public class Startup : AppStartup
             })
             .AddInjectWithUnifyResult<AdminResultProvider>();
 
+        // 缓存注册
+        services.AddCache();
+        // SqlSugar
+        services.AddSqlSugar();
+
         // 配置Nginx转发获取客户端真实IP
         // 注1:如果负载均衡不是在本机通过 Loopback 地址转发请求的,一定要加上options.KnownNetworks.Clear()和options.KnownProxies.Clear()
         // 注2:如果设置环境变量 ASPNETCORE_FORWARDEDHEADERS_ENABLED 为 True,则不需要下面的配置代码