public void GetAsync_should_throw_for_cancellation() { Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache> mockDistributedCache = new Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache>(); string key = "anything"; IAsyncCacheProvider <string> provider = mockDistributedCache.Object.AsAsyncCacheProvider <string>(); Func <Task> action = () => provider.GetAsync(key, new CancellationToken(true), false); action.ShouldThrow <OperationCanceledException>(); }
public async Task GetAsync_should_return_instance_previously_stored_in_cache() { Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache> mockDistributedCache = new Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache>(); string key = "anything"; mockDistributedCache.Setup(idc => idc.GetAsync(It.Is <string>(k => k == key))).Returns(Task.FromResult(new byte[0] { })).Verifiable(); // Because GetStringAsync() is an extension method, we cannot mock it. We mock GetAsync() instead. IAsyncCacheProvider <string> provider = mockDistributedCache.Object.AsAsyncCacheProvider <string>(); string got = await provider.GetAsync(key, CancellationToken.None, false); mockDistributedCache.Verify(v => v.GetAsync(key), Times.Once); }
public async Task GetAsync_should_return_null_on_unknown_key() { Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache> mockDistributedCache = new Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache>(); string key = "anything"; mockDistributedCache.Setup(idc => idc.GetAsync(It.Is <string>(k => k == key))).Returns(Task.FromResult(new byte[0] { })).Verifiable(); // Because GetStringAsync() is an extension method, we cannot mock it. We mock GetAsync() instead. IAsyncCacheProvider <string> provider = mockDistributedCache.Object.AsAsyncCacheProvider <string>(); string someOtherKey = Guid.NewGuid().ToString(); string got = await provider.GetAsync(someOtherKey, CancellationToken.None, false); mockDistributedCache.Verify(v => v.GetAsync(someOtherKey), Times.Once); got.Should().BeNull(); }
public async Task GetAsync_should_return_instance_previously_stored_in_cache() { Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache> mockDistributedCache = new Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache>(); string key = "anything"; var cachedValue = new byte[] { 0 }; mockDistributedCache.Setup(idc => idc.GetAsync(It.Is <string>(k => k == key))).Returns(Task.FromResult(cachedValue)).Verifiable(); IAsyncCacheProvider <byte[]> provider = mockDistributedCache.Object.AsAsyncCacheProvider <byte[]>(); byte[] got = await provider.GetAsync(key, CancellationToken.None, false); mockDistributedCache.Verify(v => v.GetAsync(key), Times.Once); got.Should().BeSameAs(cachedValue); }
public async Task GetAsync_should_return_null_on_unknown_key() { Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache> mockDistributedCache = new Mock <Microsoft.Extensions.Caching.Distributed.IDistributedCache>(); string key = "anything"; var cachedValue = new byte[] { 0 }; mockDistributedCache.Setup(idc => idc.GetAsync(It.Is <string>(k => k == key))).Returns(Task.FromResult(cachedValue)).Verifiable(); IAsyncCacheProvider <byte[]> provider = mockDistributedCache.Object.AsAsyncCacheProvider <byte[]>(); string someOtherKey = Guid.NewGuid().ToString(); byte[] got = await provider.GetAsync(someOtherKey, CancellationToken.None, false); mockDistributedCache.Verify(v => v.GetAsync(someOtherKey), Times.Once); got.Should().BeNull(); }
async Task <TCacheFormat> IAsyncCacheProvider <TCacheFormat> .GetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext) { return((TCacheFormat)await _wrappedCacheProvider.GetAsync(key, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext)); }
internal static async Task <TResult> ImplementationAsync <TResult>( IAsyncCacheProvider <TResult> cacheProvider, ITtlStrategy <TResult> ttlStrategy, Func <Context, string> cacheKeyStrategy, Func <Context, CancellationToken, Task <TResult> > action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext, Action <Context, string> onCacheGet, Action <Context, string> onCacheMiss, Action <Context, string> onCachePut, Action <Context, string, Exception> onCacheGetError, Action <Context, string, Exception> onCachePutError) { cancellationToken.ThrowIfCancellationRequested(); string cacheKey = cacheKeyStrategy(context); if (cacheKey == null) { return(await action(context, cancellationToken).ConfigureAwait(continueOnCapturedContext)); } TResult valueFromCache; try { valueFromCache = await cacheProvider.GetAsync(cacheKey, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); } catch (Exception ex) { valueFromCache = default(TResult); onCacheGetError(context, cacheKey, ex); } if (valueFromCache != null && !valueFromCache.Equals(default(TResult))) { onCacheGet(context, cacheKey); return(valueFromCache); } else { onCacheMiss(context, cacheKey); } TResult result = await action(context, cancellationToken).ConfigureAwait(continueOnCapturedContext); Ttl ttl = ttlStrategy.GetTtl(context, result); if (ttl.Timespan > TimeSpan.Zero && result != null && !result.Equals(default(TResult))) { try { await cacheProvider.PutAsync(cacheKey, result, ttl, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); onCachePut(context, cacheKey); } catch (Exception ex) { onCachePutError(context, cacheKey, ex); } } return(result); }
/// <summary> /// Gets a value from the cache asynchronously. /// </summary> /// <param name="key">The cache key.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <param name="continueOnCapturedContext">Whether async calls should continue on a captured synchronization context.</param> /// <returns>A <see cref="Task{TResult}" /> promising as Result the value from cache; or null, if none was found.</returns> public async Task <object> GetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext) { TSerialized objectToDeserialize = await _wrappedCacheProvider.GetAsync(key, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); return(objectToDeserialize == null || objectToDeserialize.Equals(default(TSerialized)) ? null :_serializer.Deserialize(objectToDeserialize)); }
/// <summary> /// Gets a value from the cache asynchronously. /// </summary> /// <param name="key">The cache key.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <param name="continueOnCapturedContext">Whether async calls should continue on a captured synchronization context.</param> /// <returns>A <see cref="Task{TResult}" /> promising as Result the value from cache; or null, if none was found.</returns> public async Task <object> GetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext) { return(_serializer.Deserialize( await _wrappedCacheProvider.GetAsync(key, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext))); }