本文整理汇总了C#中ICache.GetAsync方法的典型用法代码示例。如果您正苦于以下问题:C# ICache.GetAsync方法的具体用法?C# ICache.GetAsync怎么用?C# ICache.GetAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ICache
的用法示例。
在下文中一共展示了ICache.GetAsync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HandleResponseAsync
public async Task<ResponseHandlerResult> HandleResponseAsync(HttpContextBase context, IResponse suggestedResponse, ICache cache, string cacheKey)
{
context.ThrowIfNull("context");
suggestedResponse.ThrowIfNull("suggestedResponse");
if (!suggestedResponse.CachePolicy.HasPolicy || cache == null || cacheKey == null)
{
return ResponseHandlerResult.ResponseNotHandled();
}
CacheItem cacheItem = await cache.GetAsync(cacheKey);
string responseETag = suggestedResponse.CachePolicy.ETag;
#region If-Match precondition header
IfMatchHeader[] ifMatchHeaders = IfMatchHeader.ParseMany(context.Request.Headers["If-Match"]).ToArray();
// Only consider If-Match headers if response status code is 2xx or 412
if (ifMatchHeaders.Any() && ((suggestedResponse.StatusCode.StatusCode >= 200 && suggestedResponse.StatusCode.StatusCode <= 299) || suggestedResponse.StatusCode.StatusCode == 412))
{
// Return 412 if no If-Match header matches the response ETag
// Return 412 if an "If-Match: *" header is present and the response has no ETag
if (ifMatchHeaders.All(arg => arg.EntityTag.Value != responseETag) ||
(responseETag == null && ifMatchHeaders.Any(arg => arg.EntityTag.Value == "*")))
{
return await WriteResponseAsync(context.Response, new Response().PreconditionFailed());
}
}
#endregion
#region If-None-Match precondition header
IfNoneMatchHeader[] ifNoneMatchHeaders = IfNoneMatchHeader.ParseMany(context.Request.Headers["If-None-Match"]).ToArray();
if (ifNoneMatchHeaders.Any())
{
// Return 304 if an If-None-Match header matches the response ETag and the request method was GET or HEAD
// Return 304 if an "If-None-Match: *" header is present, the response has an ETag and the request method was GET or HEAD
// Return 412 if an "If-None-Match: *" header is present, the response has an ETag and the request method was not GET or HEAD
if (ifNoneMatchHeaders.Any(arg => arg.EntityTag.Value == responseETag) ||
(ifNoneMatchHeaders.Any(arg => arg.EntityTag.Value == "*") && responseETag != null))
{
if (String.Equals(context.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase) || String.Equals(context.Request.HttpMethod, "HEAD", StringComparison.OrdinalIgnoreCase))
{
if (cacheItem != null)
{
cacheItem.Response.CachePolicy.Apply(context.Response.Cache);
}
else
{
suggestedResponse.CachePolicy.Apply(context.Response.Cache);
}
return await WriteResponseAsync(context.Response, new Response().NotModified());
}
return await WriteResponseAsync(context.Response, new Response().PreconditionFailed());
}
}
#endregion
#region If-Modified-Since precondition header
IfModifiedSinceHeader ifModifiedSinceHeader = IfModifiedSinceHeader.Parse(context.Request.Headers["If-Modified-Since"]);
bool validIfModifiedSinceHttpDate = ifModifiedSinceHeader != null && ifModifiedSinceHeader.HttpDate <= _systemClock.UtcDateTime;
// Only consider an If-Modified-Since header if response status code is 200 and the HTTP-date is valid
if (suggestedResponse.StatusCode.ParsedStatusCode == HttpStatusCode.OK && validIfModifiedSinceHttpDate)
{
// Return 304 if the response was cached before the HTTP-date
if (cacheItem != null && cacheItem.CachedUtcTimestamp < ifModifiedSinceHeader.HttpDate)
{
return await WriteResponseAsync(context.Response, new Response().NotModified());
}
}
#endregion
#region If-Unmodified-Since precondition header
IfUnmodifiedSinceHeader ifUnmodifiedSinceHeader = IfUnmodifiedSinceHeader.Parse(context.Request.Headers["If-Unmodified-Since"]);
bool validIfUnmodifiedSinceHttpDate = ifUnmodifiedSinceHeader != null && ifUnmodifiedSinceHeader.HttpDate <= _systemClock.UtcDateTime;
// Only consider an If-Unmodified-Since header if response status code is 2xx or 412 and the HTTP-date is valid
if (((suggestedResponse.StatusCode.StatusCode >= 200 && suggestedResponse.StatusCode.StatusCode <= 299) || suggestedResponse.StatusCode.StatusCode == 412) && validIfUnmodifiedSinceHttpDate)
{
// Return 412 if the previous response was removed from the cache or was cached again at a later time
if (cacheItem == null || cacheItem.CachedUtcTimestamp >= ifUnmodifiedSinceHeader.HttpDate)
{
return await WriteResponseAsync(context.Response, new Response().PreconditionFailed());
}
}
#endregion
#region No server caching
// Do not cache the response when the response sends a non-cacheable status code, or when an Authorization header is present
//.........这里部分代码省略.........