This commit is contained in:
moozzyk 2016-10-14 13:14:23 -07:00
Родитель 48abc69b1a
Коммит 1571a2dbe9
3 изменённых файлов: 27 добавлений и 18 удалений

1
.gitignore поставляемый
Просмотреть файл

@ -28,3 +28,4 @@ project.lock.json
/.vs/
.build/
.testPublish/
launchSettings.json

Просмотреть файл

@ -25,10 +25,10 @@ namespace Microsoft.AspNetCore.ResponseCaching.Internal
{
var entry = _cache.Get(key);
if (entry is MemoryCachedResponse)
var memoryCachedResponse = entry as MemoryCachedResponse;
if (memoryCachedResponse != null)
{
var memoryCachedResponse = (MemoryCachedResponse)entry;
return Task.FromResult<IResponseCacheEntry>(new CachedResponse()
return Task.FromResult<IResponseCacheEntry>(new CachedResponse
{
Created = memoryCachedResponse.Created,
StatusCode = memoryCachedResponse.StatusCode,
@ -44,15 +44,15 @@ namespace Microsoft.AspNetCore.ResponseCaching.Internal
public async Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan validFor)
{
if (entry is CachedResponse)
var cachedResponse = entry as CachedResponse;
if (cachedResponse != null)
{
var cachedResponse = (CachedResponse)entry;
var segmentStream = new SegmentWriteStream(StreamUtilities.BodySegmentSize);
await cachedResponse.Body.CopyToAsync(segmentStream);
_cache.Set(
key,
new MemoryCachedResponse()
new MemoryCachedResponse
{
Created = cachedResponse.Created,
StatusCode = cachedResponse.StatusCode,
@ -60,7 +60,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Internal
BodySegments = segmentStream.GetSegments(),
BodyLength = segmentStream.Length
},
new MemoryCacheEntryOptions()
new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = validFor
});
@ -70,7 +70,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Internal
_cache.Set(
key,
entry,
new MemoryCacheEntryOptions()
new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = validFor
});

Просмотреть файл

@ -111,8 +111,14 @@ namespace Microsoft.AspNetCore.ResponseCaching
}
}
internal async Task<bool> TryServeCachedResponseAsync(ResponseCacheContext context, CachedResponse cachedResponse)
internal async Task<bool> TryServeCachedResponseAsync(ResponseCacheContext context, IResponseCacheEntry cacheEntry)
{
var cachedResponse = cacheEntry as CachedResponse;
if (cachedResponse == null)
{
return false;
}
context.CachedResponse = cachedResponse;
context.CachedResponseHeaders = new ResponseHeaders(cachedResponse.Headers);
context.ResponseTime = _options.SystemClock.UtcNow;
@ -171,24 +177,26 @@ namespace Microsoft.AspNetCore.ResponseCaching
context.BaseKey = _keyProvider.CreateBaseKey(context);
var cacheEntry = await _store.GetAsync(context.BaseKey);
if (cacheEntry is CachedVaryByRules)
var cachedVaryByRules = cacheEntry as CachedVaryByRules;
if (cachedVaryByRules != null)
{
// Request contains vary rules, recompute key(s) and try again
context.CachedVaryByRules = (CachedVaryByRules)cacheEntry;
context.CachedVaryByRules = cachedVaryByRules;
foreach (var varyKey in _keyProvider.CreateLookupVaryByKeys(context))
{
cacheEntry = await _store.GetAsync(varyKey);
if (cacheEntry is CachedResponse && await TryServeCachedResponseAsync(context, (CachedResponse)cacheEntry))
if (await TryServeCachedResponseAsync(context, await _store.GetAsync(varyKey)))
{
return true;
}
}
}
else if (cacheEntry is CachedResponse && await TryServeCachedResponseAsync(context, (CachedResponse)cacheEntry))
else
{
return true;
if (await TryServeCachedResponseAsync(context, cacheEntry))
{
return true;
}
}
if (context.RequestCacheControlHeaderValue.OnlyIfCached)