Skip to content

Commit 2377781

Browse files
committed
update the example provider
1 parent 6835af8 commit 2377781

1 file changed

Lines changed: 47 additions & 85 deletions

File tree

test/CustomOutputCacheProvider/CustomOutputCacheProvider.cs

Lines changed: 47 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,113 @@
11
namespace Microsoft.AspNet.OutputCache.CustomOutputCacheProvider {
22
using System;
3-
using System.Collections.Generic;
3+
using System.Runtime.Caching;
44
using System.Threading.Tasks;
55
using System.Web.Caching;
66

7-
class CustomOutputCacheItem {
8-
public object Obj;
9-
public DateTime UtcExpiry;
10-
public CustomOutputCacheItem(object entry, DateTime utcExpiryIn) {
11-
Obj = entry;
12-
UtcExpiry = utcExpiryIn;
13-
}
14-
}
15-
167
/// <summary>
178
/// This is just a proof of concept Async OutputCache Provider. It is used for testing purpose.
189
/// </summary>
1910
public class CustomOutputCacheProvider : OutputCacheProviderAsync {
20-
21-
private readonly static Dictionary<string, CustomOutputCacheItem> _dict = new Dictionary<string, CustomOutputCacheItem>();
22-
23-
private static async Task FooAsync() {
24-
await Task.Delay(1);
25-
}
26-
11+
private readonly static MemoryCache _cache = new MemoryCache("CustomOutputCacheProvider");
12+
2713
/// <summary>
28-
/// Override method for the Async OutputCache Provider
14+
/// Asynchronously inserts the specified entry into the output cache.
2915
/// </summary>
3016
/// <param name="key"></param>
17+
/// <param name="entry"></param>
18+
/// <param name="utcExpiry"></param>
3119
/// <returns></returns>
32-
public override async Task<object> GetAsync(string key) {
33-
await FooAsync();
34-
if (!_dict.ContainsKey(key)) {
35-
return null;
36-
}
37-
if (_dict[key].UtcExpiry > DateTime.Now.ToUniversalTime()) {
38-
return _dict[key].Obj;
39-
}
40-
await RemoveAsync(key);
41-
return null;
20+
public override Task<object> AddAsync(string key, object entry, DateTime utcExpiry) {
21+
//TODO:
22+
//Replace with your own async data insertion mechanism.
23+
DateTimeOffset expiration = (utcExpiry == Cache.NoAbsoluteExpiration) ? ObjectCache.InfiniteAbsoluteExpiration : utcExpiry;
24+
return Task.FromResult(_cache.AddOrGetExisting(key, entry, expiration));
4225
}
4326

4427
/// <summary>
45-
/// Override method for the Async OutputCache Provider
28+
/// Asynchronously returns a reference to the specified entry in the output cache.
4629
/// </summary>
4730
/// <param name="key"></param>
48-
/// <param name="entry"></param>
49-
/// <param name="utcExpiry"></param>
5031
/// <returns></returns>
51-
public override async Task<object> AddAsync(string key, object entry, DateTime utcExpiry) {
52-
await FooAsync();
53-
if (_dict.ContainsKey(key) && _dict[key].UtcExpiry > DateTime.Now.ToUniversalTime()) {
54-
return _dict[key].Obj;
55-
}
56-
if (!_dict.ContainsKey(key)) {
57-
_dict.Add(key, new CustomOutputCacheItem(entry, utcExpiry));
58-
}
59-
else {
60-
entry = _dict[key].Obj;
61-
}
62-
return entry;
32+
public override Task<object> GetAsync(string key) {
33+
//TODO:
34+
//Replace with your own aysnc data retrieve mechanism.
35+
return Task.FromResult(_cache.Get(key));
6336
}
6437

6538
/// <summary>
66-
/// Override method for the Async OutputCache Provider
39+
/// Asynchronously Inserts the specified entry into the output cache, overwriting the entry if it is already cached.
6740
/// </summary>
6841
/// <param name="key"></param>
6942
/// <param name="entry"></param>
7043
/// <param name="utcExpiry"></param>
7144
/// <returns></returns>
72-
public override async Task SetAsync(string key, object entry, DateTime utcExpiry) {
73-
await FooAsync();
74-
if (_dict.ContainsKey(key)) {
75-
_dict[key] = new CustomOutputCacheItem(entry, utcExpiry);
76-
}
77-
else {
78-
_dict.Add(key, new CustomOutputCacheItem(entry, utcExpiry));
79-
}
45+
public override Task SetAsync(string key, object entry, DateTime utcExpiry) {
46+
//TODO:
47+
//Replace with your own async insertion/overwriting mechanism.
48+
DateTimeOffset expiration = (utcExpiry == Cache.NoAbsoluteExpiration) ? ObjectCache.InfiniteAbsoluteExpiration : utcExpiry;
49+
_cache.Set(key, entry, expiration);
50+
return Task.CompletedTask;
8051
}
8152

8253
/// <summary>
83-
/// Override method for the Async OutputCache Provider
54+
/// Asynchronously removes the specified entry from the output cache.
8455
/// </summary>
8556
/// <param name="key"></param>
8657
/// <returns></returns>
87-
public override async Task RemoveAsync(string key) {
88-
await FooAsync();
89-
_dict.Remove(key);
58+
public override Task RemoveAsync(string key) {
59+
//TODO:
60+
//Replace with your own async data removal mechanism.
61+
_cache.Remove(key);
62+
return Task.CompletedTask;
9063
}
9164

9265
/// <summary>
93-
/// Override method for the Async OutputCache Provider
66+
/// Returns a reference to the specified entry in the output cache.
9467
/// </summary>
9568
/// <param name="key"></param>
9669
/// <returns></returns>
9770
public override object Get(string key) {
98-
if (!_dict.ContainsKey(key)) {
99-
return null;
100-
}
101-
if (_dict[key].UtcExpiry > DateTime.Now.ToUniversalTime()) {
102-
return _dict[key].Obj;
103-
}
104-
Remove(key);
105-
return null;
71+
//TODO:
72+
//Replace with your own data retrieve mechanism.
73+
return _cache.Get(key);
10674
}
10775

10876
/// <summary>
109-
/// Override method for the Async OutputCache Provider
77+
/// Inserts the specified entry into the output cache.
11078
/// </summary>
11179
/// <param name="key"></param>
11280
/// <param name="entry"></param>
11381
/// <param name="utcExpiry"></param>
11482
/// <returns></returns>
11583
public override object Add(string key, object entry, DateTime utcExpiry) {
116-
if (_dict.ContainsKey(key) && _dict[key].UtcExpiry > DateTime.Now.ToUniversalTime()) {
117-
return _dict[key].Obj;
118-
}
119-
if (!_dict.ContainsKey(key)) {
120-
_dict.Add(key, new CustomOutputCacheItem(entry, utcExpiry));
121-
}
122-
else {
123-
entry = _dict[key].Obj;
124-
}
125-
return entry;
84+
//TODO:
85+
//Replace with your own data insertion mechanism.
86+
DateTimeOffset expiration = (utcExpiry == Cache.NoAbsoluteExpiration) ? ObjectCache.InfiniteAbsoluteExpiration : utcExpiry;
87+
return _cache.AddOrGetExisting(key, entry, expiration);
12688
}
12789

12890
/// <summary>
129-
/// Override method for the Async OutputCache Provider
91+
/// Inserts the specified entry into the output cache, overwriting the entry if it is already cached
13092
/// </summary>
13193
/// <param name="key"></param>
13294
/// <param name="entry"></param>
13395
/// <param name="utcExpiry"></param>
13496
public override void Set(string key, object entry, DateTime utcExpiry) {
135-
if (_dict.ContainsKey(key)) {
136-
_dict[key] = new CustomOutputCacheItem(entry, utcExpiry);
137-
}
138-
else {
139-
_dict.Add(key, new CustomOutputCacheItem(entry, utcExpiry));
140-
}
97+
//TODO:
98+
//Replace with your own insertion/overwriting mechanism.
99+
DateTimeOffset expiration = (utcExpiry == Cache.NoAbsoluteExpiration) ? ObjectCache.InfiniteAbsoluteExpiration : utcExpiry;
100+
_cache.Set(key, entry, expiration);
141101
}
142102

143103
/// <summary>
144-
/// Override method for the Async OutputCache Provider
104+
/// Removes the specified entry from the output cache.
145105
/// </summary>
146106
/// <param name="key"></param>
147107
public override void Remove(string key) {
148-
_dict.Remove(key);
108+
//TODO:
109+
//Replace with your own data removal mechanism.
110+
_cache.Remove(key);
149111
}
150112
}
151113
}

0 commit comments

Comments
 (0)