Ejemplo n.º 1
0
        protected virtual void OnMessage(RedisChannel channel, RedisValue redisValue)
        {
            var message = JsonConvert.DeserializeObject <RedisCachingMessage>(redisValue);

            if (!string.IsNullOrEmpty(message.InstanceId) && !message.InstanceId.EqualsInvariant(_instanceId))
            {
                _log.LogTrace($"Received message {message.ToString()}");

                foreach (var key in message.CacheKeys?.OfType <string>() ?? Array.Empty <string>())
                {
                    if (message.IsToken)
                    {
                        _log.LogTrace($"Trying to cancel token with key: {key}");
                        CancellableCacheRegion.CancelForKey(key, propagate: false);
                    }
                    else
                    {
                        _log.LogTrace($"Trying to remove cache entry with key: {key} from in-memory cache");
                        base.Remove(key);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        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);
        }