public void Can_render_SlidingExpiration_as_ttlstrategy() { TimeSpan forwardTimeSpan = TimeSpan.FromDays(1); DistributedCacheEntryOptions entryOptions = new DistributedCacheEntryOptions() { SlidingExpiration = forwardTimeSpan }; ITtlStrategy ttlStrategy = entryOptions.AsTtlStrategy(); ttlStrategy.Should().BeOfType <SlidingTtl>(); Ttl ttl = ttlStrategy.GetTtl(noContext, null); ttl.SlidingExpiration.Should().BeTrue(); ttl.Timespan.Should().BeCloseTo(forwardTimeSpan); }
public void Can_render_AbsoluteExpirationRelativeToNow_as_ttlstrategy() { TimeSpan forwardTimeSpan = TimeSpan.FromDays(1); DistributedCacheEntryOptions entryOptions = new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = forwardTimeSpan }; ITtlStrategy ttlStrategy = entryOptions.AsTtlStrategy(); ttlStrategy.Should().BeOfType <RelativeTtl>(); Ttl ttl = ttlStrategy.GetTtl(noContext, null); ttl.SlidingExpiration.Should().BeFalse(); ttl.Timespan.Should().BeCloseTo(forwardTimeSpan, 10000); }
public void Can_render_AbsoluteExpiration_as_ttlstrategy() { TimeSpan forwardTimeSpan = TimeSpan.FromDays(1); DateTime date = DateTime.Now.Add(forwardTimeSpan); DistributedCacheEntryOptions entryOptions = new DistributedCacheEntryOptions() { AbsoluteExpiration = date }; ITtlStrategy ttlStrategy = entryOptions.AsTtlStrategy(); ttlStrategy.Should().BeOfType <AbsoluteTtl>(); Ttl ttl = ttlStrategy.GetTtl(noContext); ttl.SlidingExpiration.Should().BeFalse(); ttl.Timespan.Should().BeCloseTo(forwardTimeSpan, 10000); }
/// <summary> /// Gets a TTL for a cacheable item, given the current execution context and result. /// </summary> /// <param name="context">The execution context.</param> /// <param name="result">The execution result.</param> /// <returns>A <see cref="Ttl"/> representing the remaining Ttl of the cached item.</returns> public Ttl GetTtl(Context context, TResult result) { return(_wrappedTtlStrategy.GetTtl(context, (object)result)); }
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); }
internal static TResult Implementation <TResult>( ISyncCacheProvider <TResult> cacheProvider, ITtlStrategy ttlStrategy, Func <Context, string> cacheKeyStrategy, Func <Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken, 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(action(context, cancellationToken)); } TResult valueFromCache; try { valueFromCache = cacheProvider.Get(cacheKey); } 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 = action(context, cancellationToken); Ttl ttl = ttlStrategy.GetTtl(context); if (ttl.Timespan > TimeSpan.Zero) { try { cacheProvider.Put(cacheKey, result, ttl); onCachePut(context, cacheKey); } catch (Exception ex) { onCachePutError(context, cacheKey, ex); } } return(result); }