-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathMemCache.cs
198 lines (180 loc) · 8.52 KB
/
MemCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// <copyright file="MemCache.cs" company="Microsoft">
// Copyright (c) 2009 Microsoft Corporation. All rights reserved.
// </copyright>
#if USE_MEMORY_CACHE
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Security;
using System.Security.Permissions;
using System.Reflection;
using System.Runtime.Caching;
using System.Text;
using System.Web.Configuration;
using System.Web.Util;
namespace System.Web.Caching {
internal sealed class MemCache: CacheInternal {
private volatile bool _inited;
private static object _initLock = new object();
private MemoryCache _cacheInternal;
private MemoryCache _cachePublic;
private volatile bool _disposed;
internal MemCache(CacheCommon cacheCommon) : base(cacheCommon) {
// config initialization is done by Init.
Assembly asm = Assembly.Load("System.Runtime.Caching, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL");
Type t = asm.GetType("System.Runtime.Caching.MemoryCache", true, false);
_cacheInternal = HttpRuntime.CreateNonPublicInstance(t, new object[] {"asp_icache", null, true}) as MemoryCache;
_cachePublic = HttpRuntime.CreateNonPublicInstance(t, new object[] {"asp_pcache", null, true}) as MemoryCache;
}
protected override void Dispose(bool disposing) {
try {
_disposed = true;
if (disposing) {
if (_cacheInternal != null) {
_cacheInternal.Dispose();
}
if (_cachePublic != null) {
_cachePublic.Dispose();
}
}
}
finally {
base.Dispose(disposing);
}
}
internal override int PublicCount {
get {
return (_cachePublic != null) ? (int)_cachePublic.GetCount() : 0;
}
}
internal override long TotalCount {
get {
long internalCount = (_cacheInternal != null) ? _cacheInternal.GetCount(null) : 0;
return internalCount + PublicCount;
}
}
internal void Init(CacheSection cacheSection) {
if (_inited) {
return;
}
lock (_initLock) {
if (_inited) {
return;
}
NameValueCollection config = null;
if (cacheSection != null) {
//_enableMemoryCollection = (!cacheSection.DisableMemoryCollection);
//_enableExpiration = (!cacheSection.DisableExpiration);
int physicalMemoryLimitPercentage = cacheSection.PercentagePhysicalMemoryUsedLimit;
long cacheMemoryLimitMegabytes = cacheSection.PrivateBytesLimit << 20;
TimeSpan pollingInterval = cacheSection.PrivateBytesPollTime;
config = new NameValueCollection(3);
config["physicalMemoryLimitPercentage"] = physicalMemoryLimitPercentage.ToString(CultureInfo.InvariantCulture);
config["cacheMemoryLimitMegabytes"] = cacheMemoryLimitMegabytes.ToString(CultureInfo.InvariantCulture);
config["pollingInterval"] = pollingInterval.ToString();
}
Type t = _cacheInternal.GetType();
t.InvokeMember("UpdateConfig",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod,
null, // binder
_cacheInternal, // target
new object[] {config}, // args
CultureInfo.InvariantCulture);
t.InvokeMember("UpdateConfig",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod,
null, // binder
_cachePublic, // target
new object[] {config}, // args
CultureInfo.InvariantCulture);
_inited = true;
}
}
// return enumerator for public entries
internal override IDictionaryEnumerator CreateEnumerator() {
return (IDictionaryEnumerator)((IEnumerable)_cachePublic).GetEnumerator();
}
internal CacheEntryChangeMonitor CreateCacheEntryChangeMonitor(IEnumerable<String> keys, bool isPublic) {
return (isPublic) ? _cachePublic.CreateCacheEntryChangeMonitor(keys, null) : _cacheInternal.CreateCacheEntryChangeMonitor(keys, null);
}
private CacheItemPolicy GetPolicy(CacheEntry newEntry) {
CacheItemPolicy policy = new CacheItemPolicy();
policy.SlidingExpiration = newEntry.SlidingExpiration;
if (policy.SlidingExpiration == ObjectCache.NoSlidingExpiration) {
policy.AbsoluteExpiration = (newEntry.UtcExpires != Cache.NoAbsoluteExpiration) ? newEntry.UtcExpires : ObjectCache.InfiniteAbsoluteExpiration;
}
if (newEntry.Dependency != null) {
policy.ChangeMonitors.Add(new DependencyChangeMonitor(newEntry.Dependency));
}
policy.Priority = (newEntry.UsageBucket == 0xff) ? System.Runtime.Caching.CacheItemPriority.NotRemovable : System.Runtime.Caching.CacheItemPriority.Default;
CacheItemRemovedCallback callback = newEntry.CacheItemRemovedCallback;
if (callback != null) {
policy.RemovedCallback = (new RemovedCallback(callback)).CacheEntryRemovedCallback;
}
return policy;
}
internal override CacheEntry UpdateCache(CacheKey cacheKey,
CacheEntry newEntry,
bool replace,
CacheItemRemovedReason removedReason,
out object valueOld) {
valueOld = null;
CacheEntry entry = null;
string key = cacheKey.Key;
bool isPublic = cacheKey.IsPublic;
if (_disposed) {
return null;
}
MemoryCache cache = (isPublic) ? _cachePublic : _cacheInternal;
if (newEntry == null && !replace) {
// get
object o = cache.Get(key);
if (o != null) {
entry = new CacheEntry(key, o, null, null,
Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
CacheItemPriority.Default, isPublic);
entry.State = CacheEntry.EntryState.AddedToCache;
}
}
else if (newEntry != null && replace) {
// set
try {
}
finally {
// prevent ThreadAbortEx from interrupting these calls
CacheItemPolicy policy = GetPolicy(newEntry);
cache.Set(key, newEntry.Value, policy);
}
}
else if (newEntry != null && !replace) {
// add
try {
}
finally {
// prevent ThreadAbortEx from interrupting these calls
CacheItemPolicy policy = GetPolicy(newEntry);
Object o = cache.AddOrGetExisting(key, newEntry.Value, policy);
if (o != null) {
entry = new CacheEntry(key, o, null, null,
Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
CacheItemPriority.Default, isPublic);
entry.State = CacheEntry.EntryState.AddedToCache;
}
}
}
else {
// remove
valueOld = cache.Remove(key);
}
return entry;
}
internal override long TrimIfNecessary(int percent) {
return _cachePublic.Trim(percent) + _cacheInternal.Trim(percent);
}
internal override void EnableExpirationTimer(bool enable) {
// This is done by Dispose, so it's a no-op here
}
}
}
#endif