private static void PerformanceCacheDbTest(SelectTestDefinition[] testDefinitions, int testCount) { var pool = new DbContextPool <OrderContext>(OrderContextOptions.Create(true, null)); Db.OeEntitySetAdapterCollection entitySetAdapters = new OrderDbDataAdapter(false, false, null).EntitySetAdapters; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); var stopWatch = new Stopwatch(); stopWatch.Start(); for (int i = 0; i < testCount; i++) { foreach (SelectTestDefinition testDefinition in testDefinitions) { OrderContext dbContext = pool.Rent(); testDefinition.ExecutorDb(entitySetAdapters, dbContext); pool.Return(dbContext); } } stopWatch.Stop(); Console.WriteLine("Entity Framework " + stopWatch.Elapsed); }
public virtual async Task CreateAsync(User user, bool orUpdate = false, CancellationToken cancellationToken = default) { using var lease = DbContextPool.Rent(); var dbContext = lease.Subject; var existingUser = (User?)null; var supportTransactions = !dbContext.Database.IsInMemory(); await using var tx = supportTransactions ? await dbContext.Database.BeginTransactionAsync(cancellationToken) : (IDbContextTransaction?)null; var userId = user.Id; if (orUpdate) { existingUser = await dbContext.Users.FindAsync(new [] { (object)userId }, cancellationToken); if (existingUser != null) { dbContext.Users.Update(user); } } if (existingUser == null) { dbContext.Users.Add(user); } await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); await(tx?.CommitAsync(cancellationToken) ?? Task.CompletedTask); Invalidate(user, existingUser == null); }
public ShowSlideProcessor(IShowSlideQueueClient queueClient, ILogger <ShowSlideProcessor> logger, DbContextPool <ShowContext> contextPool, RedisPublisher redis) { _queueClient = queueClient; _logger = logger; _contextPool = contextPool; _redis = redis; }
/// <summary> /// 获取实体池 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <returns></returns> public static DbContextPool <T> GetDbContextPool <T>() where T : DbContext, new() { string fullname = typeof(T).FullName; string connName = ""; string lastName = fullname.Split('.').Last(); foreach (ConnectionStringSettings item in System.Configuration.ConfigurationManager.ConnectionStrings) { if (lastName.StartsWith(item.Name)) { connName = item.Name; break; } } if (string.IsNullOrEmpty(connName)) { throw new Exception(string.Format("配置文件中不包含{0}的链接字符串", fullname)); } lock (l) { if (_DbContextPool.ContainsKey(connName)) { return((DbContextPool <T>)_DbContextPool[connName]); } else { var pool = new DbContextPool <T>(connName); _DbContextPool.Add(connName, pool); return(pool); } } }
public DatabaseHouseKeeper(Storage storage, RequestLimiter requestLimiter, IAPIProvider apiProvider, IBeatmapSearchEngineProvider searchEngine, BeatmapDownloader beatmapDownloader, DbContextPool <PisstaubeDbContext> dbContextPool) : base(storage, requestLimiter, apiProvider, searchEngine, beatmapDownloader, dbContextPool) { _requestLimiter = requestLimiter; _apiProvider = apiProvider; _dbContextPool = dbContextPool; _searchEngine = searchEngine; }
protected ProviderSpecificSchema(DbContextOptions <Types.DynamicDbContext> dynamicDbContextOptions, DbContextPool <SchemaContext> schemaContextPool) { DynamicDbContextOptions = dynamicDbContextOptions; SchemaContextPool = schemaContextPool; OperationAdapter = new OeEfCoreOperationAdapter(typeof(Types.DynamicDbContext)); }
public OeEfCoreDataAdapter(DbContextOptions options, Db.OeQueryCache queryCache, OeEfCoreOperationAdapter operationAdapter) : base(queryCache, operationAdapter) { if (options != null) { _dbContextPool = new DbContextPool <T>(options); } }
public UserService( DbContextPool <TestDbContext> dbContextPool, ILogger <UserService>?log = null) { _log = log ??= NullLogger <UserService> .Instance; DbContextPool = dbContextPool; IsCaching = GetType().Name.EndsWith("Proxy"); }
public virtual async Task UpdateAsync(User user, CancellationToken cancellationToken = default) { await using var dbContext = DbContextPool.Rent(); dbContext.Users.Update(user); await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); Invalidate(user, false); }
public OeEfCoreDataAdapter(DbContextOptions?options, Cache.OeQueryCache?queryCache, OeEfCoreOperationAdapter operationAdapter) : base(queryCache, operationAdapter) { if (options != null) { _dbContextPool = new DbContextPool <T>(Fix.FixHelper.FixDistinctCount(options)); } }
public BlogFactory( DbContextPool <BlogDbContext> context, IHttpContextAccessor accessor, ISystemClock clock) { this._accessor = accessor; this._context = context; this._clock = clock; }
public virtual async Task <long> CountAsync(CancellationToken cancellationToken = default) { await Everything().ConfigureAwait(false); await using var dbContext = DbContextPool.Rent(); var count = await dbContext.Users.LongCountAsync(cancellationToken).ConfigureAwait(false); // _log.LogDebug($"Users.Count query: {count}"); return(count); }
public OsuCrawler(Storage storage, RequestLimiter requestLimiter, IAPIProvider apiProvider, IBeatmapSearchEngineProvider searchEngine, BeatmapDownloader beatmapDownloader, DbContextPool <PisstaubeDbContext> dbContextPool) { _storage = storage; _beatmapDownloader = beatmapDownloader; _dbContextPool = dbContextPool; SearchEngine = searchEngine; ApiProvider = apiProvider; RequestLimiter = requestLimiter; }
public virtual async Task <User?> TryGetAsync(long userId, CancellationToken cancellationToken = default) { // Debug.WriteLine($"TryGetAsync {userId}"); await Everything().ConfigureAwait(false); await using var dbContext = DbContextPool.Rent(); var user = await dbContext.Users .FindAsync(new[] { (object)userId }, cancellationToken) .ConfigureAwait(false); return(user); }
public virtual async Task <bool> DeleteAsync(User user, CancellationToken cancellationToken = default) { Computed.GetCurrent().Should().BeNull(); await using var dbContext = DbContextPool.Rent(); dbContext.Users.Remove(user); try { await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); Invalidate(user); return(true); } catch (DbUpdateConcurrencyException) { return(false); } }
public async Task <string> ToOsuString(DbContextPool <SoraDbContext> ctxPool) { var returnString = string.Empty; if (_bm == null) { Logger.Err("Failed to set Beatmap! Beatmap is null!"); return($"{(int) RankedStatus.NeedUpdate}|false\n"); } var count = 0; if (_ownScore != null) { var ctx = ctxPool.Rent(); try { returnString += $"{_ownScore?.ToOsuString(ctx)}\n"; } finally { ctxPool.Return(ctx); } } await foreach (var score in _scores) { count++; var ctx = ctxPool.Rent(); try { returnString += $"{score.ToOsuString(ctx)}\n"; } finally { ctxPool.Return(ctx); } } return(ScoreboardHeader(count) + returnString); }
public PersistedGrantRepository(DbContextPool <PersistedGrantDbContext> pool) : base(pool) { }
private void Client_OPCClientDataChangeEvent(object source, OPCClientDataChangeEventArgs e) { if (GlobalConfig.Global.PersistenceType == PersistenceType.Xml) { OnServiceLog("OPC读取数据不支持XML方式持久化"); return; } if (_config == null) { OnServiceLog("配置信息为空"); return; } List <ITag> tags = ConvertTagData(e); //CrossServerCache.TagCache.AddOrUpdateRange(tags); if (_config.OPCClientPersistence) { PersistenceType pt = GlobalConfig.Global.PersistenceType; if (pt == PersistenceType.CoreRT //|| pt == PersistenceType.Golden || pt == PersistenceType.eDNA) { #region 写入实时数据库 IDbContext rdb = null; try { rdb = DbContextPool.Pop(); if (rdb != null) { string tableName = "SSIOOpc"; rdb.WriteTags(tableName, e.TimeStamps, e.ItemNames, e.ItemValues); } } catch (Exception ex) { OnServiceLog(ex.Message); } finally { if (rdb != null) { DbContextPool.Push(rdb); } } OnServiceLog("OPC Client>>写入实时数据库操作完成。共:" + e.NumItems.ToString() + " 数据点。"); #endregion } else if (pt == PersistenceType.MySql || pt == PersistenceType.Oracle || pt == PersistenceType.SqlServer || pt == PersistenceType.Sqlite) { #region 写到关系数据库 if (_persistence == null) { _persistence = DataPersistenceFactory.CreateDataPersistence(pt); } ((BaseSqlPersistence)_persistence).PersistenceData("", tags); OnServiceLog("OPC Client>>写入关系数据库操作完成。共:" + e.NumItems.ToString() + " 数据点。"); #endregion } } else { string context = "OPC Client>>"; for (int i = 0; i < e.NumItems; i++) { context += String.Format("序号:{0},时间:{1},标签:{2},值:{3};", i.ToString(), e.TimeStamps[i].ToString("yyyy-MM-dd HH:mm:ss"), e.ItemNames[i], e.ItemValues[i].ToString()); } OnServiceLog(context); } }
private static void Main(string[] args) { var startup = new Startup(); DbContextPool <PisstaubeDbContext>?dbContextPool = null; var builder = new ContainerBuilder(); startup.ConfigureServices(builder); builder.Register(c => { var pool = c.Resolve <DbContextPool <PisstaubeDbContext> >(); var context = pool.Rent(); return(context); }).As <PisstaubeDbContext>() .OnRelease(c => { dbContextPool?.Return(c); }); builder.Register(c => { var contextPool = c.Resolve <DbContextPool <PisstaubeDbContext> >(); dbContextPool = contextPool; var apiProvider = c.Resolve <IAPIProvider>(); var cacheDbContextFactory = c.Resolve <PisstaubeCacheDbContextFactory>(); var searchEngine = c.Resolve <IBeatmapSearchEngineProvider>(); var dbContext = c.Resolve <PisstaubeDbContext>(); startup.Configure(apiProvider, cacheDbContextFactory, searchEngine, dbContext); return(startup); }).AutoActivate(); var container = builder.Build(); var osuCrawler = container.Resolve <OsuCrawler>(); var houseKeeper = container.Resolve <DatabaseHouseKeeper>(); var redis = container.Resolve <ConnectionMultiplexer>(); var setDownloader = container.Resolve <SetDownloader>(); if (Environment.GetEnvironmentVariable("CRAWLER_DISABLED")?.ToLower() != "true") { osuCrawler.Start(); } if (Environment.GetEnvironmentVariable("UPDATER_DISABLED")?.ToLower() != "true") { houseKeeper.Start(); } var db = redis.GetDatabase(int.Parse(Environment.GetEnvironmentVariable("REDIS_DATABASE") ?? "-1")); var sub = redis.GetSubscriber(); sub.Subscribe($"chimu:downloads", (channel, value) => { Logger.LogPrint("Download was requested..."); Logger.LogPrint(channel); Logger.LogPrint(value); DownloadMapRequest request = null; try { request = JsonUtil.Deserialize <DownloadMapRequest>(value); } catch (Exception e) { Logger.Error(e, "Failed to download Beatmap"); } try { var response = setDownloader.DownloadMap(int.Parse(request.SetId), request.NoVideo == "0"); response._ID = request._ID; db.Publish("chimu:s:downloads", JsonUtil.Serialize(response)); Logger.LogPrint("Success"); } catch (Exception ex) { Logger.Error(ex, "Failed to download Beatmap"); } }); while (true) { Thread.Sleep(1000); } }
public void ConfigureServices(ContainerBuilder builder) { var dbOptionsBuilder = new DbContextOptionsBuilder(); { var host = Environment.GetEnvironmentVariable("MARIADB_HOST"); var port = Environment.GetEnvironmentVariable("MARIADB_PORT"); var username = Environment.GetEnvironmentVariable("MARIADB_USERNAME"); var password = Environment.GetEnvironmentVariable("MARIADB_PASSWORD"); var db = Environment.GetEnvironmentVariable("MARIADB_DATABASE"); dbOptionsBuilder.UseMySql( $"Server={host};Database={db};User={username};Password={password};Port={port};CharSet=utf8mb4;SslMode=none;", mysqlOptions => { mysqlOptions.ServerVersion(new Version(10, 4, 12), ServerType.MariaDb); mysqlOptions.CharSet(CharSet.Utf8Mb4); } ); } var pool = new DbContextPool <PisstaubeDbContext>(dbOptionsBuilder.Options); builder.RegisterInstance(pool).AsSelf(); var redisOptions = new RedisCacheOptions(); { var host = Environment.GetEnvironmentVariable("REDIS_HOST"); var port = Environment.GetEnvironmentVariable("REDIS_PORT"); var pass = Environment.GetEnvironmentVariable("REDIS_PASSWORD"); var database = Environment.GetEnvironmentVariable("REDIS_DATABASE"); var connString = $"{host}"; if (!string.IsNullOrEmpty(port)) { connString += $":{port}"; } if (!string.IsNullOrEmpty(pass)) { connString += $",password={pass}"; } if (!string.IsNullOrEmpty(database)) { connString += $",defaultDatabase={database}"; } redisOptions.Configuration = connString; } var redisCache = new RedisCache(redisOptions); builder.RegisterInstance(redisCache).As <IDistributedCache>(); builder.RegisterInstance(ConnectionMultiplexer.Connect(redisOptions.Configuration)); builder.RegisterType <IpfsCache>().SingleInstance(); builder.RegisterInstance(new RequestLimiter(1200, TimeSpan.FromMinutes(1))); builder.RegisterInstance(_dataStorage).As <Storage>(); builder.RegisterInstance(_osuContextFactory).As <IDatabaseContextFactory>(); builder.RegisterType <FileStore>(); builder.RegisterType <PisstaubeCacheDbContextFactory>().AsSelf(); builder.RegisterType <SetDownloader>().AsSelf(); builder.RegisterType <MeiliBeatmapSearchEngine>().As <IBeatmapSearchEngineProvider>(); builder.RegisterType <BeatmapDownloader>(); builder.RegisterType <OsuConfigManager>(); builder.RegisterType <APIAccess>().As <IAPIProvider>().SingleInstance(); builder.RegisterType <OsuCrawler>(); builder.RegisterType <DatabaseHouseKeeper>(); }
public UserService(DbContextPool <TestDbContext> dbContextPool) { DbContextPool = dbContextPool; IsCaching = GetType().Name.EndsWith("Proxy"); }
protected ProviderSpecificSchema(DbContextOptions <DynamicDbContext> dynamicDbContextOptions, DbContextPool <SchemaContext> schemaContextPool) { DynamicDbContextOptions = dynamicDbContextOptions; _schemaContextPool = schemaContextPool; }
/// <summary> /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// </summary> public PooledDbContextFactory(DbContextPool <TContext> pool) => _pool = pool;
public SpeakerByIdDataLoader(DbContextPool <ApplicationDbContext> dbContextPool) { _dbContextPool = dbContextPool ?? throw new ArgumentNullException(nameof(dbContextPool)); }
public ShowStartProcessor(IShowStartQueueClient client, ILogger <ShowStartProcessor> logger, DbContextPool <ShowContext> contextPool) { _client = client; _logger = logger; _contextPool = contextPool; }
public SessionByAttendeeIdDataLoader(DbContextPool <ApplicationDbContext> dbContextPool) { _dbContextPool = dbContextPool ?? throw new ArgumentNullException(nameof(dbContextPool)); }
public DbDynamicMetadataProvider(DbContextOptions options) { _dbContextPool = new DbContextPool <SchemaContext>(options); }
public ConfigurationRepository(DbContextPool <ConfigurationDbContext> pool) : base(pool) { }
public TagsByCardIdDataLoader(DbContextPool <AppDbContext> dbPool) { _dbPool = dbPool ?? throw new ArgumentNullException(nameof(dbPool)); }
public DatabaseRepository(DbContextPool <TContext> pool) { _pool = pool; }