private CacheItem GetAutoReloadDataWithCacheInner <T>(
            string key,
            Func <T> dataFactory,
            Func <string> eTagFactory,
            TimeSpan timeToLive,
            TimeSpan dataUpdateDetectInternal,
            bool shouldReloadInBackground)
        {
            if (TryGetDataInner(key, out CacheItem cacheItem) &&
                shouldReloadInBackground)
            {
                var isUpdateProbobalyNeeded = cacheItem.LastETagCheckUtc.AddSafe(dataUpdateDetectInternal) < DateTimeOffset.UtcNow;
                if (isUpdateProbobalyNeeded)
                {
#pragma warning disable VSTHRD110
                    Task.Run(() =>
#pragma warning restore VSTHRD110
                    {
                        SetOrUpdateLazyData(key, cacheItem, LazyUtils.ToLazy(dataFactory), LazyUtils.ToLazy(eTagFactory), timeToLive, dataUpdateDetectInternal, shouldWaitForLock: false);
                    });
                }

                return(cacheItem);
            }

            return(SetOrUpdateLazyData(key, cacheItem, LazyUtils.ToLazy(dataFactory), LazyUtils.ToLazy(eTagFactory), timeToLive, dataUpdateDetectInternal, shouldWaitForLock: true));
        }
        public void TestToLazy_AsyncCollection()
        {
            var lazy = LazyUtils.ToLazy(async() => new List <MemoryStream> {
                new MemoryStream(), new MemoryStream(), new MemoryStream()
            });

            (lazy is IDisposable).Should().BeTrue();
        }
        public void TestToLazy_Collection()
        {
            var lazy = LazyUtils.ToLazy(() => new List <int> {
                1, 2, 3
            });

            (lazy is IDisposable).Should().BeTrue();
        }
Exemple #4
0
        public async Task TestCacheDelete_LazyCollectionDataShouldBeDisposedAsync()
        {
            ICacheInstance cache = MemoryCache.GetNamedInstance(nameof(TestCacheDelete_LazyDataShouldBeDisposedAsync));

            cache.CleanInternal = TimeSpan.FromSeconds(1);
            var data = LazyUtils.ToLazy(() => new[] { new DummyDisposable() });
            var key  = Guid.NewGuid().ToString();

            cache.SetData(key, data, TimeSpan.MaxValue);
            Console.WriteLine(data.Value.GetHashCode());
            cache.TryDeleteKey(key).Should().BeTrue();
            data.Value.ForEach(_ => _.IsDisposed.Should().Be(false));
            await Task.Delay(cache.CleanInternal.Add(TimeSpan.FromMilliseconds(500)));

            cache.CleanIfNeeded();
            data.Value.ForEach(_ => _.IsDisposed.Should().Be(true));
        }
Exemple #5
0
        public void EnsureValueDisposed_NotCreated()
        {
            var disposable = new DummyDisposable();

            disposable.IsDisposed.Should().BeFalse();

            var lazyAsync = LazyUtils.ToLazy(() => disposable);

            if (lazyAsync is IDisposable disposableLazyAsync)
            {
                disposableLazyAsync.Dispose();
                disposable.IsDisposed.Should().BeFalse();
            }
            else
            {
                Assert.Fail("lazy should be disposable");
            }
        }
Exemple #6
0
        public async Task AsyncLazy_EnsureValueDisposedAsync()
        {
            var disposable = new DummyDisposable();

            disposable.IsDisposed.Should().BeFalse();

            var lazyAsync = LazyUtils.ToAsyncLazy(async() => disposable);

            (await lazyAsync.GetValueAsync().ConfigureAwait(false)).Should().NotBeNull();
            if (lazyAsync is IDisposable disposableLazyAsync)
            {
                disposableLazyAsync.Dispose();
                disposable.IsDisposed.Should().BeTrue();
            }
            else
            {
                Assert.Fail("lazy should be disposable");
            }
        }
Exemple #7
0
        public async Task AsyncLazy_EnsureValueDisposed_CollectionAsync()
        {
            var disposables = new List <DummyDisposable> {
                new DummyDisposable(), new DummyDisposable()
            };

            disposables.ForEach(_ => _.IsDisposed.Should().BeFalse());

            var lazyAsync = LazyUtils.ToAsyncLazy(async() => disposables);

            (await lazyAsync.GetValueAsync().ConfigureAwait(false)).Should().NotBeNull();
            if (lazyAsync is IDisposable disposableLazyAsync)
            {
                disposableLazyAsync.Dispose();
                disposables.ForEach(_ => _.IsDisposed.Should().BeTrue());
            }
            else
            {
                Assert.Fail("lazy should be disposable");
            }
        }
