Skip to content

Commit b5e34f1

Browse files
committed
Create ICacheDependency interface and implement it in memory cache and mark kernel cache as TODO items
1. Create ICacheDependency interface 2. Implement it in memory cache 3. Mark kernel cache as TODO items. We will reimplement in next verion which can behavior base on netfx version
1 parent 92fcec2 commit b5e34f1

7 files changed

+90
-23
lines changed

src/OutputCacheModuleAsync/DependencyCacheEntry.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@
55
sealed class DependencyCacheEntry {
66
public string RawResponseKey;
77
public string KernelCacheUrl;
8-
public string Name;
9-
}
10-
11-
sealed class DependencyCacheEntryWrapper {
12-
public DependencyCacheEntry DependencyCacheEntry;
13-
public CacheDependency Dependencies;
14-
public TimeSpan DependencyCacheTimeSpan;
15-
public CacheItemPriority CacheItemPriority;
16-
public CacheItemRemovedCallback DependencyRemovedCallback;
8+
public string ProviderName;
179
}
1810
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace Microsoft.AspNet.OutputCache {
2+
using System.Runtime.Caching;
3+
using System.Threading.Tasks;
4+
5+
/// <summary>
6+
/// This interface is for the async outputCache providers to implement if they want to support Cache Dependencies
7+
/// </summary>
8+
public interface ICacheDependencyHandler {
9+
/// <summary>
10+
/// Async Add method that takes cacheItemPolicy as parameter
11+
/// </summary>
12+
/// <param name="key"></param>
13+
/// <param name="entry"></param>
14+
/// <param name="cacheItemPolicy"></param>
15+
/// <returns></returns>
16+
Task<object> AddAsync(string key, object entry, CacheItemPolicy cacheItemPolicy);
17+
18+
/// <summary>
19+
/// Async Set method that takes cacheItemPolicy as parameter
20+
/// </summary>
21+
/// <param name="key"></param>
22+
/// <param name="entry"></param>
23+
/// <param name="cacheItemPolicy"></param>
24+
/// <returns></returns>
25+
Task SetAsync(string key, object entry, CacheItemPolicy cacheItemPolicy);
26+
}
27+
}

src/OutputCacheModuleAsync/InMemoryOutputCacheProvider.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,34 @@
44
using System.Threading.Tasks;
55
using System.Web.Caching;
66

