public async Task HasCorrectDefaultConfiguredCacheTime(ODataCachedEndpoint endpoint) { var cacheTime = DefaultCacheTime[endpoint]; var target = new ODataCacheOutputAttribute(endpoint, 0); target.OnActionExecuting(ActionContext); var before = DateTimeOffset.Now; await target.OnActionExecutedAsync(ActionExecutedContext, CancellationToken.None); var after = DateTimeOffset.Now; if (cacheTime <= 0) { Cache.Verify( x => x.Add( It.IsAny <string>(), It.IsAny <object>(), It.IsAny <DateTimeOffset>(), It.IsAny <string>()), Times.Never); } else { Cache.Verify( x => x.Add( It.IsAny <string>(), It.IsAny <object>(), It.Is <DateTimeOffset>(e => before.AddSeconds(cacheTime) <= e && e <= after.AddSeconds(cacheTime)), null), Times.Once); } }
public async Task CacheHijackFalseOrNonGetSpecificPackage(ODataCachedEndpoint endpoint, bool hijack) { ActionContext.ActionArguments["hijack"] = hijack; var target = new ODataCacheOutputAttribute(endpoint, 100); target.OnActionExecuting(ActionContext); var before = DateTimeOffset.Now; await target.OnActionExecutedAsync(ActionExecutedContext, CancellationToken.None); var after = DateTimeOffset.Now; Cache.Verify( x => x.Add( It.IsAny <string>(), It.IsAny <object>(), It.IsAny <DateTimeOffset>(), null), Times.Once); }
public void SetCacheTime(ODataCachedEndpoint endpoint, int cacheTime) { switch (endpoint) { case ODataCachedEndpoint.GetSpecificPackage: CacheConfiguration.GetSpecificPackageCacheTimeInSeconds = cacheTime; break; case ODataCachedEndpoint.FindPackagesById: CacheConfiguration.FindPackagesByIdCacheTimeInSeconds = cacheTime; break; case ODataCachedEndpoint.FindPackagesByIdCount: CacheConfiguration.FindPackagesByIdCountCacheTimeInSeconds = cacheTime; break; case ODataCachedEndpoint.Search: CacheConfiguration.SearchCacheTimeInSeconds = cacheTime; break; } }
public async Task ObservesConfiguredValue(ODataCachedEndpoint endpoint) { var cacheTime = 600; SetCacheTime(endpoint, cacheTime); var target = new ODataCacheOutputAttribute(endpoint, 100); target.OnActionExecuting(ActionContext); var before = DateTimeOffset.Now; await target.OnActionExecutedAsync(ActionExecutedContext, CancellationToken.None); var after = DateTimeOffset.Now; Cache.Verify( x => x.Add( It.IsAny <string>(), It.IsAny <object>(), It.Is <DateTimeOffset>(e => before.AddSeconds(cacheTime) <= e && e <= after.AddSeconds(cacheTime)), null), Times.Once); }
/// <summary> /// The <paramref name="configurationName"/> refers to an integer property name on /// <see cref="IODataCacheConfiguration"/>. This property will be uses to override the /// <see cref="CacheOutputAttribute.ServerTimeSpan"/>. /// </summary> public ODataCacheOutputAttribute(ODataCachedEndpoint endpoint, int serverTimeSpan) { _endpoint = endpoint; _defaultServerTimeSpan = serverTimeSpan; ServerTimeSpan = serverTimeSpan; }