Permalink
Browse files

Updates to allow for not disposing of clients, and to better santize …

…output cache keys.
  • Loading branch information...
kendallb committed Sep 26, 2012
1 parent 029d151 commit 874648f952267874493fbfbe88b03a7796168d1d
@@ -8,8 +8,11 @@ namespace Couchbase.AspNet
{
public sealed class CouchbaseClientFactory : ICouchbaseClientFactory
{
- public IMemcachedClient Create(string name, NameValueCollection config)
+ public IMemcachedClient Create(string name, NameValueCollection config, out bool disposeClient)
{
+ // This client should be disposed of as it is not shared
+ disposeClient = true;
+
// Get the section name from the configuration file. If not found, create a default Couchbase client which
// will get the configuration information from the default Couchbase client section in the Web.config file
var sectionName = ProviderHelper.GetAndRemove(config, "section", false);
@@ -12,8 +12,9 @@ public interface ICouchbaseClientFactory
/// </summary>
/// <param name="name">Name of the section from the configuration file</param>
/// <param name="config">Configuration section information from the config file</param>
+ /// <param name="disposeClient">True if the client should be disposed of or not</param>
/// <returns>Instance of the couchbase client to use</returns>
- IMemcachedClient Create(string name, NameValueCollection config);
+ IMemcachedClient Create(string name, NameValueCollection config, out bool disposeClient);
}
}
@@ -1,4 +1,5 @@
using System;
+using System.Text;
using System.Collections.Specialized;
using System.Web.Caching;
using Enyim.Caching;
@@ -9,6 +10,7 @@ namespace Couchbase.AspNet.OutputCache
public class CouchbaseOutputCacheProvider : OutputCacheProvider
{
private IMemcachedClient client;
+ private bool disposeClient;
private static readonly string Prefix = (System.Web.Hosting.HostingEnvironment.SiteName ?? String.Empty).Replace(" ", "-") + "+" + System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath + "cache-";
/// <summary>
@@ -21,20 +23,21 @@ public class CouchbaseOutputCacheProvider : OutputCacheProvider
NameValueCollection config)
{
base.Initialize(name, config);
- client = ProviderHelper.GetClient(name, config, () => (ICouchbaseClientFactory)new CouchbaseClientFactory());
+ client = ProviderHelper.GetClient(name, config, () => (ICouchbaseClientFactory)new CouchbaseClientFactory(), out disposeClient);
ProviderHelper.CheckForUnknownAttributes(config);
}
/// <summary>
- /// Function to sanitize the key for use with Couchbase
+ /// Function to sanitize the key for use with Couchbase. We simply convert it to a Base 64 representation so that it will be unique and will allow
+ /// encoding of any URL
/// </summary>
/// <param name="key">Key to sanitize</param>
/// <returns>Sanitized key</returns>
private string SanitizeKey(
string key)
{
- return Prefix + key.Replace(" ", "-");
+ return Prefix + Convert.ToBase64String(Encoding.UTF8.GetBytes(key), Base64FormattingOptions.None);
}
/// <summary>
@@ -26,12 +26,12 @@ public static void CheckForUnknownAttributes(NameValueCollection nvc)
throw new System.Configuration.ConfigurationErrorsException("Unknown parameter: " + nvc.Keys[0]);
}
- public static IMemcachedClient GetClient(string name, NameValueCollection config, Func<ICouchbaseClientFactory> createDefault)
+ public static IMemcachedClient GetClient(string name, NameValueCollection config, Func<ICouchbaseClientFactory> createDefault, out bool disposeClient)
{
var factory = GetFactoryInstance(ProviderHelper.GetAndRemove(config, "factory", false), createDefault);
System.Diagnostics.Debug.Assert(factory != null, "factory == null");
- return factory.Create(name, config);
+ return factory.Create(name, config, out disposeClient);
}
private static ICouchbaseClientFactory GetFactoryInstance(string typeName, Func<ICouchbaseClientFactory> createDefault)
@@ -11,15 +11,16 @@ namespace Couchbase.AspNet.SessionState
public class CouchbaseSessionStateProvider : SessionStateStoreProviderBase
{
private IMemcachedClient client;
+ private bool disposeClient;
private static bool exclusiveAccess;
-
+
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
// Initialize the base class
base.Initialize(name, config);
// Create our Couchbase client instance
- client = ProviderHelper.GetClient(name, config, () => (ICouchbaseClientFactory)new CouchbaseClientFactory());
+ client = ProviderHelper.GetClient(name, config, () => (ICouchbaseClientFactory)new CouchbaseClientFactory(), out disposeClient);
// By default use exclusive session access. But allow it to be overridden in the config file
var exclusive = ProviderHelper.GetAndRemove(config, "exclusiveAccess", false) ?? "true";
@@ -48,7 +49,9 @@ public override void CreateUninitializedItem(HttpContext context, string id, int
public override void Dispose()
{
- client.Dispose();
+ if (disposeClient) {
+ client.Dispose();
+ }
}
public override void EndRequest(HttpContext context) { }
View
@@ -13,7 +13,7 @@ ASP.NET SessionState Provider
* You'll need .NET Framework 3.5 or later to use the precompiled binaries.
* To build the client, you'll need Visual Studio 2010 with MVC 3 installed.
-* The Nuget package for [CouchbaseNetClient 1.1.6](http://nuget.org/packages/CouchbaseNetClient) is referenced by Couchbase.AspNet
+* The Nuget package for [CouchbaseNetClient 1.0.6](http://nuget.org/packages/CouchbaseNetClient) is referenced by Couchbase.AspNet
* Couchbase Server 1.8
## Configuring the SessionState provider

0 comments on commit 874648f

Please sign in to comment.