/// <summary> /// 更新响应到缓存 /// </summary> /// <param name="cacheKey">缓存键</param> /// <returns></returns> public async Task SetAsync(string cacheKey) { if (this.enable == false) { return; } if (this.attribute is IApiActionCachePolicyAttribute policy) { if (policy.GetWritePolicy(this.context) == CachePolicy.Ignore) { return; } } if (string.IsNullOrEmpty(cacheKey) == true) { cacheKey = await this.attribute.GetCacheKeyAsync(this.context).ConfigureAwait(false); if (string.IsNullOrEmpty(cacheKey) == true) { return; } } var httpResponse = this.context.ResponseMessage; var cacheEntry = await ResponseCacheEntry.FromResponseMessageAsync(httpResponse).ConfigureAwait(false); await this.provider.SetAsync(cacheKey, cacheEntry, attribute.Expiration).ConfigureAwait(false); }
/// <summary> /// 响应缓存结果 /// </summary> /// <param name="value">缓存的值</param> /// <param name="hasValue">是否有缓存的值</param> public ResponseCacheResult(ResponseCacheEntry value, bool hasValue) { this.Value = value; this.HasValue = hasValue; }
/// <summary> /// 设置响应实体到缓存 /// </summary> /// <param name="key">键</param> /// <param name="entry">缓存实体</param> /// <param name="expiration">有效时间</param> /// <returns></returns> public Task SetAsync(string key, ResponseCacheEntry entry, TimeSpan expiration) { this.cache.Set(key, entry, DateTimeOffset.Now.Add(expiration)); return(ApiTask.CompletedTask); }