static void Main(string[] args) { var ib = new IdleBus(TimeSpan.FromSeconds(10)); ib.Notice += (_, e2) => { if (e2.NoticeType == IdleBus <IDisposable> .NoticeType.AutoCreate || e2.NoticeType == IdleBus <IDisposable> .NoticeType.AutoRelease) { var log = $"[{DateTime.Now.ToString("HH:mm:ss")}] 线程{Thread.CurrentThread.ManagedThreadId}:{e2.Log}"; //Trace.WriteLine(log); Console.WriteLine(log); } }; var testkey1 = ib.Exists("key1"); ib .Register("key1", () => new ManualResetEvent(false)) .Register("key2", () => new AutoResetEvent(false)); var testkey2 = ib.Exists("key1"); for (var a = 3; a < 2000; a++) { ib.Register("key" + a, () => new System.Data.SqlClient.SqlConnection()); } for (var a = 1; a < 2000; a++) { ib.Get("key" + a); } var now = DateTime.Now; int counter = 100 * 1000; for (var k = 0; k < 100; k++) { new Thread(() => { var rnd = new Random(); for (var a = 0; a < 1000; a++) { for (var l = 0; l < 10; l++) { ib.Get("key" + rnd.Next(1, 2000)); } if (Interlocked.Decrement(ref counter) <= 0) { Console.WriteLine($"测试完成,100线程并发 ib.Get() 获取100万次,耗时:{DateTime.Now.Subtract(now).TotalMilliseconds}ms"); } } }).Start(); } Console.ReadKey(); ib.Dispose(); }
/// <summary> /// 获得FreeSql实例 /// </summary> /// <param name="ib"></param> /// <param name="serviceProvider"></param> /// <returns></returns> public static IFreeSql GetFreeSql(this IdleBus <IFreeSql> ib, IServiceProvider serviceProvider) { var user = serviceProvider.GetRequiredService <IUser>(); var appConfig = serviceProvider.GetRequiredService <AppConfig>(); if (appConfig.Tenant && user.DataIsolationType == DataIsolationType.OwnDb && user.TenantId.HasValue) { var tenantName = "tenant_" + user.TenantId.ToString(); var exists = ib.Exists(tenantName); if (!exists) { var dbConfig = serviceProvider.GetRequiredService <DbConfig>(); //查询租户数据库信息 var freeSql = serviceProvider.GetRequiredService <IFreeSql>(); var tenantRepository = freeSql.GetRepository <TenantEntity>(); var tenant = tenantRepository.Select.DisableGlobalFilter("Tenant").WhereDynamic(user.TenantId).ToOne <CreateFreeSqlTenantDto>(); var timeSpan = tenant.IdleTime.HasValue && tenant.IdleTime.Value > 0 ? TimeSpan.FromMinutes(tenant.IdleTime.Value) : TimeSpan.MaxValue; ib.TryRegister(tenantName, () => CreateFreeSql(user, appConfig, dbConfig, tenant), timeSpan); } return(ib.Get(tenantName)); } else { var freeSql = serviceProvider.GetRequiredService <IFreeSql>(); return(freeSql); } }
/// <summary> /// 检查库 /// </summary> /// <param name="key">库标识</param> private void Check(TKey key) { if (!Options.KeyOptionsCollection.ContainsKey(key)) { var ex = new FreeSqlException($"库 {key} 不存在."); if (!Options.UseIdleBus) { Notice(null, new IdleBus <TKey, IFreeSql> .NoticeEventArgs( IdleBus <TKey, IFreeSql> .NoticeType.Get, key, ex, $"{key} 实例获取失败.")); } throw ex; } if (Options.UseIdleBus ? !IdleBus.Exists(key) : !Dic.ContainsKey(key)) { Register(key); } }
/// <summary> /// 判断临时任务是否存在 /// </summary> /// <param name="id"></param> /// <returns></returns> public bool ExistsTempTask(string id) => _tasks.ContainsKey(id) == false && _ib.Exists(id);