Exemplo n.º 1
0
        /// <summary>
        /// 更新响应到缓存
        /// </summary>
        /// <param name="context"></param>
        /// <param name="cacheKey">缓存键</param>
        /// <param name="response">响应消息</param>
        /// <returns></returns>
        private static async Task SetCacheAsync(ApiRequestContext context, string? cacheKey, HttpResponseMessage? response)
        {
            var attribute = context.ApiAction.CacheAttribute;
            if (attribute == null)
            {
                return;
            }

            if (response == null)
            {
                return;
            }

            if (attribute.GetWritePolicy(context) == CachePolicy.Ignore)
            {
                return;
            }

            if (string.IsNullOrEmpty(cacheKey) == true)
            {
                cacheKey = await attribute.GetCacheKeyAsync(context).ConfigureAwait(false);
            }

            if (cacheKey == null)
            {
                return;
            }

            var provider = context.HttpContext.Services.GetService<IResponseCacheProvider>();
            if (provider == null)
            {
                return;
            }

            var cacheEntry = await ResponseCacheEntry.FromResponseMessageAsync(response).ConfigureAwait(false);
            await provider.SetAsync(cacheKey, cacheEntry, attribute.Expiration).ConfigureAwait(false);
        }
Exemplo n.º 2
0
 /// <summary>
 /// 响应缓存结果
 /// </summary>
 /// <param name="value">缓存的值</param>
 /// <param name="hasValue">是否有缓存的值</param>
 public ResponseCacheResult(ResponseCacheEntry value, bool hasValue)
 {
     this.Value    = value;
     this.HasValue = hasValue;
 }