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

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

@ -27,4 +27,5 @@ nuget.exe
project.lock.json project.lock.json
/.vs/ /.vs/
.build/ .build/
.testPublish/ .testPublish/
launchSettings.json

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

@ -24,11 +24,11 @@ namespace Microsoft.AspNetCore.ResponseCaching.Internal
public Task<IResponseCacheEntry> GetAsync(string key) public Task<IResponseCacheEntry> GetAsync(string key)
{ {
var entry = _cache.Get(key); 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, Created = memoryCachedResponse.Created,
StatusCode = memoryCachedResponse.StatusCode, StatusCode = memoryCachedResponse.StatusCode,
@ -44,15 +44,15 @@ namespace Microsoft.AspNetCore.ResponseCaching.Internal
public async Task SetAsync(string key, IResponseCacheEntry entry, TimeSpan validFor) 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); var segmentStream = new SegmentWriteStream(StreamUtilities.BodySegmentSize);
await cachedResponse.Body.CopyToAsync(segmentStream); await cachedResponse.Body.CopyToAsync(segmentStream);
_cache.Set( _cache.Set(
key, key,
new MemoryCachedResponse() new MemoryCachedResponse
{ {
Created = cachedResponse.Created, Created = cachedResponse.Created,
StatusCode = cachedResponse.StatusCode, StatusCode = cachedResponse.StatusCode,
@ -60,7 +60,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Internal
BodySegments = segmentStream.GetSegments(), BodySegments = segmentStream.GetSegments(),
BodyLength = segmentStream.Length BodyLength = segmentStream.Length
}, },
new MemoryCacheEntryOptions() new MemoryCacheEntryOptions
{ {
AbsoluteExpirationRelativeToNow = validFor AbsoluteExpirationRelativeToNow = validFor
}); });
@ -70,7 +70,7 @@ namespace Microsoft.AspNetCore.ResponseCaching.Internal
_cache.Set( _cache.Set(
key, key,
entry, entry,
new MemoryCacheEntryOptions() new MemoryCacheEntryOptions
{ {
AbsoluteExpirationRelativeToNow = validFor 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.CachedResponse = cachedResponse;
context.CachedResponseHeaders = new ResponseHeaders(cachedResponse.Headers); context.CachedResponseHeaders = new ResponseHeaders(cachedResponse.Headers);
context.ResponseTime = _options.SystemClock.UtcNow; context.ResponseTime = _options.SystemClock.UtcNow;
@ -171,24 +177,26 @@ namespace Microsoft.AspNetCore.ResponseCaching
context.BaseKey = _keyProvider.CreateBaseKey(context); context.BaseKey = _keyProvider.CreateBaseKey(context);
var cacheEntry = await _store.GetAsync(context.BaseKey); 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 // Request contains vary rules, recompute key(s) and try again
context.CachedVaryByRules = (CachedVaryByRules)cacheEntry; context.CachedVaryByRules = cachedVaryByRules;
foreach (var varyKey in _keyProvider.CreateLookupVaryByKeys(context)) foreach (var varyKey in _keyProvider.CreateLookupVaryByKeys(context))
{ {
cacheEntry = await _store.GetAsync(varyKey); if (await TryServeCachedResponseAsync(context, await _store.GetAsync(varyKey)))
if (cacheEntry is CachedResponse && await TryServeCachedResponseAsync(context, (CachedResponse)cacheEntry))
{ {
return true; 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) if (context.RequestCacheControlHeaderValue.OnlyIfCached)