Skip to content

Commit

Permalink
Got rid of disposing of Couchbase buckets since that is not desired i…
Browse files Browse the repository at this point in the history
…n 2.1, changed references from 'client' to 'bucket', added clear documentation to the session state store based on our private SqlSessionStateStore code and cleaned up code formatting using Resharper and a solution specific settings file.
  • Loading branch information
kendallb committed May 17, 2015
1 parent 858a7a3 commit b8aedff
Show file tree
Hide file tree
Showing 9 changed files with 696 additions and 276 deletions.
176 changes: 176 additions & 0 deletions Couchbase.AspNet.sln.DotSettings

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Couchbase.AspNet/Couchbase.AspNet.csproj
Expand Up @@ -70,9 +70,9 @@
<ItemGroup>
<Compile Include="OutputCache\CouchbaseOutputCacheProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="CouchbaseClientFactory.cs" />
<Compile Include="CouchbaseBucketFactory.cs" />
<Compile Include="SessionState\CouchbaseSessionStateProvider.cs" />
<Compile Include="ICouchbaseClientFactory.cs" />
<Compile Include="ICouchbaseBucketFactory.cs" />
<Compile Include="ProviderHelper.cs" />
<Compile Include="Web\CouchbaseHttpApplication.cs" />
</ItemGroup>
Expand Down
@@ -1,45 +1,51 @@
using System.Collections.Specialized;
using Couchbase.Core;

namespace Couchbase.AspNet
{
public sealed class CouchbaseClientFactory : ICouchbaseClientFactory
{
public IBucket Create(string name, NameValueCollection config, out bool disposeClient)
{
// This client should be disposed of as it is not shared
disposeClient = true;

// Get the bucket name to use from the configuration file and use a specific bucket if specified
var bucketName = ProviderHelper.GetAndRemove(config, "bucket", false);
if (!string.IsNullOrEmpty(bucketName))
return ClusterHelper.GetBucket(bucketName);

// If no bucket is specified, simply use the default bucket (which will be the first in the list)
return ClusterHelper.Get().OpenBucket();
}
}
}

#region [ License information ]
/* ************************************************************
*
* @author Couchbase <info@couchbase.com>
* @copyright 2012 Couchbase, Inc.
* @copyright 2012 Attila Kiskó, enyim.com
* @copyright 2012 Good Time Hobbies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ************************************************************/
using System.Collections.Specialized;
using Couchbase.Core;

namespace Couchbase.AspNet
{
public sealed class CouchbaseBucketFactory : ICouchbaseBucketFactory
{
/// <summary>
/// Returns a Couchbase bucket or create one if it does not exist
/// </summary>
/// <param name="name">Name of the section from the configuration file</param>
/// <param name="config">Configuration section information from the config file</param>
/// <returns>Instance of the couchbase bucket to use</returns>
public IBucket GetBucket(
string name,
NameValueCollection config)
{
// Get the bucket name to use from the configuration file and use a specific bucket if specified
var bucketName = ProviderHelper.GetAndRemove(config, "bucket", false);
if (!string.IsNullOrEmpty(bucketName))
return ClusterHelper.GetBucket(bucketName);

// If no bucket is specified, simply use the default bucket (which will be the first in the list)
return ClusterHelper.Get().OpenBucket();
}
}
}

#region [ License information ]
/* ************************************************************
*
* @author Couchbase <info@couchbase.com>
* @copyright 2012 Couchbase, Inc.
* @copyright 2012 Attila Kiskó, enyim.com
* @copyright 2012 Good Time Hobbies, Inc.
* @copyright 2015 AMain.com, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ************************************************************/
#endregion
@@ -1,42 +1,42 @@
using System.Collections.Specialized;
using Couchbase.Core;

namespace Couchbase.AspNet
{
public interface ICouchbaseClientFactory
{
/// <summary>
/// Returns a Couchbase client. This will be called by the provider's Initialize method. Note
/// that the instance of the client returned will be owned by the called, and will be disposed.
/// So make sure you don't return a shared instance, but create a new one.
/// </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>
IBucket Create(string name, NameValueCollection config, out bool disposeClient);
}
}

#region [ License information ]
/* ************************************************************
*
* @author Couchbase <info@couchbase.com>
* @copyright 2012 Couchbase, Inc.
* @copyright 2012 Attila Kiskó, enyim.com
* @copyright 2012 Good Time Hobbies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ************************************************************/
#endregion
using System.Collections.Specialized;
using Couchbase.Core;

namespace Couchbase.AspNet
{
public interface ICouchbaseBucketFactory
{
/// <summary>
/// Returns a Couchbase bucket or create one if it does not exist
/// </summary>
/// <param name="name">Name of the section from the configuration file</param>
/// <param name="config">Configuration section information from the config file</param>
/// <returns>Instance of the couchbase bucket to use</returns>
IBucket GetBucket(
string name,
NameValueCollection config);
}
}

