Skip to content

Commit c3428f0

Browse files
committed
fix bug for cache vary scenarios
1 parent 719bfbc commit c3428f0

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

src/OutputCacheModuleAsync/InMemoryOutputCacheProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public override object Get(string key) {
3030
}
3131

3232
public override object Add(string key, object entry, DateTime utcExpiry) {
33-
DateTimeOffset expiration = (utcExpiry == Cache.NoAbsoluteExpiration) ? ObjectCache.InfiniteAbsoluteExpiration : utcExpiry;
34-
return _cache.Add(key, entry, expiration) ? entry : null;
33+
DateTimeOffset expiration = (utcExpiry == Cache.NoAbsoluteExpiration) ? ObjectCache.InfiniteAbsoluteExpiration : utcExpiry;
34+
return _cache.AddOrGetExisting(key, entry, expiration);
3535
}
3636

3737
public override void Set(string key, object entry, DateTime utcExpiry) {

src/OutputCacheModuleAsync/OutputCacheHelper.cs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public bool IsAcceptableEncoding(string contentEncoding, string acceptEncoding)
294294
}
295295
double weight = GetAcceptableEncodingHelper(contentEncoding, acceptEncoding);
296296
return !(weight == 0) &&
297-
(!(weight <= 0) || GetAcceptableEncodingHelper(Asterisk, acceptEncoding) == 0);
297+
(!(weight <= 0) || GetAcceptableEncodingHelper(Asterisk, acceptEncoding) != 0);
298298
}
299299

300300
public bool IsResponseCacheable() {
@@ -509,7 +509,7 @@ private bool HasDependencyChanged(string depKey, string[] fileDeps, string kerne
509509
DependencyCacheTimeSpan = Cache.NoSlidingExpiration,
510510
DependencyRemovedCallback = null
511511
};
512-
memoryCache.Add(depKey, dcew, DateTimeOffset.MaxValue);
512+
memoryCache.Set(depKey, dcew, DateTimeOffset.MaxValue);
513513
return false;
514514
}
515515
// file dependencies have changed
@@ -596,24 +596,22 @@ private async Task InsertResponseAsync(string cachedVaryKey,
596596
string depKey = OutputcacheKeyprefixDependencies + dependencies.GetUniqueID();
597597
OutputCacheEntry oce = Convert(rawResponse, depKey, dependencies.GetFileDependencies());
598598
await provider.SetAsync(rawResponseKey, oce, absExp);
599-
{
600-
// use Add and dispose dependencies if there's already one in the cache
601-
var dce = new DependencyCacheEntry {
602-
RawResponseKey = rawResponseKey,
603-
KernelCacheUrl = oce.KernelCacheUrl,
604-
Name = provider.Name
605-
};
606-
var dcew = new DependencyCacheEntryWrapper {
607-
DependencyCacheEntry = dce,
608-
Dependencies = dependencies,
609-
CacheItemPriority = System.Web.Caching.CacheItemPriority.Normal,
610-
DependencyCacheTimeSpan = Cache.NoSlidingExpiration,
611-
DependencyRemovedCallback = null
612-
};
613-
object d = await provider.AddAsync(depKey, dcew, absExp);
614-
if (d != null) {
615-
dependencies.Dispose();
616-
}
599+
// use Add and dispose dependencies if there's already one in the cache
600+
var dce = new DependencyCacheEntry {
601+
RawResponseKey = rawResponseKey,
602+
KernelCacheUrl = oce.KernelCacheUrl,
603+
Name = provider.Name
604+
};
605+
var dcew = new DependencyCacheEntryWrapper {
606+
DependencyCacheEntry = dce,
607+
Dependencies = dependencies,
608+
CacheItemPriority = System.Web.Caching.CacheItemPriority.Normal,
609+
DependencyCacheTimeSpan = Cache.NoSlidingExpiration,
610+
DependencyRemovedCallback = null
611+
};
612+
object d = await provider.AddAsync(depKey, dcew, absExp);
613+
if (d != null) {
614+
dependencies.Dispose();
617615
}
618616
}
619617
}

src/OutputCacheModuleAsync/OutputCacheModuleAsync.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,15 @@ private async Task OnEnterAsync(object source, EventArgs eventArgs) {
6060
// - a CachedVary object (if the object varies by something)
6161
// - a CachedRawResponse object (i.e. it doesn't vary on anything)
6262
// First assume it's a CacheVary and try to get the cachedItem with it
63-
CachedRawResponse cachedRawResponse;
63+
CachedRawResponse cachedRawResponse = null;
6464
var cachedVary = item as CachedVary;
6565
if (cachedVary != null) {
6666
var cachedItem = await helper.GetAsCacheVaryAsync(cachedVary);
67-
if (cachedItem == null) {
68-
return;
67+
if (cachedItem != null) {
68+
cachedRawResponse = (CachedRawResponse)cachedItem;
6969
}
70-
cachedRawResponse = (CachedRawResponse)cachedItem;
7170
}
72-
else {
71+
if(cachedRawResponse == null) {
7372
cachedRawResponse = item as CachedRawResponse;
7473
}
7574
if (cachedRawResponse == null) {

0 commit comments

Comments
 (0)