Exemple #8
0
        public void EnsureValueDisposed_Collection()
        {
            var disposables = new List <DummyDisposable> {
                new DummyDisposable(), new DummyDisposable()
            };

            disposables.ForEach(_ => _.IsDisposed.Should().BeFalse());

            var lazyAsync = LazyUtils.ToLazy(() => disposables);

            lazyAsync.Value.Should().NotBeNull();
            if (lazyAsync is IDisposable disposableLazyAsync)
            {
                disposableLazyAsync.Dispose();
                disposables.ForEach(_ => _.IsDisposed.Should().BeTrue());
            }
            else
            {
                Assert.Fail("lazy should be disposable");
            }
        }
        public async Task HttpGetETagCacheTestsAsync()
        {
            ICacheInstance cache = MemoryCache.GetNamedInstance(Guid.NewGuid().ToString());

            using (var client = new HttpClient())
            {
                var baseUrl = $"https://httpbin.org/etag/";
                var etag    = Guid.NewGuid();

                int counter1 = 0;
                int counter2 = 0;

                var lazyResponse = LazyUtils.ToLazy(async() =>
                {
                    Interlocked.Increment(ref counter1);
                    var request = new HttpRequestMessage(HttpMethod.Get, $"{baseUrl}{etag}");
                    return(await client.SendAsync(request).ConfigureAwait(false));
                });
                cache.GetAutoReloadDataWithCache(
                    baseUrl,
                    async() =>
                {
                    Interlocked.Increment(ref counter2);
                    return(await lazyResponse.Value.Content.ReadAsStringAsync().ConfigureAwait(false));
                },
                    () => Task.FromResult(lazyResponse.Value.Headers.ETag?.Tag),
                    TimeSpan.FromHours(1),
                    TimeSpan.FromMilliseconds(100));
                counter1.Should().Be(1);
                counter2.Should().Be(1);

                lazyResponse = LazyUtils.ToLazy(async() =>
                {
                    Interlocked.Increment(ref counter1);
                    var request = new HttpRequestMessage(HttpMethod.Get, $"{baseUrl}{etag}");
                    return(await client.SendAsync(request).ConfigureAwait(false));
                });
                cache.GetAutoReloadDataWithCache(
                    baseUrl,
                    async() =>
                {
                    Interlocked.Increment(ref counter2);
                    return(await lazyResponse.Value.Content.ReadAsStringAsync().ConfigureAwait(false));
                },
                    () => Task.FromResult(lazyResponse.Value.Headers.ETag?.Tag),
                    TimeSpan.FromHours(1),
                    TimeSpan.FromMilliseconds(100));
                counter1.Should().Be(1);
                counter2.Should().Be(1);

                await Task.Delay(150);

                lazyResponse = LazyUtils.ToLazy(async() =>
                {
                    Interlocked.Increment(ref counter1);
                    var request = new HttpRequestMessage(HttpMethod.Get, $"{baseUrl}{etag}");
                    return(await client.SendAsync(request).ConfigureAwait(false));
                });
                cache.GetAutoReloadDataWithCache(
                    baseUrl,
                    async() =>
                {
                    Interlocked.Increment(ref counter2);
                    return(await lazyResponse.Value.Content.ReadAsStringAsync().ConfigureAwait(false));
                },
                    () => Task.FromResult(lazyResponse.Value.Headers.ETag?.Tag),
                    TimeSpan.FromHours(1),
                    TimeSpan.FromMilliseconds(100));
                counter1.Should().Be(1);
                counter2.Should().Be(1);

                etag = Guid.NewGuid();
                await Task.Delay(50);

                lazyResponse = LazyUtils.ToLazy(async() =>
                {
                    Interlocked.Increment(ref counter1);
                    var request = new HttpRequestMessage(HttpMethod.Get, $"{baseUrl}{etag}");
                    return(await client.SendAsync(request).ConfigureAwait(false));
                });
                cache.GetAutoReloadDataWithCache(
                    baseUrl,
                    async() =>
                {
                    Interlocked.Increment(ref counter2);
                    return(await lazyResponse.Value.Content.ReadAsStringAsync().ConfigureAwait(false));
                },
                    () => Task.FromResult(lazyResponse.Value.Headers.ETag?.Tag),
                    TimeSpan.FromHours(1),
                    TimeSpan.FromMilliseconds(100));
                counter1.Should().Be(2);
                counter2.Should().Be(1);

                await Task.Delay(150);

                lazyResponse = LazyUtils.ToLazy(async() =>
                {
                    Interlocked.Increment(ref counter1);
                    var request = new HttpRequestMessage(HttpMethod.Get, $"{baseUrl}{etag}");
                    return(await client.SendAsync(request).ConfigureAwait(false));
                });
                cache.GetAutoReloadDataWithCache(
                    baseUrl,
                    async() =>
                {
                    Interlocked.Increment(ref counter2);
                    return(await lazyResponse.Value.Content.ReadAsStringAsync().ConfigureAwait(false));
                },
                    () => Task.FromResult(lazyResponse.Value.Headers.ETag?.Tag),
                    TimeSpan.FromHours(1),
                    TimeSpan.FromMilliseconds(100));
                counter1.Should().Be(3);
                counter2.Should().Be(2);
            }
        }
        public void TestToAsyncLazy_Disposable()
        {
            var lazy = LazyUtils.ToAsyncLazy(async() => new MemoryStream());

            (lazy is IDisposable).Should().BeTrue();
        }
        public void TestToAsyncLazy_NonDisposable()
        {
            var lazy = LazyUtils.ToAsyncLazy(async() => 3);

            (lazy is IDisposable).Should().BeTrue();
        }
        public void TestToLazy_AsyncNonDisposable()
        {
            var lazy = LazyUtils.ToLazy(async() => 3);

            (lazy is IDisposable).Should().BeFalse();
        }