7-
class InMemoryOutputCacheProvider : OutputCacheProviderAsync {
8-
private readonly MemoryCache _cache = new MemoryCache("Microsoft.AspNet.OutputCache Default In - Memory Provider");
7+
class InMemoryOutputCacheProvider : OutputCacheProviderAsync, ICacheDependencyHandler {
8+
private readonly static MemoryCache _cache = new MemoryCache("InMemoryOutputCacheProvider");
99

10-
public override Task<object> GetAsync(string key) {
11-
return Task.FromResult(Get(key));
10+
public override Task<object> AddAsync(string key, object entry, DateTime utcExpiry) {
11+
DateTimeOffset expiration = (utcExpiry == Cache.NoAbsoluteExpiration) ? ObjectCache.InfiniteAbsoluteExpiration : utcExpiry;
12+
return Task.FromResult(_cache.AddOrGetExisting(key, entry, expiration));
1213
}
1314

14-
public override Task<object> AddAsync(string key, object entry, DateTime utcExpiry) {
15-
return Task.FromResult(Add(key,entry,utcExpiry));
15+
Task<object> ICacheDependencyHandler.AddAsync(string key, object entry, CacheItemPolicy cacheItemPolicy) {
16+
return Task.FromResult(_cache.AddOrGetExisting(key, entry, cacheItemPolicy));
17+
}
18+
19+
Task ICacheDependencyHandler.SetAsync(string key, object entry, CacheItemPolicy cacheItemPolicy) {
20+
_cache.Set(key, entry, cacheItemPolicy);
21+
return Task.CompletedTask;
22+
}
23+
24+
public override Task<object> GetAsync(string key) {
25+
return Task.FromResult(_cache.Get(key));
1626
}
1727

1828
public override Task SetAsync(string key, object entry, DateTime utcExpiry) {
19-
Set(key, entry, utcExpiry);
29+
_cache.Set(key, entry, expiration);
2030
return Task.CompletedTask;
2131
}
2232

23-
public override Task RemoveAsync(string key) {
24-
Remove(key);
33+
public override Task RemoveAsync(string key) {
34+
_cache.Remove(key);
2535
return Task.CompletedTask;
2636
}
2737

src/OutputCacheModuleAsync/Microsoft.AspNet.OutputCache.OutputCacheModuleAsync.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<Compile Include="HttpMethods.cs" />
5656
<Compile Include="CacheDirectives.cs" />
5757
<Compile Include="HttpHeaders.cs" />
58+
<Compile Include="ICacheDependencyHandler.cs" />
5859
<Compile Include="InMemoryOutputCacheProvider.cs" />
5960
<Compile Include="DependencyCacheEntry.cs" />
6061
<Compile Include="CachedRawResponse.cs" />
@@ -64,6 +65,7 @@
6465
<Compile Include="OutputCacheHelper.cs" />
6566
<Compile Include="OutputCacheModuleAsync.cs" />
6667
<Compile Include="Properties\AssemblyInfo.cs" />
68+
<Compile Include="RemovedCallback.cs" />
6769
<Compile Include="Resource\SR.Designer.cs">
6870
<AutoGen>True</AutoGen>
6971
<DesignTime>True</DesignTime>

src/OutputCacheModuleAsync/OutputCacheModuleAsync.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,7 @@ private async Task OnEnterAsync(object source, EventArgs eventArgs) {
9494
}
9595
helper.UpdateCachedResponse(settings, cachedRawResponse.RawResponse);
9696

97-
//Re-insert entry in kernel cache if necessary
98-
string originalCacheUrl = cachedRawResponse.KernelCacheUrl;
99-
if (originalCacheUrl != null) {
100-
OutputCacheUtility.SetupKernelCaching(originalCacheUrl, app.Context.Response);
101-
}
97+
//TODO Re-insert entry in kernel cache if necessary
10298

10399
//Complete request
104100
app.CompleteRequest();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace Microsoft.AspNet.OutputCache {
2+
using System.Runtime.Caching;
3+
using System.Web.Caching;
4+
5+
/// <summary>
6+
/// This class convert System.Web.Caching.CacheItemRemovedCallback into format of System.Runtime.Caching.CacheEntryRemovedCallback
7+
/// </summary>
8+
sealed class RemovedCallback {
9+
CacheItemRemovedCallback _callback;
10+
11+
public RemovedCallback(CacheItemRemovedCallback callback) {
12+
_callback = callback;
13+
}
14+
15+
public void CacheEntryRemovedCallback(CacheEntryRemovedArguments arguments) {
16+
string key = arguments.CacheItem.Key;
17+
object value = arguments.CacheItem.Value;
18+
CacheItemRemovedReason reason;
19+
switch (arguments.RemovedReason) {
20+
case CacheEntryRemovedReason.Removed:
21+
reason = CacheItemRemovedReason.Removed;
22+
break;
23+
case CacheEntryRemovedReason.Expired:
24+
reason = CacheItemRemovedReason.Expired;
25+
break;
26+
case CacheEntryRemovedReason.Evicted:
27+
reason = CacheItemRemovedReason.Underused;
28+
break;
29+
case CacheEntryRemovedReason.ChangeMonitorChanged:
30+
reason = CacheItemRemovedReason.DependencyChanged;
31+
break;
32+
default:
33+
reason = CacheItemRemovedReason.Removed;
34+
break;
35+
}
36+
_callback(key, value, reason);
37+
}
38+
}
39+
}

test/CustomOutputCacheProvider/Microsoft.AspNet.OutputCache.CustomOutputCacheProvider.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<Reference Include="System" />
4040
<Reference Include="System.Configuration" />
4141
<Reference Include="System.Core" />
42+
<Reference Include="System.Runtime.Caching" />
4243
<Reference Include="System.Web" />
4344
<Reference Include="System.Xml.Linq" />
4445
<Reference Include="System.Data.DataSetExtensions" />

0 commit comments

Comments
 (0)