public void TestCacheInvalidationWithTwoRoots() { _cacheService.StoreRootCacheKeys(new[] { "entity1.model", "entity2.model" }); _cacheService.InsertValue("EF_key1", "value1", new[] { "entity1.model", "entity2.model" }, DateTime.Now.AddMinutes(10)); _cacheService.StoreRootCacheKeys(new[] { "entity1.model", "entity2.model" }); _cacheService.InsertValue("EF_key2", "value2", new[] { "entity1.model", "entity2.model" }, DateTime.Now.AddMinutes(10)); var value1 = _cacheService.GetValue("EF_key1"); Assert.IsNotNull(value1); var value2 = _cacheService.GetValue("EF_key2"); Assert.IsNotNull(value2); _cacheService.InvalidateCacheDependencies(new[] { "entity2.model" }); value1 = _cacheService.GetValue("EF_key1"); Assert.IsNull(value1); value2 = _cacheService.GetValue("EF_key2"); Assert.IsNull(value2); var keys = _cacheService.GetAllEFCachedKeys(); var key1 = keys.FirstOrDefault(key => key == "EF_key1"); Assert.IsNull(key1); Assert.AreEqual(0, keys.Count); }
public void TestCacheInvalidationWithTwoRoots() { _cacheService.InsertValue("EF_key1", "value1", new HashSet <string> { "entity1.model", "entity2.model" }); _cacheService.InsertValue("EF_key2", "value2", new HashSet <string> { "entity1.model", "entity2.model" }); var value1 = _cacheService.GetValue("EF_key1"); Assert.IsNotNull(value1); var value2 = _cacheService.GetValue("EF_key2"); Assert.IsNotNull(value2); _cacheService.InvalidateCacheDependencies(new[] { "entity2.model" }); value1 = _cacheService.GetValue("EF_key1"); Assert.IsNull(value1); value2 = _cacheService.GetValue("EF_key2"); Assert.IsNull(value2); }
public override int SaveChanges() { // ensure auto history this.EnsureAutoHistory(); var changedEntityNames = this.GetChangedEntityNames(); var result = base.SaveChanges(); _cacheServiceProvider.InvalidateCacheDependencies(changedEntityNames); return(result); }
public override int SaveChanges() { this.ChangeTracker.DetectChanges(); var changedEntityNames = this.GetChangedEntityNames(); this.ChangeTracker.AutoDetectChangesEnabled = false; // for performance reasons, to avoid calling DetectChanges() again. var result = base.SaveChanges(); this.ChangeTracker.AutoDetectChangesEnabled = true; _cacheServiceProvider.InvalidateCacheDependencies(changedEntityNames); return result; }
/// <summary> /// Overridden method, sets shadow property, and clears second-level cache /// </summary> /// <returns></returns> public override int SaveChanges() { #region for Second Level Cache! string[] changedEntityNames = this.GetChangedEntityNames(); _efCacheServiceProvider.InvalidateCacheDependencies(changedEntityNames); #endregion #region Shadow property changes // Detect changes this.ChangeTracker.DetectChanges(); // Search all new and changed flights var entries = this.ChangeTracker.Entries <Flight>() .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified); if (!String.IsNullOrEmpty(ShadowPropertyName)) { // set the Shadow State column "LastChange" for all of them foreach (var entry in entries) { entry.Property(ShadowPropertyName).CurrentValue = DateTime.Now; } } // Save changes (we do not need DetectChanges() to be called again!) this.ChangeTracker.AutoDetectChangesEnabled = false; var result = base.SaveChanges(); // Call base class now this.ChangeTracker.AutoDetectChangesEnabled = true; return(result); #endregion }
/// <summary> /// Invalidates all of the cache entries which are dependent on any of the specified root keys. /// </summary> public bool InvalidateCacheDependencies(DbCommand command, DbContext context, EFCachePolicy cachePolicy) { if (command == null) { throw new ArgumentNullException(nameof(command)); } var commandText = command.CommandText; if (!_sqlCommandsProcessor.IsCrudCommand(commandText)) { return(false); } if (shouldSkipCacheInvalidationCommands(commandText)) { return(false); } var cacheDependencies = GetCacheDependencies(command, context, cachePolicy); cacheDependencies.Add(EFCachePolicy.EFUnknownsCacheDependency); _cacheServiceProvider.InvalidateCacheDependencies(new EFCacheKey(cacheDependencies)); _logger.LogDebug(CacheableEventId.QueryResultInvalidated, $"Invalidated [{string.Join(", ", cacheDependencies)}] dependencies."); return(true); }
/// <summary> /// 重写保存更改方法,清除缓存 /// </summary> /// <returns></returns> public override int SaveChanges() { this.ChangeTracker.DetectChanges(); var changedEntityNames = this.GetChangedEntityNames(); var result = base.SaveChanges(); _cacheServiceProvider.InvalidateCacheDependencies(changedEntityNames); return(result); }
public override int SaveChanges() { ChangeTracker.DetectChanges(); var changedEntityNames = cacheProvider != null ? this.GetChangedEntityNames() : new string[0]; var result = base.SaveChanges(); cacheProvider?.InvalidateCacheDependencies(changedEntityNames); return result; }
private async Task <int> SaveChangesAndCleanCache(IEFCacheServiceProvider efCacheServiceProvider, CancellationToken cancellationToken) { var changedEntityNames = this.GetChangedEntityNames() .Where(x => !x.Contains("SeedWork")) .ToArray(); ChangeTracker.AutoDetectChangesEnabled = false; // for performance reasons, to avoid calling DetectChanges() again. var result = await base.SaveChangesAsync(cancellationToken); ChangeTracker.AutoDetectChangesEnabled = true; efCacheServiceProvider.InvalidateCacheDependencies(changedEntityNames); return(result); }
/// <summary> /// Invalidates all of the cache entries which are dependent on any of the specified root keys. /// </summary> public bool InvalidateCacheDependencies(DbCommand command, DbContext context, EFCachePolicy cachePolicy) { var commandText = command.CommandText; if (!IsCrudCommand(commandText)) { return(false); } var cacheDependencies = GetCacheDependencies(command, context, cachePolicy); cacheDependencies.Add(EFCachePolicy.EFUnknownsCacheDependency); _cacheServiceProvider.InvalidateCacheDependencies(new EFCacheKey { CacheDependencies = cacheDependencies }); _logger.LogInformation(CacheableEventId.QueryResultInvalidated, $"Invalidated [{string.Join(", ", cacheDependencies)}] dependencies."); return(true); }
/// <summary> /// Invalidates all of the cache entries which are dependent on any of the specified root keys. /// </summary> public bool InvalidateCacheDependencies(string commandText, EFCacheKey cacheKey) { if (cacheKey is null) { throw new ArgumentNullException(nameof(cacheKey)); } if (!_sqlCommandsProcessor.IsCrudCommand(commandText)) { _logger.LogDebug($"Skipped invalidating a none-CRUD command[{commandText}]."); return(false); } if (shouldSkipCacheInvalidationCommands(commandText)) { _logger.LogDebug($"Skipped invalidating the related cache entries of this query[{commandText}] based on the provided predicate."); return(false); } cacheKey.CacheDependencies.Add($"{_cacheKeyPrefix}{EFCachePolicy.UnknownsCacheDependency}"); _cacheServiceProvider.InvalidateCacheDependencies(cacheKey); _logger.LogDebug(CacheableEventId.QueryResultInvalidated, $"Invalidated [{string.Join(", ", cacheKey.CacheDependencies)}] dependencies."); return(true); }