public static int Save() { LastSaveException = null; int count = 0; List <AccountLimitsChecker> checkers; lock (LockObject) { checkers = new List <AccountLimitsChecker>(Checkers.Values); } foreach (var checker in checkers) { try { using (var context = AccountDbContext.CreateFromAccountIdLocalCache(checker.AccountId)) { count += checker.SaveData(context); } } catch (Exception exception) { LastSaveException = exception; } } return(count); }
public void Add(Guid accountId, List <TCacheObject> cacheObjects, Func <TCacheObject, TEntity> getEntity) { if (cacheObjects == null) { throw new ArgumentNullException("cacheObjects"); } if (cacheObjects.Count == 0) { return; } if (accountId == Guid.Empty) { throw new Exception("accountId == Guid.Empty"); } using (var accountDbContext = AccountDbContext.CreateFromAccountIdLocalCache(accountId)) { foreach (var cacheObject in cacheObjects) { var dbEntity = getEntity(cacheObject); if (dbEntity == null) { throw new Exception("dbEntity == null"); } var entry = accountDbContext.Entry(dbEntity); entry.State = EntityState.Added; } accountDbContext.SaveChanges(); } }
public Guid GetOrCreateTypeId(Guid accountId, string name) { if (name == null) { throw new ArgumentNullException("name"); } var key = GetMapKey(accountId, name); Guid id; if (NameToIdMap.TryGetValue(key, out id)) { return(id); } lock (UpdateMapLock) { if (NameToIdMap.TryGetValue(key, out id)) { return(id); } using (var accountDbContext = AccountDbContext.CreateFromAccountIdLocalCache(accountId)) { var repository = accountDbContext.GetMetricTypeRepository(); var metricType = repository.GetOneOrNullByName(name); if (metricType == null /* || metricType.IsDeleted*/) // репозиторий не возвращает удалённые { // Проверим лимиты var limitChecker = AccountLimitsCheckerManager.GetCheckerForAccount(accountId); var checkResult = limitChecker.CheckMaxMetrics(accountDbContext); if (!checkResult.Success) { throw new OverLimitException(checkResult.Message); } metricType = new MetricType() { Id = Guid.NewGuid(), IsDeleted = false, CreateDate = DateTime.Now, SystemName = name, DisplayName = name }; repository.Add(metricType); accountDbContext.SaveChanges(); limitChecker.RefreshMetricsCount(); } bool success = NameToIdMap.TryAdd(key, metricType.Id); if (success) { return(metricType.Id); } throw new Exception("Не удалось добавить тип метрики в карту"); } } }
protected override TWriteObject LoadObject(AccountCacheRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (request.AccountId == Guid.Empty) { throw new Exception("request.AccountId == Guid.Empty"); } using (var accountDbContext = AccountDbContext.CreateFromAccountIdLocalCache(request.AccountId)) { return(LoadObject(request, accountDbContext)); } }
protected virtual void UpdateBatch(Guid accountId, List <TWriteObject> cacheObjects, bool useCheck) { if (cacheObjects == null) { throw new ArgumentNullException("cacheObjects"); } if (cacheObjects.Count == 0) { return; } if (accountId == Guid.Empty) { throw new Exception("accountId == Guid.Empty"); } var batchTimer = new Stopwatch(); batchTimer.Start(); using (var accountDbContext = AccountDbContext.CreateFromAccountIdLocalCache(accountId)) { // перенесено в MyDataContext // storageDbContext.Configuration.AutoDetectChangesEnabled = false; accountDbContext.Configuration.ValidateOnSaveEnabled = false; foreach (var cacheObject in cacheObjects) { UpdateBatchObject(accountDbContext, cacheObject, useCheck); } var saveTimer = new Stopwatch(); saveTimer.Start(); accountDbContext.Database.ExecuteSqlCommand("SELECT 'cache-update-batch-begin'"); // перенесено в MyDataContext // storageDbContext.Configuration.AutoDetectChangesEnabled = true; accountDbContext.SaveChanges(); accountDbContext.Database.ExecuteSqlCommand("SELECT 'cache-update-batch-end'"); saveTimer.Stop(); batchTimer.Stop(); ComponentControl.Log.Debug("UpdateBatch " + cacheObjects.Count + " штук за " + (int)batchTimer.ElapsedMilliseconds + " мс / SaveChanges за " + (int)saveTimer.ElapsedMilliseconds + " мс"); } }
public void Update( Guid accountId, List <TCacheObject> cacheObjects, Func <TCacheObject, TEntity> getCurrentEntity, Func <TCacheObject, TEntity> getLastSavedEntity) { if (cacheObjects == null) { throw new ArgumentNullException("cacheObjects"); } if (cacheObjects.Count == 0) { return; } if (accountId == Guid.Empty) { throw new Exception("accountId == Guid.Empty"); } using (var accountDbContext = AccountDbContext.CreateFromAccountIdLocalCache(accountId)) { foreach (var cacheObject in cacheObjects) { var lastSavedEntity = getLastSavedEntity(cacheObject); if (lastSavedEntity == null) { throw new Exception("lastSavedEntity == null"); } var currentEntity = getCurrentEntity(cacheObject); if (currentEntity == null) { throw new Exception("currentEntity == null"); } var entry = accountDbContext.Entry(lastSavedEntity); entry.CurrentValues.SetValues(currentEntity); } accountDbContext.SaveChanges(); } }
protected virtual void UpdateBatch(Guid accountId, List <TWriteObject> cacheObjects, bool useCheck) { if (cacheObjects == null) { throw new ArgumentNullException("cacheObjects"); } if (cacheObjects.Count == 0) { return; } if (accountId == Guid.Empty) { throw new Exception("accountId == Guid.Empty"); } using (var accountDbContext = AccountDbContext.CreateFromAccountIdLocalCache(accountId)) { foreach (var cacheObject in cacheObjects) { UpdateBatchObject(accountDbContext, cacheObject, useCheck); } accountDbContext.SaveChanges(); } }
public IUnitTestTypeCacheReadObject FindByName(Guid accountId, string name) { var key = GetMapKey(accountId, name); Guid unitTestTypeId = Guid.Empty; lock (UpdateMapLock) { if (NameToIdMap.TryGetValue(key, out unitTestTypeId)) { return(Read(new AccountCacheRequest() { AccountId = accountId, ObjectId = unitTestTypeId })); } } using (var accountDbContext = AccountDbContext.CreateFromAccountIdLocalCache(accountId)) { var repository = accountDbContext.GetUnitTestTypeRepository(); var unitTestType = repository.GetOneOrNullBySystemName(name); if (unitTestType != null) { unitTestTypeId = unitTestType.Id; lock (UpdateMapLock) { NameToIdMap.TryAdd(key, unitTestTypeId); } return(Read(new AccountCacheRequest() { AccountId = accountId, ObjectId = unitTestTypeId })); } } return(null); }