/
MemcachedSessionStateStore.cs
316 lines (275 loc) · 12.2 KB
/
MemcachedSessionStateStore.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using System;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Configuration;
using System.Web.SessionState;
using BeIT.MemCached;
/// <summary>
/// A .NET Session Store that uses Memcached
/// Created by Rob Blankaert
/// Copyright 2012 appFigures
/// </summary>
public class MemcachedSessionStateStore : SessionStateStoreProviderBase
{
public String ApplicationName { set; get; }
/// <summary>
/// An invalidation key
/// </summary>
public String VersionKey = "0";
/// <summary>
/// Default name of memcached pool. This can (and should) be overridden in Web.Config
/// </summary>
private String pool = "sessions";
public bool WriteExceptionsToEventLog { get; set; }
private SessionStateSection sessionConfig = null;
private MemcachedClient _client = null;
private MemcachedClient client
{
get
{
if (_client == null) {
_client = MemcachedClient.GetInstance(pool);
}
return _client;
}
}
public MemcachedSessionStateStore() { }
/// <summary>
/// Initializes the provider.
/// </summary>
/// <param name="name">A friednly name for the provider.</param>
/// <param name="config">A config object for the provider.</param>
public override void Initialize(String name, System.Collections.Specialized.NameValueCollection config)
{
// Initialize values from Web.Config
if (config == null) throw new ArgumentNullException("config");
if (name == null || name.Length == 0) name = "MemcachedSessionStateStore";
if (String.IsNullOrEmpty(config["description"])) {
config.Remove("description");
config.Add("description", "BeITMemcached based Session Store");
}
if (!String.IsNullOrEmpty(config["pool"])) {
pool = config["pool"];
}
// Initialize the abstract base class
base.Initialize(name, config);
// Initialize the ApplicationName property
ApplicationName = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;
// Get <sessionState> configuration element
Configuration cfg = WebConfigurationManager.OpenWebConfiguration(ApplicationName);
ConfigurationSection sec = cfg.GetSection("system.web/sessionState");
sessionConfig = (SessionStateSection)sec;
}
/// <summary>
/// Sets a reference to the SessionStateItemExpireCallback delegate for the Session_OnEnd event defined in the Global.asax file.
/// </summary>
/// <param name="expireCallback">The SessionStateItemExpireCallback delegate for the Session_OnEnd event defined in the Global.asax file.</param>
/// <returns>true if the session-state store provider supports calling the Session_OnEnd event; otherwise, false.</returns>
/// <remarks>This callback is not supported in this provider</remarks>
public override bool SetItemExpireCallback(SessionStateItemExpireCallback expireCallback)
{
return false;
}
/// <summary>
/// Updates a locked session and releases the lock.
/// </summary>
/// <param name="context">The current request's HttpContext.</param>
/// <param name="id">The session id for the current request.</param>
/// <param name="item">The SessionStateStoreData object that contains the current session values to be stored.</param>
/// <param name="lockId">The lock id for the current request.</param>
/// <param name="newItem">Whether this is a new item or not.</param>
public override void SetAndReleaseItemExclusive(HttpContext context, String id, SessionStateStoreData item, object lockId, bool newItem)
{
// Set session
client.Set(GetSessionHash(id), Serialize((SessionStateItemCollection)item.Items), sessionConfig.Timeout);
// Remove lock (no longer exclusive)
client.Delete(GetSessionLockHash(id));
}
/// <summary>
/// Unlocks a session.
/// </summary>
/// <param name="context">The current request's HttpContext.</param>
/// <param name="id">The session id for the current request.</param>
/// <param name="lockId">The lock id for the current request.</param>
public override void ReleaseItemExclusive(HttpContext context, String id, object lockId)
{
client.Delete(GetSessionLockHash(id));
}
/// <summary>
/// Retrieves the contents of the specified session id.
/// </summary>
/// <param name="context"></param>
/// <param name="id">The session id for the current request.</param>
/// <param name="locked">Whether the requested session is currently locked.</param>
/// <param name="lockAge">Amount of time this session has been locked for.</param>
/// <param name="lockId">The lock id for the current request.</param>
/// <param name="actions"></param>
/// <returns>The SessionStateStoreData if found, null if not.</returns>
public override SessionStateStoreData GetItem(HttpContext context, String id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
{
locked = false;
lockId = 0;
lockAge = TimeSpan.Zero;
actions = SessionStateActions.None;
byte[] o = GetSessionData(id);
if (o != null) {
return new SessionStateStoreData(Deserialize(o), null, (int)sessionConfig.Timeout.TotalMinutes);
} else {
return CreateNewStoreData(context, (int)sessionConfig.Timeout.TotalMinutes);
}
}
/// <summary>
/// Retrieves the contents of the specified session id and locks it for the duration of the request.
/// </summary>
/// <param name="context">The current request's HttpContext.</param>
/// <param name="id">The session id for the current request.</param>
/// <param name="locked">Whether the requested session is currently locked.</param>
/// <param name="lockAge">Amount of time this session has been locked for.</param>
/// <param name="lockId">The lock id for the current request.</param>
/// <param name="actions"></param>
/// <returns>The SessionStateStoreData if found, null if not.</returns>
public override SessionStateStoreData GetItemExclusive(HttpContext context, String id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
{
locked = false;
lockAge = TimeSpan.Zero;
lockId = null;
actions = SessionStateActions.None;
// Try to get the lock
if (!client.Add(GetSessionLockHash(id), new LockInfo(), DateTime.Now.AddSeconds(10))) {
LockInfo l = GetLock(id);
if (l != null) {
locked = true;
lockId = l.LockID;
lockAge = DateTime.Now.Subtract(l.LockTime);
return null;
}
}
byte[] o = GetSessionData(id);
if (o != null) {
return new SessionStateStoreData(Deserialize(o), null, (int)sessionConfig.Timeout.TotalMinutes);
} else {
return CreateNewStoreData(context, (int)sessionConfig.Timeout.TotalMinutes);
}
}
/// <summary>
/// Deletes a session.
/// </summary>
/// <param name="context">The current request's HttpContext.</param>
/// <param name="id">The session id for the current request.</param>
/// <param name="lockId">The lock id for the current request.</param>
/// <param name="item">The SessionStateStoreData to be removed.</param>
/// <remarks>The SessionStateStoreData is not used in this case since we use the session id to generate the Memcached key.</remarks>
public override void RemoveItem(HttpContext context, String id, object lockId, SessionStateStoreData item)
{
client.Delete(GetSessionHash(id));
// Notice we're not removing the lock here even though
// we should, but since session ids are unique there's no reason
// to waste time waiting for the delete to finish.
// If you want to delete the lock object anyway uncomment the line below
//client.Delete(GetSessionLockHash(id));
}
/// <summary>
/// Creates a new SessionStateStoreData object to be used for the current request.
/// </summary>
/// <param name="context">The current request's HttpContext.</param>
/// <param name="timeout">The session's timeout.</param>
/// <returns>A SessionStateStoreData to use for this request.</returns>
public override SessionStateStoreData CreateNewStoreData(HttpContext context, int timeout)
{
return new SessionStateStoreData(new SessionStateItemCollection(),
SessionStateUtility.GetSessionStaticObjects(context),
timeout);
}
/// <summary>
/// Updates the expiration date and time for the given session id.
/// </summary>
/// <param name="context">The current request's HttpContext.</param>
/// <param name="id">The session id for the current request.</param>
public override void ResetItemTimeout(HttpContext context, String id)
{
byte[] o = GetSessionData(id);
if (o != null) client.Set(GetSessionHash(id), o, sessionConfig.Timeout);
}
public override void CreateUninitializedItem(HttpContext context, String id, int timeout) { }
public override void InitializeRequest(HttpContext context) { }
public override void EndRequest(HttpContext context) { }
public override void Dispose() { }
/// <summary>
/// Converts the session's content into a byte array so we can store it in Memcached.
/// </summary>
/// <param name="items">The session's items that should be stored in Memcached.</param>
/// <returns>A byte array that can be saved in Memcached.</returns>
private static byte[] Serialize(SessionStateItemCollection items)
{
MemoryStream ms = new MemoryStream();
BinaryWriter writer = new BinaryWriter(ms);
if (items != null) items.Serialize(writer);
writer.Close();
return ms.ToArray();
}
/// <summary>
/// Converts a session's content from its cached representation so we can use it.
/// </summary>
/// <param name="serializedItems">Raw data from Memcached.</param>
/// <returns>A SessionStateItemCollection.</returns>
private static SessionStateItemCollection Deserialize(byte[] serializedItems)
{
if (serializedItems != null) {
MemoryStream ms = new MemoryStream(serializedItems);
if (ms.Length > 0) {
BinaryReader reader = new BinaryReader(ms);
return SessionStateItemCollection.Deserialize(reader);
}
}
return new SessionStateItemCollection();
}
/// <summary>
/// Creates a unique id for the session item.
/// </summary>
/// <param name="id">The session id for the current request.</param>
/// <returns>A unique key for this session.</returns>
private String GetSessionHash(String id)
{
return String.Format("SESSION:{0}:{1}:{2}",
VersionKey,
ApplicationName,
id);
}
/// <summary>
/// Creates a unique id for the session's lock item.
/// </summary>
/// <param name="id">The session id for the current request.</param>
/// <returns>A unique key for this session's lock.</returns>
private String GetSessionLockHash(String id)
{
return String.Format("SESSION:{0}:{1}:{2}:LOCK",
VersionKey,
ApplicationName,
id);
}
/// <summary>
/// Retreive session data from Memcached.
/// </summary>
/// <param name="id">The session id to look up.</param>
/// <returns>A byte array with data for the requested session id.</returns>
private byte[] GetSessionData(String id)
{
Object o = client.Get(GetSessionHash(id));
try {
byte[] b = (byte[])o;
return b;
} catch (InvalidCastException) {
return null;
}
}
/// <summary>
/// Gets a LockInfo object for the given session id.
/// </summary>
/// <param name="id">The id of the session to get the LockInfo for.</param>
/// <returns>The corresponding LockInfo object, null if none exists.</returns>
private LockInfo GetLock(String id)
{
return client.Get(GetSessionLockHash(id)) as LockInfo;
}
}