Skip to content

Commit

Permalink
Updated the readme to point to the blog.
Browse files Browse the repository at this point in the history
  • Loading branch information
chadman committed Jul 6, 2012
1 parent 0c975c9 commit 44f1380
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 48 deletions.
14 changes: 1 addition & 13 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,16 +3,4 @@ Redis is an open source, advanced key-value store. It is often referred to as a
It is the perfect solution for storing session level cache objects for ASP.NET. Redis has its own built in expiration mechanisms for cleaning up old keys so no It is the perfect solution for storing session level cache objects for ASP.NET. Redis has its own built in expiration mechanisms for cleaning up old keys so no
new functionality will have to be built for that. new functionality will have to be built for that.


To use the RedisSessionStateProvider, modify the sessionState section in the web.config. Below is an example. I wrote a blog post to explain the implementation, please check it out here: http://wp.me/pPMqN-w

<sessionState mode="Custom" customProvider="RedisSessionStateProvider" cookieless="false" timeout="1">
<providers>
<add name="RedisSessionStateProvider"
type="RedisProvider.SessionProvider.CustomServiceProvider"
server="localhost"
port="6379"
password=""
writeExceptionsToEventLog="false" />
</providers>
</sessionState>

76 changes: 42 additions & 34 deletions RedisProvider/SessionProvider/RedisSessionProvider.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ private RedisClient RedisSessionClient {
#endregion Properties #endregion Properties


#region Private Methods #region Private Methods
/// <summary>
/// Prepends the application name to the redis key if one exists. Querying by application name is recommended for session
/// </summary>
/// <param name="id">The session id</param>
/// <returns>Concatenated string applicationname:sessionkey</returns>
private string RedisKey(string id) { private string RedisKey(string id) {
return string.Format("{0}{1}", !string.IsNullOrEmpty(this.ApplicationName) ? this.ApplicationName + ":" : "", id); return string.Format("{0}{1}", !string.IsNullOrEmpty(this.ApplicationName) ? this.ApplicationName + ":" : "", id);
} }
Expand All @@ -72,6 +77,7 @@ public CustomServiceProvider() {
} }
#endregion Constructor #endregion Constructor


#region Overrides
public override void Dispose() { public override void Dispose() {


} }
Expand Down Expand Up @@ -123,23 +129,6 @@ public override bool SetItemExpireCallback(SessionStateItemExpireCallback expire
return true; return true;
} }


/// <summary>
/// Serialize is called by the SetAndReleaseItemExclusive method to
/// convert the SessionStateItemCollection into a Base64 string to
/// be stored in MongoDB.
/// </summary>
private string Serialize(SessionStateItemCollection items) {
using (MemoryStream ms = new MemoryStream())
using (BinaryWriter writer = new BinaryWriter(ms)) {
if (items != null)
items.Serialize(writer);

writer.Close();

return Convert.ToBase64String(ms.ToArray());
}
}

public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem) { public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem) {
using (RedisClient client = this.RedisSessionClient) { using (RedisClient client = this.RedisSessionClient) {
// Serialize the SessionStateItemCollection as a string. // Serialize the SessionStateItemCollection as a string.
Expand Down Expand Up @@ -263,23 +252,6 @@ private SessionStateStoreData GetSessionStoreItem(bool lockRecord,
return item; return item;
} }



private SessionStateStoreData Deserialize(HttpContext context, string serializedItems, int timeout) {
using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(serializedItems))) {
SessionStateItemCollection sessionItems = new SessionStateItemCollection();

if (ms.Length > 0) {
using (BinaryReader reader = new BinaryReader(ms)) {
sessionItems = SessionStateItemCollection.Deserialize(reader);
}
}

return new SessionStateStoreData(sessionItems,
SessionStateUtility.GetSessionStaticObjects(context),
timeout);
}
}

public override void ReleaseItemExclusive(HttpContext context, string id, object lockId) { public override void ReleaseItemExclusive(HttpContext context, string id, object lockId) {


using (RedisClient client = this.RedisSessionClient) { using (RedisClient client = this.RedisSessionClient) {
Expand Down Expand Up @@ -340,5 +312,41 @@ public override void InitializeRequest(HttpContext context) {
public override void EndRequest(HttpContext context) { public override void EndRequest(HttpContext context) {
this.Dispose(); this.Dispose();
} }
#endregion Overrides

#region Serialization
/// <summary>
/// Serialize is called by the SetAndReleaseItemExclusive method to
/// convert the SessionStateItemCollection into a Base64 string to
/// be stored in MongoDB.
/// </summary>
private string Serialize(SessionStateItemCollection items) {
using (MemoryStream ms = new MemoryStream())
using (BinaryWriter writer = new BinaryWriter(ms)) {
if (items != null)
items.Serialize(writer);

writer.Close();

return Convert.ToBase64String(ms.ToArray());
}
}

private SessionStateStoreData Deserialize(HttpContext context, string serializedItems, int timeout) {
using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(serializedItems))) {
SessionStateItemCollection sessionItems = new SessionStateItemCollection();

if (ms.Length > 0) {
using (BinaryReader reader = new BinaryReader(ms)) {
sessionItems = SessionStateItemCollection.Deserialize(reader);
}
}

return new SessionStateStoreData(sessionItems,
SessionStateUtility.GetSessionStaticObjects(context),
timeout);
}
}
#endregion Serialization
} }
} }
2 changes: 1 addition & 1 deletion RedisProvider/Web.config
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<add key="PreserveLoginUrl" value="true" /> <add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" /> <add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="ApplicationName" value="RedisProvider" /> <add key="Application.Name" value="RedisProvider" />
</appSettings> </appSettings>
<system.web> <system.web>
<compilation debug="true" targetFramework="4.0" /> <compilation debug="true" targetFramework="4.0" />
Expand Down

0 comments on commit 44f1380

Please sign in to comment.