Пример #1
0
        public async Task Send_Msg_Async_Throw_Exception_Should_Not_Break()
        {
            CancellationToken token = new CancellationToken();

            A.CallTo(() => fakeBus.PublishAsync("test_topic", A <EasyCachingMessage> ._, token)).ThrowsAsync((arg) => new Exception());

            await fakeHybrid.RemoveAsync("fake-remove");

            Assert.True(true);
        }
        public async Task Send_Msg_Async_Throw_Exception_Should_Not_Break()
        {
            var token = new CancellationToken();

            var(hybridProvider, _, _, _) = CreateCachingProviderWithFakes(
                setupFakeBus: bus =>
                A.CallTo(() => bus.PublishAsync("test_topic", A <EasyCachingMessage> ._, token)).ThrowsAsync(new InvalidOperationException()));

            await hybridProvider.RemoveAsync("fake-remove");

            Assert.True(true);
        }
Пример #3
0
        public async Task SetAsync_And_RemoveAsync_Should_Succeed()
        {
            var cacheKey = $"{_namespace}_{Guid.NewGuid().ToString()}";

            await hybridCaching_1.SetAsync(cacheKey, "val", TimeSpan.FromSeconds(30));

            await hybridCaching_1.RemoveAsync(cacheKey);

            var res = await hybridCaching_1.ExistsAsync(cacheKey);

            Assert.False(res);
        }
Пример #4
0
        /// <summary>
        /// Processes the evict async.
        /// </summary>
        /// <returns>The evict async.</returns>
        /// <param name="context">Context.</param>
        /// <param name="isBefore">If set to <c>true</c> is before.</param>
        private async Task ProcessEvictAsync(AspectContext context, bool isBefore)
        {
            if (GetMethodAttributes(context.ServiceMethod).FirstOrDefault(x => typeof(EasyCachingEvictAttribute).IsAssignableFrom(x.GetType())) is EasyCachingEvictAttribute attribute && attribute.IsBefore == isBefore)
            {
                try
                {
                    if (attribute.IsAll)
                    {
                        // If is all , clear all cached items which cachekey start with the prefix.
                        var cachePrefix = KeyGenerator.GetCacheKeyPrefix(context.ServiceMethod, attribute.CacheKeyPrefix);

                        if (attribute.IsHybridProvider)
                        {
                            await HybridCachingProvider.RemoveByPrefixAsync(cachePrefix);
                        }
                        else
                        {
                            var _cacheProvider = CacheProviderFactory.GetCachingProvider(attribute.CacheProviderName ?? Options.Value.CacheProviderName);
                            await _cacheProvider.RemoveByPrefixAsync(cachePrefix);
                        }
                    }
                    else
                    {
                        // If not all , just remove the cached item by its cachekey.
                        var cacheKey = KeyGenerator.GetCacheKey(context.ServiceMethod, context.Parameters, attribute.CacheKeyPrefix);

                        if (attribute.IsHybridProvider)
                        {
                            await HybridCachingProvider.RemoveAsync(cacheKey);
                        }
                        else
                        {
                            var _cacheProvider = CacheProviderFactory.GetCachingProvider(attribute.CacheProviderName ?? Options.Value.CacheProviderName);
                            await _cacheProvider.RemoveAsync(cacheKey);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (!attribute.IsHighAvailability)
                    {
                        throw;
                    }
                    else
                    {
                        Logger?.LogError(new EventId(), ex, $"Cache provider remove error.");
                    }
                }
            }
        }