As was reiterated in a recent post by A Main Hobbies CEO Kendall Bennett, Couchbase is a great solution for improving the performance of your ASP.NET applications.  Beyond offering sub-millisecond CRUD operations for your application’s persistence  needs, Couchbase also offers many benefits for your application’s transient data needs.

In addition to managing your ASP.NET session state with Couchbase Server, you can now use Couchbase Server as the backing store for your application’s output cache.  The latest commits to the Couchbase.AspNet project on Couchbase Labs includes the CouchbaseOutputCacheProvider.

A brief aside for those interested in implementation details or for those who have implemented an output cache provider…  It turns out that Couchbase Server is well suited for use as an output cache backing store.  With its time-to-live capabilities, creating the CouchbaseOutputCacheProvider class required very few lines of code.  There was no need to create a wrapper around the cache item just to add a persisted expiration field.  There was no need to check the expiration of an item after retrieving it, since it wouldn’t be returned by Couchbase Server if expired.

To use the new provider, you’ll configure the client as you always do:

<section name=“couchbase” type=“Couchbase.Configuration.CouchbaseClientSection, Couchbase”/>

<couchbase>
<servers bucket=“default” bucketPassword=“”>
<add uri=“http://127.0.0.1:8091/pools”/>
>
>

Then configure the outputCache provider section:

<code><outputCache defaultProvider=“CouchbaseCache”>
<providers>
<add name=“CouchbaseCache” type=“Couchbase.AspNet.OutputCache.CouchbaseOutputCacheProvider, Couchbase.AspNet” section=“couchbase-caching”/>
>
>><outputCache defaultProvider=“CouchbaseCache”>
<providers>
<add name=“CouchbaseCache” type=“Couchbase.AspNet.OutputCache.CouchbaseOutputCacheProvider, Couchbase.AspNet” section=“couchbase-caching”/>
>
>

Then configure OutputCache on your actions as you normally would (ASP.NET MVC):

[OutputCache(Duration = 60, VaryByParam=“foo”)]
public ActionResult Time(string foo)
{
return Content(DateTime.Now.ToString());
}

Or in ASP.NET WebForms, use the page directive:

<%@ OutputCache Duration=“60” VaryByParam=“foo” %>

If you’re already using Couchbase in your app and need to specify a custom section, simply provide a new section name and set a “section” attribute on the “add” node of the “providers” element as below:

<section name=“couchbaseSession” type=“Couchbase.Configuration.CouchbaseClientSection, Couchbase”/>

<couchbaseSession>
<servers bucket=“sessionState” bucketPassword=“”>
<add uri=“http://127.0.0.1:8091/pools”/>
>
>

<outputCache defaultProvider=“CouchbaseCache”>
<providers>
<add name=“Couchbase” type=“Couchbase.AspNet.SessionState.CouchbaseSessionStateProvider, Couchbase.AspNet” section=“couchbaseSession” />
>
><section name=“couchbaseSession” type=“Couchbase.Configuration.CouchbaseClientSection, Couchbase”/>

<couchbaseSession>
<servers bucket=“sessionState” bucketPassword=“”>
<add uri=“http://127.0.0.1:8091/pools”/>
>
>

<outputCache defaultProvider=“CouchbaseCache”>
<providers>
<add name=“Couchbase” type=“Couchbase.AspNet.SessionState.CouchbaseSessionStateProvider, Couchbase.AspNet” section=“couchbaseSession” />
>
>

You’re free to use the code or the Nuget package.  This is a Couchbase Labs project with the  Apache License 2.0.  Just remember though, that this isn’t an officially supported Couchbase product.  Feel free to post questions to the forums though.

Happy output-caching!

Author

Posted by John Zablocki, NET. SDK Developer, Couchbase

John Zablocki is a NET. SDK Developer at Couchbase. John is also the organizer of Beantown ALT.NET and a former adjunct at Fairfield University. You can also check out the book on Amazon named "Couchbase Essentials" which explains how to install and configure Couchbase Server.

2 Comments

  1. very comprehensive and complete article showing all aspects of this instance.

    We are normally using MVC and DOJO with ASP.NET. see examples of work and contact http://www.smallmarketingcompany.com

  2. That\’s a great news!

Leave a reply