public void ExpiredGlobalRegionTokenRemovesAll() { //Arrange var cache = GetPlatformMemoryCache(); var key1 = "myKey1"; var key2 = "myKey2"; var value1 = new object(); var value2 = new object(); var token1 = CacheRegion.CreateChangeTokenForKey(key1); var token2 = CacheRegion2.CreateChangeTokenForKey(key2); cache.Set(key1, value1, token1); cache.Set(key2, value2, token2); //Act GlobalCacheRegion.ExpireRegion(); //Assertion var result = cache.Get(key1); Assert.Null(result); result = cache.Get(key2); Assert.Null(result); }
protected virtual void OnConnectionRestored(object sender, ConnectionFailedEventArgs e) { // Return cache to the same state as it was initially. // Don't set directly true because it may be disabled in app settings CacheEnabled = _storefrontOptions.CacheEnabled; // We should fully clear cache because we don't know // what's changed in another instances since Redis became unavailable GlobalCacheRegion.ExpireRegion(); }
protected virtual void OnConnectionFailed(object sender, ConnectionFailedEventArgs e) { // If we have no connection to Redis, we can't invalidate cache on another platform instances, // so the better idea is to disable cache at all for data consistence CacheEnabled = false; // We should fully clear cache because we don't know // what's changed until platform found Redis is unavailable GlobalCacheRegion.ExpireRegion(); }
protected virtual void OnConnectionFailed(object sender, ConnectionFailedEventArgs e) { _log.LogError($"Redis disconnected from instance { _instanceId }. Endpoint is {e.EndPoint}, failure type is {e.FailureType}"); // If we have no connection to Redis, we can't invalidate cache on another platform instances, // so the better idea is to disable cache at all for data consistence CacheEnabled = false; // We should fully clear cache because we don't know // what's changed until platform found Redis is unavailable GlobalCacheRegion.ExpireRegion(); }
public void Global_Cache_Region_Expired_Successfully() { var platformMemoryCache = GetPlatformMemoryCache(); var firstValue = GetSampleValueWithCache(platformMemoryCache); var secondValue = GetSampleValueWithCache(platformMemoryCache); GlobalCacheRegion.ExpireRegion(); var thirdValue = GetSampleValueWithCache(platformMemoryCache); Assert.Equal(firstValue, secondValue); Assert.NotEqual(firstValue, thirdValue); }
public virtual ICacheEntry CreateEntry(object key) { var result = _memoryCache.CreateEntry(key); if (result != null) { result.RegisterPostEvictionCallback(callback: EvictionCallback); var options = GetDefaultCacheEntryOptions(); //Add GlobalCache token for each entry options.AddExpirationToken(GlobalCacheRegion.CreateChangeToken()); result.SetOptions(options); } return(result); }
protected virtual void OnConnectionRestored(object sender, ConnectionFailedEventArgs e) { _log.LogInformation($"Redis connection restored for instance { ServerId }"); _telemetryClient.TrackEvent("RedisConnectionRestored", new Dictionary <string, string> { { "channelName", _redisCachingOptions.ChannelName }, { "cacheId", ServerId } }); // Return cache to the same state as it was initially. // Don't set directly true because it may be disabled in app settings CacheEnabled = _cachingOptions.CacheEnabled; // We should fully clear cache because we don't know // what's changed in another instances since Redis became unavailable GlobalCacheRegion.ExpireRegion(); }
protected virtual void OnConnectionFailed(object sender, ConnectionFailedEventArgs e) { _log.LogError($"Redis disconnected from instance { ServerId }. Endpoint is {e.EndPoint}, failure type is {e.FailureType}"); _telemetryClient.TrackException(e.Exception); _telemetryClient.TrackEvent("RedisDisconnected", new Dictionary <string, string> { { "channelName", _redisCachingOptions.ChannelName }, { "cacheId", ServerId }, { "endpoint", e.EndPoint.ToString() }, { "failureType", e.FailureType.ToString() } }); // If we have no connection to Redis, we can't invalidate cache on another platform instances, // so the better idea is to disable cache at all for data consistence CacheEnabled = false; // We should fully clear cache because we don't know // what's changed until platform found Redis is unavailable GlobalCacheRegion.ExpireRegion(); }
public MemoryCacheEntryOptions GetDefaultCacheEntryOptions() { var result = new MemoryCacheEntryOptions(); if (!CacheEnabled) { result.AbsoluteExpirationRelativeToNow = TimeSpan.FromTicks(1); } else { if (AbsoluteExpiration != null) { result.AbsoluteExpirationRelativeToNow = AbsoluteExpiration; } else if (SlidingExpiration != null) { result.SlidingExpiration = SlidingExpiration; } result.AddExpirationToken(GlobalCacheRegion.CreateChangeToken()); } return(result); }
public void CreateChangeTokenReturnsCompositeToken() { //Act var token = CacheRegion.CreateChangeTokenForKey("key"); //Assertion Assert.IsType <CompositeChangeToken>(token); //Tokens for item, region and global Assert.Equal(3, ((CompositeChangeToken)token).ChangeTokens.Count); Assert.All(((CompositeChangeToken)token).ChangeTokens, x => Assert.IsType <LazyCancellationChangeToken>(x)); //Act token = CacheRegion.CreateChangeToken(); //Assertion Assert.IsType <CompositeChangeToken>(token); //Tokens for item, region and global Assert.Equal(2, ((CompositeChangeToken)token).ChangeTokens.Count); Assert.All(((CompositeChangeToken)token).ChangeTokens, x => Assert.IsType <LazyCancellationChangeToken>(x)); //Act token = GlobalCacheRegion.CreateChangeToken(); //Assertion Assert.IsType <LazyCancellationChangeToken>(token); }
public void CancelTokenForKeyOnTokenCancelledCallCounts(bool propagate, int callbackCount) { //Arrange var registeryCallbackInvokedCount = 0; CancellableCacheRegion.OnTokenCancelled = args => { Interlocked.Increment(ref registeryCallbackInvokedCount); }; var cache = GetPlatformMemoryCache(); var key = "myKey"; var value1 = new object(); var value2 = new object(); var key1 = CacheRegion.GenerateRegionTokenKey(key); var token1 = CacheRegion.CreateChangeTokenForKey(key); var key2 = CacheRegion2.GenerateRegionTokenKey(key); var token2 = CacheRegion2.CreateChangeTokenForKey(key); //Act cache.Set(key1, value1, token1); cache.Set(key2, value2, token2); //Assertion var result = cache.Get(key1); Assert.Same(value1, result); result = cache.Get(key2); Assert.Same(value2, result); //Act CancellableCacheRegion.CancelForKey(key1, propagate); //Assertion result = cache.Get(key1); Assert.Null(result); result = cache.Get(key2); Assert.Same(value2, result); //Act CancellableCacheRegion.CancelForKey(key2, propagate); //Assertion result = cache.Get(key2); Assert.Null(result); //Arrange token1 = CacheRegion.CreateChangeTokenForKey(key); token2 = CacheRegion2.CreateChangeTokenForKey(key); cache.Set(key1, value1, token1); cache.Set(key2, value2, token2); //Act //cancel region CancellableCacheRegion.CancelForKey(CacheRegion.GenerateRegionTokenKey(), propagate); //Assertion result = cache.Get(key1); Assert.Null(result); result = cache.Get(key2); Assert.Same(value2, result); //Arrange token1 = CacheRegion.CreateChangeTokenForKey(key); token2 = CacheRegion2.CreateChangeTokenForKey(key); cache.Set(key1, value1, token1); cache.Set(key2, value2, token2); //Act //cancel global region CancellableCacheRegion.CancelForKey(GlobalCacheRegion.GenerateRegionTokenKey(), propagate); result = cache.Get(key1); Assert.Null(result); result = cache.Get(key2); Assert.Null(result); //Callback mustn't call Assert.Equal(callbackCount, registeryCallbackInvokedCount); }
public ActionResult ResetCache() { GlobalCacheRegion.ExpireRegion(); return(StoreFrontRedirect("~/")); }
public ActionResult ResetPlatformCache() { GlobalCacheRegion.ExpireRegion(); return(NoContent()); }