|
1 | 1 | namespace Microsoft.AspNet.OutputCache.CustomOutputCacheProvider { |
2 | 2 | using System; |
3 | | - using System.Collections.Generic; |
| 3 | + using System.Runtime.Caching; |
4 | 4 | using System.Threading.Tasks; |
5 | 5 | using System.Web.Caching; |
6 | 6 |
|
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 | | - |
16 | 7 | /// <summary> |
17 | 8 | /// This is just a proof of concept Async OutputCache Provider. It is used for testing purpose. |
18 | 9 | /// </summary> |
19 | 10 | 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 | + |
27 | 13 | /// <summary> |
28 | | - /// Override method for the Async OutputCache Provider |
| 14 | + /// Asynchronously inserts the specified entry into the output cache. |
29 | 15 | /// </summary> |
30 | 16 | /// <param name="key"></param> |
| 17 | + /// <param name="entry"></param> |
| 18 | + /// <param name="utcExpiry"></param> |
31 | 19 | /// <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)); |
42 | 25 | } |
43 | 26 |
|
44 | 27 | /// <summary> |
45 | | - /// Override method for the Async OutputCache Provider |
| 28 | + /// Asynchronously returns a reference to the specified entry in the output cache. |
46 | 29 | /// </summary> |
47 | 30 | /// <param name="key"></param> |
48 | | - /// <param name="entry"></param> |
49 | | - /// <param name="utcExpiry"></param> |
50 | 31 | /// <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)); |
63 | 36 | } |
64 | 37 |
|
65 | 38 | /// <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. |
67 | 40 | /// </summary> |
68 | 41 | /// <param name="key"></param> |
69 | 42 | /// <param name="entry"></param> |
70 | 43 | /// <param name="utcExpiry"></param> |
71 | 44 | /// <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; |
80 | 51 | } |
81 | 52 |
|
82 | 53 | /// <summary> |
83 | | - /// Override method for the Async OutputCache Provider |
| 54 | + /// Asynchronously removes the specified entry from the output cache. |
84 | 55 | /// </summary> |
85 | 56 | /// <param name="key"></param> |
86 | 57 | /// <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; |
90 | 63 | } |
91 | 64 |
|
92 | 65 | /// <summary> |
93 | | - /// Override method for the Async OutputCache Provider |
| 66 | + /// Returns a reference to the specified entry in the output cache. |
94 | 67 | /// </summary> |
95 | 68 | /// <param name="key"></param> |
96 | 69 | /// <returns></returns> |
97 | 70 | 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); |
106 | 74 | } |
107 | 75 |
|
108 | 76 | /// <summary> |
109 | | - /// Override method for the Async OutputCache Provider |
| 77 | + /// Inserts the specified entry into the output cache. |
110 | 78 | /// </summary> |
111 | 79 | /// <param name="key"></param> |
112 | 80 | /// <param name="entry"></param> |
113 | 81 | /// <param name="utcExpiry"></param> |
114 | 82 | /// <returns></returns> |
115 | 83 | 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); |
126 | 88 | } |
127 | 89 |
|
128 | 90 | /// <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 |
130 | 92 | /// </summary> |
131 | 93 | /// <param name="key"></param> |
132 | 94 | /// <param name="entry"></param> |
133 | 95 | /// <param name="utcExpiry"></param> |
134 | 96 | 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); |
141 | 101 | } |
142 | 102 |
|
143 | 103 | /// <summary> |
144 | | - /// Override method for the Async OutputCache Provider |
| 104 | + /// Removes the specified entry from the output cache. |
145 | 105 | /// </summary> |
146 | 106 | /// <param name="key"></param> |
147 | 107 | public override void Remove(string key) { |
148 | | - _dict.Remove(key); |
| 108 | + //TODO: |
| 109 | + //Replace with your own data removal mechanism. |
| 110 | + _cache.Remove(key); |
149 | 111 | } |
150 | 112 | } |
151 | 113 | } |
|
0 commit comments