#region [ License information ]
/* ************************************************************
*
* @author Couchbase <info@couchbase.com>
* @copyright 2012 Couchbase, Inc.
* @copyright 2012 Attila Kiskó, enyim.com
* @copyright 2012 Good Time Hobbies, Inc.
* @copyright 2015 AMain.com, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ************************************************************/
#endregion
47 changes: 27 additions & 20 deletions Couchbase.AspNet/OutputCache/CouchbaseOutputCacheProvider.cs
Expand Up @@ -10,9 +10,15 @@ namespace Couchbase.AspNet.OutputCache
{
public class CouchbaseOutputCacheProvider : OutputCacheProvider
{
private IBucket client;
private bool disposeClient;
private static readonly string Prefix = (System.Web.Hosting.HostingEnvironment.SiteName ?? String.Empty).Replace(" ", "-") + "+" + System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath + "cache-";
private IBucket _bucket;

/// <summary>
/// Defines the prefix for the actual cache data stored in the Couchbase bucket. Must be unique to ensure it does not conflict with
/// other applications that might be using the Couchbase bucket.
/// </summary>
private static readonly string _prefix =
(System.Web.Hosting.HostingEnvironment.SiteName ?? string.Empty).Replace(" ", "-") + "+" +
System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath + "cache-";

/// <summary>
/// Function to initialize the provider
Expand All @@ -23,9 +29,13 @@ public class CouchbaseOutputCacheProvider : OutputCacheProvider
string name,
NameValueCollection config)
{
// Initialize the base class
base.Initialize(name, config);
client = ProviderHelper.GetClient(name, config, () => (ICouchbaseClientFactory)new CouchbaseClientFactory(), out disposeClient);

// Create our Couchbase bucket instance
_bucket = ProviderHelper.GetBucket(name, config);

// Make sure no extra attributes are included
ProviderHelper.CheckForUnknownAttributes(config);
}

Expand All @@ -35,10 +45,10 @@ public class CouchbaseOutputCacheProvider : OutputCacheProvider
/// </summary>
/// <param name="key">Key to sanitize</param>
/// <returns>Sanitized key</returns>
private string SanitizeKey(
private static string SanitizeKey(
string key)
{
return Prefix + Convert.ToBase64String(Encoding.UTF8.GetBytes(key), Base64FormattingOptions.None);
return _prefix + Convert.ToBase64String(Encoding.UTF8.GetBytes(key), Base64FormattingOptions.None);
}

/// <summary>
Expand Down Expand Up @@ -75,17 +85,16 @@ public class CouchbaseOutputCacheProvider : OutputCacheProvider
// We should only store the item if it's not in the cache. So try to add it and if it
// succeeds, return the value we just stored
var expiration = ToExpiration(utcExpiry);
if (client.Insert(key, Serialize(entry), expiration).Success)
if (_bucket.Insert(key, Serialize(entry), expiration).Success)
return entry;

// If it's in the cache we should return it
var retval = DeSerialize(client.Get<byte[]>(key).Value);
var retval = DeSerialize(_bucket.Get<byte[]>(key).Value);

// If the item got evicted between the Add and the Get (very rare) we store it anyway,
// but this time with Set to make sure it always gets into the cache
if (retval == null)
{
client.Insert(key, entry, expiration);
if (retval == null) {
_bucket.Insert(key, entry, expiration);
retval = entry;
}

Expand All @@ -103,7 +112,7 @@ public class CouchbaseOutputCacheProvider : OutputCacheProvider
public override object Get(
string key)
{
var result = client.Get<byte[]>(SanitizeKey(key));
var result = _bucket.Get<byte[]>(SanitizeKey(key));
return DeSerialize(result.Value);
}

Expand All @@ -114,7 +123,7 @@ public class CouchbaseOutputCacheProvider : OutputCacheProvider
public override void Remove(
string key)
{
client.Remove(SanitizeKey(key));
_bucket.Remove(SanitizeKey(key));
}

/// <summary>
Expand All @@ -128,7 +137,7 @@ public class CouchbaseOutputCacheProvider : OutputCacheProvider
object entry,
DateTime utcExpiry)
{
client.Insert(SanitizeKey(key), Serialize(entry), ToExpiration(utcExpiry));
_bucket.Insert(SanitizeKey(key), Serialize(entry), ToExpiration(utcExpiry));
}

/// <summary>
Expand All @@ -139,8 +148,7 @@ public class CouchbaseOutputCacheProvider : OutputCacheProvider
private byte[] Serialize(
object value)
{
using (var ms = new MemoryStream())
{
using (var ms = new MemoryStream()) {
new BinaryFormatter().Serialize(ms, value);
return ms.ToArray();
}
Expand All @@ -154,12 +162,10 @@ public class CouchbaseOutputCacheProvider : OutputCacheProvider
private object DeSerialize(
byte[] bytes)
{
if (bytes == null)
{
if (bytes == null) {
return null;
}
using (var ms = new MemoryStream(bytes))
{
using (var ms = new MemoryStream(bytes)) {
return new BinaryFormatter().Deserialize(ms);
}
}
Expand All @@ -173,6 +179,7 @@ public class CouchbaseOutputCacheProvider : OutputCacheProvider
* @copyright 2012 Couchbase, Inc.
* @copyright 2012 Attila Kiskó, enyim.com
* @copyright 2012 Good Time Hobbies, Inc.
* @copyright 2015 AMain.com, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down

0 comments on commit b8aedff

Please sign in to comment.